Single error bar on stacked bar plot ggplot

后端 未结 1 341
渐次进展
渐次进展 2021-01-15 08:40

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 t

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-15 09:17

    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

    0 讨论(0)
提交回复
热议问题