[英]How to plot multiple ACF values on the one graph
我刚刚开始使用R,并且想使用ACF查看数据中的自相关。 我的数据框(GL)看起来像这样
GL
well year month value area
684 1994 Jan 8.53 H
684 1994 Feb 8.62 H
684 1994 Mar 8.12 H
684 1994 Apr 8.21 H
684 1995 Jan 8.53 H
684 1995 Feb 8.62 H
684 1995 Mar 8.12 H
684 1995 Apr 8.21 H
684 1996 Jan 8.53 H
684 1996 Feb 8.62 H
684 1996 Mar 8.12 H
684 1996 Apr 8.21 H
101 1994 Jan 8.53 R
101 1994 Feb 8.62 R
101 1994 Mar 8.12 R
101 1994 Apr 8.21 R
101 1995 Jan 8.53 R
101 1995 Feb 8.62 R
101 1995 Mar 8.12 R
101 1995 Apr 8.21 R
101 1996 Jan 8.53 R
101 1996 Feb 8.62 R
101 1996 Mar 8.12 R
101 1996 Apr 8.21 R
我想要:
1.使用lappy或某种循环来计算每口井的ACF(我的实际数据集大约有100口井和三组)
2.在每组的一张图上绘制每个孔的ACF值(以线的形式)(因此在这种情况下,我将有两个Acf图H和R。
我可以使用split和lapply计算每个孔的ACF,例如
split <- split(GL$value,GL$well)
test <- lapply(split,acf)
但是以这种方式拆分不会保存区域信息。 如果我这样分裂:
split1 <- split(GL,GL$well)
然后,我不知道如何对每个孔的值执行lapply。
当您很好地分割数据时,
spl1 <- split(GL, GL$well)
lapply
是这样的。
lapply(spl1, function(x) acf(x$value))
不过,我们可以使它更好一些。
当我们按列表编号执行lapply
,我们得到一个“计数器”,通过它可以访问列表名称以将信息标题粘贴在一起。 使用par(mfrow=c(<rows>, <columns>))
我们可以设置图的排列。
par(mfrow=c(1, 2))
lapply(seq_along(spl1), function(x) acf(spl1[[x]]$value,
main=paste0("well ", names(spl1)[x], ", ",
"area ", unique(spl1[[x]]$area))))
结果
可能必须根据井的分组方式进行调整。
( 作为旁注:最好避免覆盖函数名称。您可以使用split()
并为结果赋予与可能引起您自己和R困惑的函数相同的名称。其他受欢迎的候选对象是data
, df
, table
。我们可以总是快速使用?
检查名称是否为“ free”,例如?df
。)
数据
# result of `dput(GL)`
GL <- structure(list(well = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("101", "684"), class = "factor"), year = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("1994", "1995", "1996"
), class = "factor"), month = structure(c(3L, 2L, 4L, 1L, 3L,
2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L,
2L, 4L, 1L), .Label = c("Apr", "Feb", "Jan", "Mar"), class = "factor"),
value = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), .Label = c("8.12",
"8.21", "8.53", "8.62"), class = "factor"), area = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("H", "R"), class = "factor")), row.names = c(NA,
-24L), class = "data.frame")
您可以使用data.table
解决它:
让我们先从数据(从你稍加修改,所以会有针对每个不同的值well
):
structure(list(well = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("101", "684"), class = "factor"), year = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("1994", "1995", "1996"), class = "factor"), month = structure(c(3L, 2L, 4L, 1L, 3L,
2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label = c("Apr", "Feb", "Jan", "Mar"), class = "factor"),
value = c(4.65144120692275, 8.98342372477055, 17.983893298544,
15.3687085728161, 8.9577708535362, 7.47583840973675, 16.6564453896135, 11.6158618542831, 23.6109819535632, 14.1604918171652, 11.3882310683839, 20.4579487598967, 3.31275907787494, 22.109053656226, 13.598402187461, 12.3686389743816, 17.9585587936454, 17.3689122993965, 7.38424337399192, 6.93579732463695, 13.2789171519689, 21.2500206897967, 13.5766511948314, 3.58588649751619), area = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("H", "R"), class = "factor")), row.names = c(NA, -24L), class = c("data.table", "data.frame"))
然后,我们为每个列表well
:
GL[, datos := .(list(value)) , by = well]
在每一行datos
变量将有对应的所有值的列表well
,所以我们可以删除他们大多只保留各自的第一行well
的,因为它已经拥有的所有信息。 这是通过GL[, .SD[1,], by = well]
因此结果将是两行数据表。 之后,我们可以链接另一个将生成并保存每个图的表达式:
GL[, .SD[1,], by = well][
, {png(filename = paste0(well, "-", area, ".png"),
width = 1600,
height = 1600,
units = "px",
res = 100);
plot(a[[1]], main = paste("Well:", well,
"Area:", area, sep = " "));
dev.off()},
by = well]
您的两个图将以“ 684-H.png”和“ 101-R.png”之类的名称保存在当前目录中。
这里的关键点: data.table
接受表达式而不仅仅是函数,因此绝对有可能生成图表并将其保存到任何给定位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.