Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing bar width when using stat_summary with ggplot

Tags:

r

ggplot2

I am using stat_summary in ggplot to plot a bar chart. I would like to change the width of the bars. Usually this is done using the width option. With pre-summarised data and stat="identity it works as expected:

data <- data.frame(group=rep(c("a","b"), 20), y=rnorm(40,100,50))
se <- function(x, na.rm=T) sd(x, na.rm=na.rm)/sqrt(length(x))
data2 <- cast(data, group ~ ., value="y", c(mean, se))
ggplot(data2, aes(group, mean, ymin=mean-1.96*se, ymax=mean+1.96*se)) +
 geom_bar(stat="identity", width=0.5) + geom_errorbar(width=0, size=2)

However, in the same plot on original data using stat_summary, the bars don't change width, while errorbars do:

ggplot(data, aes(group, y)) + stat_summary(fun.y="mean", geom="bar", width=0.5) +
 stat_summary(fun.data="mean_cl_normal", geom="errorbar", width=0, size=2)

Is there a way to change bar width even when using stat_summary?

Since the first example works, this question obviously already has a work-around, however, I would really like to know if there is any way to do it with stat_summary, because I use it a lot and is often more convenient.

Thank you!

like image 893
M4RT1NK4 Avatar asked Jul 05 '13 01:07

M4RT1NK4


1 Answers

This issue has been resolved. width now also works with stat_summary and geom="errorbar".

like image 117
tim06927 Avatar answered Nov 11 '22 05:11

tim06927