1
library(ggplot2)
library(plyr)
library(dplyr)
library(reshape2)


ggplot(df, aes(x=Treatment, y=Mean, fill=variable))+
           facet_wrap(~Gen)+geom_bar(stat="identity", colour="black")+
           ggtitle("G")+xlab("Stress Treatment")+ylab("Variable level")+
           geom_errorbar(aes(ymin=Mean-se, ymax=Mean+se), size=0.5,
           width=0.3, position="identity", colour="black")

stacked barplot produced using above codes

I was trying to make a stacked bar graph with error bars, can someone help me how to correctly positioned error bars in stacked graph along with anova letters?

data

https://docs.google.com/spreadsheets/d/1b734dNdd4AeVvPmB6dSmHanSPPwbJ0TfOhfPjJDswTI/edit?usp=sharing
washfaq
  • 268
  • 2
  • 12
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Dec 01 '20 at 18:05
  • As I am new to stackoverflow, unable to post data and picture, Once I get 10 reputation score, then I will be able to post it, is there any other way to post the data and picture? – washfaq Dec 02 '20 at 09:39

1 Answers1

2

This should do what you want:

Basically, you have to add your Mean values to fit in the stack, while keeping the order used by geom. There may be more efficient ways to generate tmp but this gets across the idea.

tmp <- unlist( lapply( seq(0,138,by=6), function(x) c(
df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x]+df$Mean[1+x],
df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x],
df$Mean[3+x],
df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x]+df$Mean[1+x]+df$Mean[5+x]+df$Mean[4+x],
df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x]+df$Mean[1+x]+df$Mean[5+x],
df$Mean[3+x]+df$Mean[6+x] ) ) )
df <- cbind( df, Mean_2=tmp )
ggplot(df, aes(x=Treatment, y=Mean, fill=variable))+
           facet_wrap(~Gen)+geom_bar(stat="identity", colour="black")+
           ggtitle("G")+xlab("Stress Treatment")+ylab("Variable level")+
           geom_errorbar(aes(ymin=Mean_2-se, ymax=Mean_2+se), size=0.5,
           width=0.3, position="identity", colour="black")

Result:

enter image description here

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
  • thank you for your help, I am new to "R", if you can share some link/material to understand "tmp"script in order to to use other same kind of data, that would be great. – washfaq Dec 17 '20 at 18:08
  • Here is a similar problem with good answers (https://stackoverflow.com/questions/30872977/how-to-stack-error-bars-in-a-stacked-bar-plot-using-geom-errorbar). An interesting read. My specific solution here is to give geom_errorbar a helping hand, namely the "Mean_2" column. It simply takes the original Mean values and adds on top the other Mean values that are placed underneath in the plot. Since geom_bar has an internal ordering you have to check that in the plot to generate the stacking in the Mean_2 column accordingly. – Andre Wildberg Dec 17 '20 at 18:17
  • I have tried with my data, but no luck. I am new to "R" and unable to understand tmp script to place errorbars on specific position. Thanks for your help Andre. – washfaq Dec 19 '20 at 16:28
  • How I did it: 1. copy your data into a file. 2. read.csv() your data into variable "df". 3. generate the tmp vector (see first box). 4. cbind "tmp" to df and name the column "Mean_2" (see second box). 5. Exactly your ggplot script, just replace "Mean" with "Mean_2". -- The values in the column Mean_2 (from tmp) are simply your Mean values but added where needed because they are stacked since the plot is a stacked barplot. It doesn't do that automatically because the devs don't find it useful to add error bars to stacked barplots. – Andre Wildberg Dec 19 '20 at 19:12
  • Dear Andre, Thank you for helping me, if you can help me to explain tmp script in 1st box, that would be great. My question is how you have written the values like [1+x], [2+x], [3+x] and so on; tmp <- unlist( lapply( seq(0,138,by=6), function(x) c( df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x]+df$Mean[1+x], df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x], df$Mean[3+x], df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x]+df$Mean[1+x]+df$Mean[5+x]+df$Mean[4+x], df$Mean[3+x]+df$Mean[6+x]+df$Mean[2+x]+df$Mean[1+x]+df$Mean[5+x], df$Mean[3+x]+df$Mean[6+x] ) ) ) – washfaq Dec 20 '20 at 09:48
  • Look at your data structure. Your data is organized in blocks. First block is "[C]","V1", and then all variables ("GY", "Hi", etc). These are the stacks in the stacked plot. I cycle through all of them in the right order, e.g. [3], [6], [2], [1] for "GY", and then for all variables. The order is taken from the geom_bar plotting order, which afaik cannot be automated easily. This would take care of one stacked bar. Since all blocks have the same order of variables it can be looped through with lapply by adding to the indices like [3+x]. – Andre Wildberg Dec 20 '20 at 10:04
  • Thank you Andre, I am trying after looking your comment, I hope it will gonna be fine this time. – washfaq Dec 20 '20 at 10:30
  • Dear Andre, I have edited the tmp script as per geom_bar plotting order and it is working. Thank you for your help. Very much appreciated. – washfaq Dec 20 '20 at 13:17
  • Glad that helped! You can accept the answer as solved if you wish. Happy plotting! – Andre Wildberg Dec 20 '20 at 15:29