用ggplot2绘制ACF图:设置geom_bar的宽度

时间:2021-04-12 15:01:20

With acf we can make ACF plot in base R graph.

有了acf,我们就可以在底图R中绘制acf图。

x <- lh
acf(x)

用ggplot2绘制ACF图:设置geom_bar的宽度

The following code can be used to get the ACF plot in ggplot2.

可以使用以下代码在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

用ggplot2绘制ACF图:设置geom_bar的宽度

Question

问题

How to get lines rather than bars or how to set the width of bars so that they look like lines? Thanks

如何获得线条而不是条棒或者如何设置条形的宽度,使它们看起来像线条?谢谢

4 个解决方案

#1


17  

You're probably better off plotting with line segments via geom_segment()

最好通过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

用ggplot2绘制ACF图:设置geom_bar的宽度

#2


5  

How about using geom_errorbar with width=0?

使用宽度为0的geom_errorbar怎么样?

ggplot(data=bacfdf, aes(x=lag, y=acf)) + 
    geom_errorbar(aes(x=lag, ymax=acf, ymin=0), width=0)

#3


3  

@konrad; try the following code:

@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'))

用ggplot2绘制ACF图:设置geom_bar的宽度

#4


0  

From the forecast package comes a function ggtsdisplay that plots both ACF and PACF with ggplot. x is the residuals from the model fit (fit$residuals).

从预报包中,有一个函数ggtsdisplay,它用ggplot绘制ACF和PACF。x是模型拟合的残差(fit$residuals)。

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

#1


17  

You're probably better off plotting with line segments via geom_segment()

最好通过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

用ggplot2绘制ACF图:设置geom_bar的宽度

#2


5  

How about using geom_errorbar with width=0?

使用宽度为0的geom_errorbar怎么样?

ggplot(data=bacfdf, aes(x=lag, y=acf)) + 
    geom_errorbar(aes(x=lag, ymax=acf, ymin=0), width=0)

#3


3  

@konrad; try the following code:

@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'))

用ggplot2绘制ACF图:设置geom_bar的宽度

#4


0  

From the forecast package comes a function ggtsdisplay that plots both ACF and PACF with ggplot. x is the residuals from the model fit (fit$residuals).

从预报包中,有一个函数ggtsdisplay,它用ggplot绘制ACF和PACF。x是模型拟合的残差(fit$residuals)。

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