gpt4 book ai didi

右 |在内部组合使用 par(mfrow = ...) 的图

转载 作者:行者123 更新时间:2023-12-04 12:00:16 24 4
gpt4 key购买 nike

plot.acf为例。 acfpacf 都在内部调用这个函数。我怎样才能并排绘制它们?

示例:

TS <- ts.union(mdeaths, fdeaths)
acf(TS)
pacf(TS)

我尝试使用 par(mfrow = c(2,4))layout 来组合它们,但是 stats:::plot.acf 覆盖它。预期的输出将是:

enter image description here

最佳答案

与我的其他答案不同的方法:使用 ggplot2 绘制 ACF。

ggacf <- function(x, ci=0.95, type="correlation", xlab="Lag", ylab=NULL,
ylim=NULL, main=NULL, ci.col="blue", lag.max=NULL) {

x <- as.data.frame(x)

x.acf <- acf(x, plot=F, lag.max=lag.max, type=type)

ci.line <- qnorm((1 - ci) / 2) / sqrt(x.acf$n.used)

d.acf <- data.frame(lag=x.acf$lag, acf=x.acf$acf)

g <- ggplot(d.acf, aes(x=lag, y=acf)) +
geom_hline(yintercept=0) +
geom_segment(aes(xend=lag, yend=0)) +
geom_hline(yintercept=ci.line, color=ci.col, linetype="dashed") +
geom_hline(yintercept=-ci.line, color=ci.col, linetype="dashed") +
theme_bw() +
xlab("Lag") +
ggtitle(ifelse(is.null(main), "", main)) +
if (is.null(ylab))
ylab(ifelse(type=="partial", "PACF", "ACF"))
else
ylab(ylab)

g
}

这旨在创建与 plot.acf() 类似的界面。然后,您可以使用 gridExtra 包中的 ggplot2 绘图可用的所有强大功能。

library(ggplot2)
library(gridExtra)

grid.arrange(ggacf(lh), ggacf(lh, type="partial"), ncol=2)

然后你得到这个:

enter image description here

不幸的是 grid.arrange() 不适用于基础图形,因此建议使用 ggplot2

关于右 |在内部组合使用 par(mfrow = ...) 的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28857241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com