ACF Plot with ggplot2: Setting width of geom_bar

前端 未结 4 1027
时光说笑
时光说笑 2021-02-18 23:25

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

x <- lh
acf(x)

相关标签:
4条回答
  • 2021-02-19 00:08

    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).

    forecast::ggtsdisplay(x,lag.max=30)
    
    0 讨论(0)
  • 2021-02-19 00:12

    @konrad; try the following code:

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

    0 讨论(0)
  • 2021-02-19 00:19

    You're probably better off plotting with line segments via 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
    

    enter image description here

    0 讨论(0)
  • 2021-02-19 00:19

    How about using geom_errorbar with width=0?

    ggplot(data=bacfdf, aes(x=lag, y=acf)) + 
        geom_errorbar(aes(x=lag, ymax=acf, ymin=0), width=0)
    
    0 讨论(0)
提交回复
热议问题