[R] Stacked geom_bar with aggregated SE -ggplot2

John Kane jrkrideau at inbox.com
Tue Apr 30 18:12:06 CEST 2013


Hi Olvia, 
Welcome to the R-help list.

Are you seriously proposing to add error bars to a stacked barplot? Why?

 Do you mean a dodged barplot?

My first thought is that there probably are several better ways of presenting your data than stacked bar charts--I sometimes think that anything other than pie charts beats a stacked bar chart.

It might help to have some idea of what you are trying to do. We really need a concise statement of the problem in subject matter terms--probably a bit of an expansion on what you provided at the end of your post and then some example data and what code you have been working with.  Definately look at ?dput as a way to provide the sample data.

Have a look at https://github.com/hadley/devtools/wiki/Reproducibility for some suggestions on how to put frame a question for the R-help list.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: olivia.burge at gmail.com
> Sent: Wed, 1 May 2013 00:20:59 +1200
> To: r-help at r-project.org
> Subject: [R] Stacked geom_bar with aggregated SE -ggplot2
> 
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!



More information about the R-help mailing list