r - 有没有办法在单个情节下绘制 2 个或更多 acfs
问题描述
我有一个时间序列数据框。我有两个连续变量,我需要为其绘制自相关 (acf) 函数。我试图用来plot_grid()
确保两个图显示在一个窗口中,但这没有发生。这是示例:
#df is dataframe; col1 and col2 are continuous variables
p1 <- acf(df$col1,lag.max = 5 ,plot = TRUE)
p2 <- acf(df$col2,lag.max = 5 ,plot = TRUE)
plot_grid(p1, p2, nrow = 2,ncol =1,rel_heights = c(2/1,2/1),rel_widths = c(2/2,2/2))
f <- structure(list(Date = structure(c(1505779200, 1505779500, 1505779800,
1505780100), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
A = c(212.429693925327, 211.464088210329, 211.653306685973,
210.981936189015), B= c(75.9448191760481, 76.2501222022257,
76.1316674891558, 76.8299563088116)), row.names = c(NA, 4L
), class = "data.frame")
p1 <- acf(f$A, lag.max = 5, plot = FALSE)
p2 <- acf(f$B, lag.max = 5, plot = FALSE)
cowplot::plot_grid(autoplot(p1), autoplot(p2), nrow = 2)
解决方案
plot_grid
来自cowplot
包(我认为)并适用于基于网格的图形对象(即来自ggplot2
/的输出lattice
)。acf()
使用基本图形。使用的东西par(mfrow ...)
应该可以工作,如下所示:
orig_pars <- par(mfrow=c(1,2))
acf(df$col1,lag.max = 5 ,plot = TRUE)
acf(df$col2,lag.max = 5 ,plot = TRUE)
pars(orig_pars) ## reset
或者你可以
- 环顾四周寻找基于网格的 ACF 绘图解决方案:有人可能已经写过
ggacf
或等效的 - 从
acf()
对象中提取数据并使用 lattice/ggplot 创建自己的图
推荐阅读
- python - 如何在不杀死未完成的芹菜任务的情况下重新启动heroku应用程序
- pdf - 如何在 Android/iOS 中处理 GeoPDF
- php - PHP邮件程序内联取消订阅而不在浏览器中打开网址
- c# - C# 是否可以为其他目的复制 using 语法?
- c# - Tor在2个端口上隐藏服务?
- android - 如何在android中使用orderByValue在firebase中推送数据?
- php - iOS 不能通过 PHP 的头文件播放 mp3 文件
- powershell - 使用 Test-Path 查询多个文件版本
- javascript - javascript:发布到 api 时序列化/反序列化类实例
- android - webpack tns 运行 android --bundle 失败,“onNavigatingTo”未定义