Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACF Plot with ggplot2: Setting width of geom_bar

Tags:

r

ggplot2

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

x <- lh
acf(x)

enter image description here

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

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

Question

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

like image 885
MYaseen208 Avatar asked Jul 22 '13 13:07

MYaseen208


People also ask

How do I change the width of a bar in ggplot2?

To Increase or Decrease width of Bars of BarPlot, we simply assign one more width parameter to geom_bar() function. We can give values from 0.00 to 1.00 as per our requirements.

How do I reduce the space between bars in ggplot2?

Set the width of geom_bar() to a small value to obtain narrower bars with more space between them. By default, the width of bars is 0.9 (90% of the resolution of the data). You can set this argument to a lower value to get bars that are narrower with more space between them.

How do I increase the space between grouped bars in ggplot2?

How do I increase the space between grouped bars in ggplot2? For grouped bars, there is no space between bars within each group by default. However, you can add some space between bars within a group, by making the width smaller and setting the value for position_dodge to be larger than width.

How do I increase the size of the bar in R?

Adjusting Bar Width and Spacing To make the bars narrower or wider, set the width of each bar with the width argument. Larger values make the bars wider, and smaller values make the bars narrower. To add space between bars, specify the space argument. The default value is 0.2.


2 Answers

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

like image 134
Gavin Simpson Avatar answered Sep 28 '22 13:09

Gavin Simpson


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)
like image 37
shadow Avatar answered Sep 28 '22 12:09

shadow