改变由R产生的acf图

Kat*_*tyB 1 plot r

这可能是也可能不是一个非常简单的问题.但我试图改变R中产生的acf情节并且没有运气.我想改变acf看起来的方式,即改变基本情节.该图显示左侧R产生的正常acf和右侧的acf,有没有办法改变这个?在此输入图像描述

我尝试在各种搜索引擎中键入"将AC中的acf plot更改",但找不到合适的解决方案.到目前为止,我已经存储了acf输出:

a <- acf(blah)
xyplot(acf~lag,data=a,type = "l")
Run Code Online (Sandbox Code Playgroud)

这将返回acf的线图,但不保留95%置信区间.有没有人有什么建议?

Mat*_*eck 7

我可以通过使用获得类似于你想要的情节ggplot2.我在这里用过这个ldeaths例子.关键点可能是将acf对象中的值提取到data.frame.从那里你可以用它来绘制你想要的任何东西.

library(ggplot2)

# compute acf without plotting
acz <- acf(ldeaths, plot=F)

# 95% confidence interval limits
ci <- qnorm((1 + 0.95)/2)/sqrt(sum(!is.na(series)))  

# convert to data frame
acd <- data.frame(lag=acz$lag, acf=acz$acf)

# use data frame for ggplot
ggplot(acd, aes(lag, acf)) + geom_area(fill="grey") +
    geom_hline(yintercept=c(ci, -ci), linetype="dashed") +
    theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您可以ggplot2通过查看此处的文档来熟悉自己.这将有助于您进一步自定义绘图.

acf函数不会导出置信区间- 此计算在plot.acf函数内部完成.因此,在使用ggplot绘制ACF时,您需要自己计算CI边界.

  • 这只是一个问题,"+"在代码行上的位置,在直接复制到R时停止工作; `ggplot(acd,aes(lag,acf))+ geom_area(fill ="gray")+ geom_hline(yintercept = c(0.05,-0.05),linetype ="dashed")+ theme_bw()`all as one line of代码应该工作. (3认同)