With acf
we can make ACF plot
in base R
graph.
x <- lh
acf(x)
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)
@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'))
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
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)