質問

I have been trying without any success to draw a stacked bar plot with a single error bar on top of each individual bar and not for each sections within bars. I can manage to draw all error bars for each section but did not find a solution to draw a single error bar.

Here is the data frame df

    Sp  Type    Or        Rate        se

1  H   Dis     Bottom    14.5454545  8.0403025
2  H   Dis     Top       2.7272727   1.9403407
3  H   Dis     WP        0.9090909   0.9090909
4  H   He      Bottom    5.4545455   1.4845392
5  H   He      Top       15.4545455  5.0797135
6  H   He      WP        0.0000000   0.0000000
7  H   HeDis   Bottom    9.0909091   3.8330638
8  H   HeDis   Top       8.1818182   4.1659779
9  H   HeDis   WP        3.6363636   2.0100756
10 N   Dis     Bottom    19.0909091  8.9329715
11 N   Dis     Top       0.0000000   0.0000000
12 N   Dis     WP        0.0000000   0.0000000
13 N   He      Bottom    22.7272727  7.0743137
14 N   He      Top       0.0000000   0.0000000
15 N   He      WP        3.6363636   2.7773186
16 N   HeDis   Bottom    14.5454545  5.2835139
17 N   HeDis   Top       10.0000000  4.3808583
18 N   HeDis   WP        0.0000000   0.0000000

Here is my script to draw the stacked bar plot with error bars for each sections within bars (the position of each error bar is not correct but this does not matter as this is not what I want):

dodge<- position_dodge(width = 0.65)
cols <- c(Top="darkgrey",Bottom="lightgrey", Well_plate="white") 
Stacked_plot_bis<-ggplot(df,
aes(x=factor(Type),y=Rate,fill=factor(Or))) +
geom_bar(aes(width=.65), stat="identity",colour="black")+
geom_errorbar(aes(ymin=Rate-se,ymax=Rate+se), position="dodge",colour="black",
width=.65)+
scale_fill_manual(values = cols)+
facet_grid(. ~ Sp)

This draws the error bars of all sections within each bar, how do I draw a single overall error bar on top of each individual bar which does not take into account the sections but only the overall value for each Type?

Any help would be very much appreciated!

役に立ちましたか?

解決

You can create a Dummy data set (i did it with data.table package) that will compute the mean(se) (although I don't think it's the best practice) and plot it as following (I've changed the color of the error bars so you could see them better)

dodge<- position_dodge(width = 0.65)
cols <- c(Top="darkgrey",Bottom="lightgrey", Well_plate="white") 
library(data.table)
Dummy <- data.table(df)[, list(Rate = sum(Rate), se = mean(se), Or = "WP"), by = c("Sp", "Type")] 
ggplot(df,aes(x=factor(Type),y=Rate,fill=factor(Or))) +
  geom_bar(aes(width=.65), stat="identity",colour="black")+
  geom_errorbar(data = Dummy, aes(ymax = Rate +se, ymin=  Rate -se), position="dodge", colour="red", width=.65)+
  scale_fill_manual(values = cols)+
  facet_grid(. ~ Sp)

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top