ACF Plot with ggplot2: 设置geom_bar的宽度

24

使用acf函数可以在基本的R绘图中制作自相关函数(ACF)图。

x <- lh
acf(x)

以下代码可用于在ggplot2中获取ACF图

conf.level <- 0.95
ciline <- qnorm((1 - conf.level)/2)/sqrt(length(x))
bacf <- acf(x, plot = FALSE)
bacfdf <- with(bacf, data.frame(lag, acf))

library(ggplot2)
q <- ggplot(data=bacfdf, mapping=aes(x=lag, y=acf)) +
       geom_bar(stat = "identity", position = "identity")
q

enter image description here

问题

如何获取线条而不是柱形图或如何设置柱形图的宽度,使其看起来像线条?谢谢


2
请注意,这里有一个 ggplot2 的包装器:https://github.com/dewittpe/qwraps。使用 devtools::install_github("dewittpe/qwraps") 进行安装。 - krlmlr
这篇文章非常有用。我在想是否可以使用建议的方法创建类似于Stata的双变量时间序列交叉自相关图(Cross-correlogram)? - Konrad
2
@konrad 请尝试以下代码:library(ggfortify) p1 <- autoplot(acf(AirPassengers, plot = FALSE), conf.int.fill = '#0000FF', conf.int.value = 0.8, conf.int.type = 'ma') print(p1) library(cowplot) ggdraw(switch_axis_position(p1, axis = 'xy', keep = 'xy')) - MYaseen208
6个回答

27

使用geom_segment()绘制线段可能更好

library(ggplot2)

set.seed(123)
x <- arima.sim(n = 200, model = list(ar = 0.6))

bacf <- acf(x, plot = FALSE)
bacfdf <- with(bacf, data.frame(lag, acf))

q <- ggplot(data = bacfdf, mapping = aes(x = lag, y = acf)) +
       geom_hline(aes(yintercept = 0)) +
       geom_segment(mapping = aes(xend = lag, yend = 0))
q

在此输入图片描述


4
这个答案很老(但仍然有用)。只是想提醒您忘记添加 geom_hline(aes(yintercept = ciline), linetype = 3, color = 'darkblue') + geom_hline(aes(yintercept = -ciline), linetype = 3, color = 'darkblue') 来模仿原始基础绘图中的线条。 - Romain
虚线:linetype=2 - Rottmann

5
使用width=0的geom_errorbar怎么样?
ggplot(data=bacfdf, aes(x=lag, y=acf)) + 
    geom_errorbar(aes(x=lag, ymax=acf, ymin=0), width=0)

4

@konrad; 请尝试以下代码:

library(ggfortify)
p1 <- autoplot(acf(AirPassengers, plot = FALSE), conf.int.fill = '#0000FF', conf.int.value = 0.8, conf.int.type = 'ma') 
print(p1) 
library(cowplot) 
ggdraw(switch_axis_position(p1, axis = 'xy', keep = 'xy'))

enter image description here


2
这段代码没有生成你所附加的图像,蓝色区域在你呈现的代码中是矩形的。 - Daniel Gardiner

3

forecast::ggAcf() 是另一个选项:

library(ggplot2)
library(forecast)

ggAcf(wineind,lag.max=24)+
  labs(title='wineind')

3
根据您的回答,我综合了一个ggplot ACF / PACF绘图方法:
    require(zoo)
    require(tseries)
    require(ggplot2)
    require(cowplot)

    ts= zoo(data[[2]]) # data[[2]] because my time series data was the second column

    # Plot ACP / ACF with IC
    # How to compute IC for ACF and PACF :
    # https://stats.stackexchange.com/questions/211628/how-is-the-confidence-interval-calculated-for-the-acf-function
    ic_alpha= function(alpha, acf_res){
      return(qnorm((1 + (1 - alpha))/2)/sqrt(acf_res$n.used))
    }

    ggplot_acf_pacf= function(res_, lag, label, alpha= 0.05){
      df_= with(res_, data.frame(lag, acf))
      
      # IC alpha
      lim1= ic_alpha(alpha, res_)
      lim0= -lim1
      
      
      ggplot(data = df_, mapping = aes(x = lag, y = acf)) +
        geom_hline(aes(yintercept = 0)) +
        geom_segment(mapping = aes(xend = lag, yend = 0)) +
        labs(y= label) +
        geom_hline(aes(yintercept = lim1), linetype = 2, color = 'blue') +
        geom_hline(aes(yintercept = lim0), linetype = 2, color = 'blue')
    }

    acf_ts= ggplot_acf_pacf(res_= acf(ts, plot= F)
                   , 20
                   , label= "ACF")
    pacf_ts= ggplot_acf_pacf(res_= pacf(ts, plot= F)
                         , 20
                         , label= "PACF")
    # Concat our plots
    acf_pacf= plot_grid(acf_ts, pacf_ts, ncol = 2, nrow = 1)
    acf_pacf

结果:

enter image description here


2

使用预测包中的ggtsdisplay函数,可以使用ggplot绘制ACF和PACF。其中x是模型拟合后的残差(fit$residuals)。

forecast::ggtsdisplay(x,lag.max=30)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接