我一直在使用一些函数式程序来避免改变结构,并且没有明确的解释哪种方法在性能方面是最好的。
在这种情况下,任何人都可以帮忙并建议性能和内存管理方面的最佳解决方案是什么?
例如:
变异选项
struct User {
var name:String
init(name:String) {
self.name = name
}
mutating func change(name:String){
self.name = name
}
}
Run Code Online (Sandbox Code Playgroud)
非变异选项
struct User {
var name:String
init(name:String) {
self.name = name
}
func change(name:String) -> User {
return User(name: name)
}
}
Run Code Online (Sandbox Code Playgroud) 我想显示一个由创建的图,geom_smooth()但是对我来说,能够描述如何创建该图很重要。
我可以从文档中看到,当n> = 1000时,使用gam作为平滑函数,但是我看不到使用了多少个结或使用哪个函数生成了平滑。
例:
library(ggplot2)
set.seed(12345)
n <- 3000
x1 <- seq(0, 4*pi,, n)
x2 <- runif(n)
x3 <- rnorm(n)
lp <- 2*sin(2* x1)+3*x2 + 3*x3
p <- 1/(1+exp(-lp))
y <- ifelse(p > 0.5, 1, 0)
df <- data.frame(x1, x2, x3, y)
# default plot
ggplot(df, aes(x = x1, y = y)) +
geom_smooth()
# specify method='gam'
# linear
ggplot(df, aes(x = x1, y = y)) +
geom_smooth(method = 'gam')
# specify gam and splines
# Shows …Run Code Online (Sandbox Code Playgroud) 在ggplot图表中,我想在副轴上重新缩放y,以使最小y值等于0,最大y值等于100。
mydata <- data.frame(x = c(0, 1, 2, 3, 4, 5),
y = c(20, 55, 69, 72, 73, 72))
break2 <- seq(0, 100, 10)
library(ggplot2)
ggplot(data = mydata, aes(x = x, y = y)) +
ylim(20, 73) +
geom_point(shape = 21, size = 5, color = "black", fill = "orange", stroke = 1) +
scale_x_continuous(name = 'Number of Contacts') +
scale_y_continuous(name = "Remembered (%)",
sec.axis = sec_axis(trans = ~ (.-min(.)) * 100 / (max(.) - min(.)),
name = "Remembered (Index)", …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 df (xyz/latlonvalue) 中的点绘制为geom_sfshapefile 上的栅格。我尝试了另一篇文章中的答案,但没有一个有帮助。数据结构如下。
首先,我尝试raster::rasterFromXYZ(df)使用其他页面上的答案中的代码,但它不起作用。每次尝试都会收到相同的错误消息。
structure(list(x = c(-87.001233, -87.416633, -86.999683, -86.58395,
-86.998233, -86.998233, -86.998233, -86.998233, -87.416633, -87.416633,
-87.416633, -87.416633, -87.001233, -87.001233, -87.001233, -87.001233,
-87.001233, -87.001233, -87.001233, -87.001233, -87.001233, -87.001233,
-86.58395, -86.58395, -86.58395, -86.58395, -86.999683, -86.999683,
-86.999683, -86.999683, -86.916517, -86.965333, -87.233917, -86.721362,
-86.916517, -86.916517, -86.916517, -86.916517, -86.916517, -86.916517,
-86.916517, -86.916517, -86.916517, -86.916517, -86.766867, -86.766867,
-86.766867, -86.766867, -87.233917, -87.233917, -87.233917, -87.233917,
-86.965333, -86.965333, -86.965333, -86.965333, -86.721362, -86.721362,
-86.721362, -86.721362, -86.721362, -86.721362, -86.721362, -86.721362,
-86.721362, -86.721362, -86.721362, -86.37455, …Run Code Online (Sandbox Code Playgroud) 我有一个像这个例子的数据:
df <-
data.frame(
date = seq(as.Date("2015-01-01"), as.Date("2015-12-31"), 100),
id = rep(LETTERS, each = 4)[1:100],
replicate(2, sample(1001, 100))
)
Run Code Online (Sandbox Code Playgroud)
用 ggplot 绘图没问题
ggplot(df, aes(x = date)) +
geom_line(aes(y = X1)) +
geom_line(aes(y = X2), linetype = 2) +
facet_wrap( ~ id , scales = "free")
Run Code Online (Sandbox Code Playgroud)
我发现了大约方面实施highchart一些信息
higcharter网测试
,我试图以使一个互动的情节与highchart绘制,但highchart::hw_grid()和browsable()已被弃用。有人可以帮我绘制数据吗?
我的片段:
highchart() %>%
hc_add_series(name = "X1", data = df$X1) %>%
hc_add_series(name = "X2", data = df$X2) %>%
hw_grid(id, rowheight = 200, ncol = 3)
Run Code Online (Sandbox Code Playgroud) 我正在 R 中使用\xe2\x80\x98openxlsx\xe2\x80\x99包。\xd9\xb0我想在 xlsx 文件中添加一些数据。我使用以下代码创建工作簿并在其中添加工作表。
\n\n wb=createWorkbook()\n addWorksheet(wb,"sheet 1")\n writeData(wb,sheet = 1,"From",startCol = 1,startRow = 1)\n writeData(wb,sheet = 1,"To",startCol = 2,startRow = 1)\n writeData(wb,sheet = 1,"From",startCol = 1,startRow = 2)\n writeData(wb,sheet = 1,"From",startCol = 1,startRow = 2)\n saveWorkbook(wb,"file.xlsx",overwrite = TRUE)\nRun Code Online (Sandbox Code Playgroud)\n\n这段代码在很长一段时间内运行良好,但最近我遇到了这个错误
\n\n\n\n\naddWorksheet(wb, "sheet 1") 中出错:第一个参数必须是\n 工作簿。
\n
这个错误将如何解决?
\n我想创建一个新的几何ggplot描述在这里,同时它与简单要素对象适应交易。
例如,让我们做同样的练习,绘制一组点的凸包。因此,我编写了一个新geom_envelope()函数,该函数借用了元素,geom_sf()并编写了一个相应的GeomEnvelope ggproto对象,该对象执行了覆盖该draw_group()方法的计算(因为我想要一个完整的点集用于单个多边形)。
但是,由于我无法绘制多边形,因此我必须缺少一些东西。我已经尝试了一段时间,但还是出现错误或没有任何内容绘制。
library(sf); library(ggplot2); library(dplyr)
Npts <- 10
pts <- matrix(runif(2*Npts), ncol = 2) %>%
st_multipoint() %>%
st_sfc() %>%
st_cast("POINT") %>%
st_sf()
GeomEnvelope <- ggproto(
"GeomEnvelope", GeomSf,
required_aes = "geometry",
default_aes = aes(
shape = NULL,
colour = "grey20",
fill = "white",
size = NULL,
linetype = 1,
alpha = 0.5,
stroke = 0.5
),
draw_key = draw_key_polygon,
draw_group = function(data, panel_params, coord) {
n <- …Run Code Online (Sandbox Code Playgroud) 我必须使用 RStudio 以 PDF 格式编写报告,但是当我尝试使用 Rmarkdown 中一个块中所需的包来编写文档时,它向我显示此错误:
tlmgr search --file --global '/multirow.sty'
tlmgr: Remote repository is newer than local (2018 < 2019)
Cross release updates are only supported with
update-tlmgr-latest(.sh/.exe) --update
Please see https://tug.org/texlive/upgrade.html for details.
! LaTeX Error: File `multirow.sty' not found.
! Emergency stop.
<read *>
Errore: Failed to compile questionario_sulla_responsabilita_222222.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See questionario_sulla_responsabilita_222222.log for more info.
Inoltre: Warning message:
In system2("tlmgr", args, ...) :
running command ''tlmgr' search --file --global '/multirow.sty'' had status …Run Code Online (Sandbox Code Playgroud) 我正在研究主成分分析 (PCA)。我发现ggfortify效果很好,但想做一些手动调整。
然后尝试绘制 PCA 结果如下:
evec <- read.table(textConnection("
PC1 PC2 PC3
-0.5708394 -0.6158420 -0.5430295
-0.6210178 -0.1087985 0.7762086
-0.5371026 0.7803214 -0.3203424"
), header = TRUE, row.names = c("M1", "M2", "M3"))
res.ct <- read.table(textConnection("
PC1 PC2 PC3
-1.762697 -1.3404825 -0.3098503
-2.349978 -0.0531175 0.6890453
-1.074205 1.5606429 -0.6406848
2.887080 -0.7272039 -0.3687029
2.299799 0.5601610 0.6301927"
), header = TRUE, row.names = c("A", "B", "C", "D", "E"))
require(ggplot2)
require(dplyr)
gpobj <-
res.ct %>%
ggplot(mapping = aes(x=PC1, y=PC2)) +
geom_point(color="grey30") +
annotate(geom="text", x=res.ct$PC1*1.07, y=res.ct$PC2*1.07,
label=rownames(res.ct)) …Run Code Online (Sandbox Code Playgroud) 我想使用该geom_label_repel函数向 ggplot 对象添加一个带有填充头部的箭头。我以为我可以使用:arrow.fill = 'black'就像我对 所做的那样geom_segment,但它在geom_label_repel. 这是获得填充箭头的另一种方法吗?
我使用 的原因geom_label_repel是它是我设法在标签边界处开始箭头的唯一方法。如果可以通过其他方式找到此坐标,我可以使用geom_segment代替。
library(tidyverse)
library(ggrepel)
dmax <- iris %>%
filter(Sepal.Length == max(Sepal.Length))
ggplot(data = iris, aes(x=Sepal.Width, y=Sepal.Length)) +
geom_point() +
geom_label_repel(data=dmax, aes(label = 'max'),
box.padding = unit(.25, 'lines'),
point.padding = unit(1.5, 'lines'),
arrow = arrow(length = unit(0.25, 'cm'), type = 'closed')) +
geom_segment(aes(x=3, xend=max(Sepal.Width), y=0, yend=max(Sepal.Width)),
arrow=arrow(length = unit(0.25, 'cm'), type = 'closed'),
arrow.fill = 'black')
Run Code Online (Sandbox Code Playgroud)