改变由R产生的acf图

时间:2012-12-14 14:57:07

标签: r plot

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

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

a <- acf(blah)
xyplot(acf~lag,data=a,type = "l")

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

2 个答案:

答案 0 :(得分: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()

enter image description here

通过查看文档here,您可以熟悉ggplot2。这将有助于您进一步自定义绘图。

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

答案 1 :(得分:1)

Coda包生成一个很好的自相关图:

enter image description here

相关问题