[R] Stacked geom_bar with aggregated SE -ggplot2

Olivia Burge olivia.burge at gmail.com
Tue Apr 30 14:20:59 CEST 2013


Hi there, 

I've been battling with an extension of this in my own data: getting
appropriate error bars once data is stacked in a bar graph. 
(original question:
http://r.789695.n4.nabble.com/ggplot2-se-variable-in-geom-errorbar-s-limits-
td3311176.html). It wouldn't let me reply to that thread.

A modification of the earlier answer: 
data(diamonds) 

 diamonds_df <- ddply(diamonds, .(cut, color), summarise, 
                        mean_price = mean(price), 
                        se_price = sd(price)/sqrt(length(price)) 
                        ) 

 limits <- aes(ymax = mean_price + se_price, ymin = mean_price - se_price) 

a<-ggplot(diamonds_df, aes(x = cut, y = mean_price, fill=color)) + 
 geom_bar(position=position_stack(width=0.9)) 
a 

Then attempting to add error bars: 
a+ geom_errorbar(limits, position=position_stack(width=0.9), width=0.9) 

I have tried:   
r + stat_sum_df("mean_cl_normal", geom = "errorbar"),  using the aggregation
function in http://www.stanford.edu/~paunesku/, using ddplyr but haven't
managed to work out a way of specifying the error bars to R.  In my case I
would like to use an error bar analogous to each value of "cut" in the data
example.   When I calculate the SEs appropriately ggplot2 won't stack. 

This is first time posting and I've scoured the various forums and websites
for a worked example.   

In case it is relevant, my response variable is fitted germination of an
lmer model ("germination"), x is a categorical variable (treatment, with 3
levels), and I am using a categorical variable ("species") to stack. 

Kind regards, 

Olivia
MSc student



More information about the R-help mailing list