Skip to main content
fixed error in `geom_segment()` layer, added dashed lines, changed `ggplot2` theme
Source Link

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

library(ggplot2) theme_set(theme_bw())  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)) ciline <- qnorm((1 - 0.95)/2)/sqrt(length(x)) q <- ggplot(data = bacfdf, mapping = aes(x = lag, y = acf)) +   geom_hline(aes(yintercept = 0)) +   geom_segment(mapping = aes(xend = lag, yend = 0)) + geom_hline(yintercept = -ciline, linetype = 2, color = 'darkblue') + geom_hline(yintercept = ciline, linetype = 2, color = 'darkblue') q 

enter image description hereenter image description here

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

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

library(ggplot2) theme_set(theme_bw())  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)) ciline <- qnorm((1 - 0.95)/2)/sqrt(length(x)) q <- ggplot(data = bacfdf, mapping = aes(x = lag, y = acf)) + geom_hline(aes(yintercept = 0)) + geom_segment(mapping = aes(xend = lag, yend = 0)) + geom_hline(yintercept = -ciline, linetype = 2, color = 'darkblue') + geom_hline(yintercept = ciline, linetype = 2, color = 'darkblue') q 

enter image description here

Source Link
Gavin Simpson
  • 175.7k
  • 28
  • 405
  • 461

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