堆积条形图ggplot上的单个错误栏 [英] Single error bar on stacked bar plot ggplot

查看:159
本文介绍了堆积条形图ggplot上的单个错误栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图没有任何成功,在每个单独的栏顶部绘制一个带有单个错误栏的堆积条形图,而不是针对条形内的每个部分。我可以设法为每个部分绘制所有错误栏,但没有找到解决方案来绘制单个错误栏。

以下是数据框df

  Sp类型或比率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 He 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.38085 83
18 N HeDis WP 0.0000000 0.0000000

下面是我的脚本来绘制堆积条形图对于每个小节内部的错误条(每个错误栏的位置都不正确,但这并不重要,因为这不是我想要的):

  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, color =black)+
geom_errorbar(aes(ymin = Rate-se,ymax = Rate + se),position =dodge,color =black,
width = .65)+
scale_fill_manual(values = cols)+
facet_grid(。 〜Sp)

这会绘制每个小节中所有节的误差线,如何绘制单个在每个单独的酒吧顶部的整体错误栏,它没有考虑到部分,但只是每个类型的总值?



任何帮助将非常感激! / p>

解决方案

您可以创建一个Dummy数据集(我使用 data.table

 闪避<  -  position_dodge(width = 0.65)
cols< - c(Top =darkgrey,Bottom =lightgrey,Well_plate =white)
library(data.table)
Dummy < - data.table(df)[,list =总和(比率),se =平均值(se),Or =WP),by = c(Sp,Type)]
ggplot(df,aes(x = factor(Type), y = Rate,fill = factor(Or)))+
geom_bar(aes(wi dash = .65),stat =identity,color =black)+
geom_errorbar(data = Dummy,aes(ymax = Rate + se,ymin = Rate -se),position =dodge, color =red,width = .65)+
scale_fill_manual(values = cols)+
facet_grid(。 〜Sp)


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)

这篇关于堆积条形图ggplot上的单个错误栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆