堆叠条ggplot2上的错误条 [英] Error bars on stacked bar ggplot2

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

问题描述

我正在努力将错误栏放到堆叠栏上的正确位置。正如我在以前的帖子中看到的,我使用ddply来堆栈错误栏。然后,这改变了堆叠的顺序,所以我订购了这个因子。现在看起来误差条在一组条上是正确的,但在另一条上却是正确的。我想要的是一张如下图所示的图形,只是带有误差条显示的标准误差。我列出了原始数据的输入和ddply数据以及数据集

  Suz2 $ org < - 因子(Suz2 $ org,levels = c('fungi','bacteria'), order = TRUE)

library(plyr)
plydat < - ddply(Suz2,。(org,group,time),transform,ybegin = copy -se,yend = copy + se )

colvec< -c(blue,orange)

ggplot(plydat,aes(time,copy))+
geom_bar(aes (fill = factor(org)),stat =identity,width = 0.7)+
scale_fill_manual(values = colvec)+
facet_wrap(〜group,nrow = 1)+
geom_errorbar (aes(ymax = ybegin,ymin = yend),width = .5)+
theme(panel.background = element_rect(fill ='white',color ='white'),
panel.grid = element_line(color = NA),
panel.grid.minor = element_line(color = NA),
panel.border = element_rect(fill = NA,color =black),
axis.tex tx = element_text(size = 10,color =black,face =bold),
axis.title.x = element_text(vjust = 0.1,face =bold),
轴。 text.y = element_text(size = 12,color =black),
axis.title.y = element_text(vjust = 0.2,size = 12,face =bold))
<



$ p $ d $ >结构(list(org)= structure(c(1L,1L,1L,1L,1L,1L,1L,
1L,2L,2L,2L,2L,2L,2L,2L,2L),.Label = c(真菌,细菌
),class = c(ordered,factor)),time = structure(c(1L,1L,
1L,1L,2L, 2L,2L,2L,1L,1L,1L,1L,2L,2L,2L,2L),标签= c(0W,
6W c(97800000,15500000,40200000,$ b $ 10400000,55100000,14300000,1.6e + 07,8640000,2.98e + 08,77900000,
2.33e + 08,2.2e + 08,3.37e + 08 (3L,1 $ b,4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L, 4L,1L,2L),.Label = c(本机D0,
本机D707,Notill D0,Notill (11100000,2810000,7110000,2910000,1.7e + 07,1500000,
1930000,2980000,43900000,2010000,56400000,420000000,210000,
75700000,22500000,57500000,28100000),ybegin = c(86700000,
12690000,33090000,7490000,38100000,12800000,14070000,
5660000,254100000,57800000,176600000,178800000,261300000 ,
65900000,266500000,160900000),yend = c(108900000,18310000,
47310000,13310000,72100000,15800000,17930000,11620000,
341900000,9.8e + 07,289400000,261200000 ,412700000,110900000,
381500000,217100000)),.Names = c(org,time,copy,
group,se,ybegin,yend ),row.names = c(NA,-16L),class =data.frame)



< (org = structure(c(1L,1L,1L,1L,1L,1L,1L,1L) 1L,1L,
1L,2L,2L,2L,2L,2L,2L,2L,2L),标签= c(真菌,细菌
)有序,因素)) ,时间=结构(c(1L,1L,
1L,1L,2L,2L,2L,2L,1L,1L,1L,1L,2L,2L,2L,2L) 0W,
6W),class =factor),copy = c(97800000,15500000,40200000,
10400000,55100000,14300000,1.6e + 07,8640000,2.98e + 08 ,77900000,
2.33e + 08,2.2e + 08,3.37e + 08,88400000,3.24e + 08,1.89e + 08),group = structure(c(3L,
4L,1L ,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L),标签= c(Native D0,
Native D707 ,Notill D707),class =factor),
se = c(11100000,2810000,7110000,2910000,1.7e + 07,1500000,
1930000,2980000,43900000,2010000, 56400000,41200000,
75700000,22500000,57500000,28100000)),.Names = c(org,
time,copy,group,se),row。 Suz2 p>

  org时间复制组se 
1真菌0W 9.78e + 07 Notill D0 11100000
2真菌0W 1.55e +07 Notill D707 2810000
3种真菌0W 4.02e + 07土着D0 7110000
4种真菌0W 1.04e + 07土着D707 2910000
5种真菌6W 5.51e + 07 Notill D0 17000000
6种真菌6W 1.43e + 07 Notill D707 1500000
7种真菌6W 1.60e + 07原产D0 1930000
8种真菌6W 8.64e + 06原产D707 2980000
9种细菌0W 2.98e + 08 Notill D0 43900000
10个细菌0W 7.79e + 07 Notill D707 20100000
11个细菌0W 2.33e + 08原生D0 56400000
12个细菌0W 2.20e + 08原生D707 41200000
13个细菌6W 3.37e +08 Notill D0 75700000
14细菌6W 8.84e + 07 Notill D707 22500000
15细菌6W 3.24e + 08原生D0 57500000
16细菌6W 1.89e + 08原生D707 28100000


解决方案

ybegin 细菌数据,错误栏的范围c $ c>和 yend 太小。由于细菌的条位于 fungi 条的顶部,因此 fungi的高度 bars( plydat $ copy [plydat $ org ==fungi] )必须添加到<$ c $的错误栏值c>细菌数据。

  plydat [plydat $ org ==bacteria,] 
< - transform(plydat [plydat $ org ==bacteria,],
ybegin = ybegin + plydat [plydat $ org ==fungi,copy],
yend = yend + plydat [plydat $ org ==fungi,copy])


I'm struggling to put error bars into the correct place on a stacked bar. As I read on an earlier post I used ddply in order to stack the error bars. Then that changed the order of the stacking so I ordered the factor. Now it appears the error bars are correct on one set of bars but not the other. What I want is a graph that looks like that below, just with the standard error shown with error bars. I'm listing the dput of the original data and the ddply data as well as the data set.

Suz2$org <- factor(Suz2$org, levels = c('fungi','bacteria'),ordered = TRUE)

library(plyr) 
plydat <- ddply(Suz2,.(org, group, time),transform,ybegin = copy - se,yend = copy + se) 

colvec <-c("blue", "orange")

ggplot(plydat, aes(time, copy)) + 
  geom_bar(aes(fill = factor(org)), stat="identity", width = 0.7) +
  scale_fill_manual(values = colvec) +
  facet_wrap(~group,nrow = 1)+
  geom_errorbar(aes(ymax=ybegin , ymin= yend ),width=.5) +
  theme(panel.background = element_rect(fill='white', colour='white'), 
        panel.grid = element_line(color = NA),
        panel.grid.minor = element_line(color = NA),
        panel.border = element_rect(fill = NA, color = "black"),
        axis.text.x  = element_text(size=10, colour="black", face = "bold"),  
        axis.title.x = element_text(vjust=0.1, face = "bold"),
        axis.text.y = element_text(size=12, colour="black"),
        axis.title.y = element_text(vjust=0.2, size = 12, face = "bold"))

dput(plydat)

structure(list(org = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("fungi", "bacteria"
), class = c("ordered", "factor")), time = structure(c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("0W", 
"6W"), class = "factor"), copy = c(97800000, 15500000, 40200000, 
10400000, 55100000, 14300000, 1.6e+07, 8640000, 2.98e+08, 77900000, 
2.33e+08, 2.2e+08, 3.37e+08, 88400000, 3.24e+08, 1.89e+08), group = structure(c(3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), .Label = c("Native D0", 
"Native D707", "Notill D0", "Notill D707"), class = "factor"), 
    se = c(11100000, 2810000, 7110000, 2910000, 1.7e+07, 1500000, 
    1930000, 2980000, 43900000, 20100000, 56400000, 41200000, 
    75700000, 22500000, 57500000, 28100000), ybegin = c(86700000, 
    12690000, 33090000, 7490000, 38100000, 12800000, 14070000, 
    5660000, 254100000, 57800000, 176600000, 178800000, 261300000, 
    65900000, 266500000, 160900000), yend = c(108900000, 18310000, 
    47310000, 13310000, 72100000, 15800000, 17930000, 11620000, 
    341900000, 9.8e+07, 289400000, 261200000, 412700000, 110900000, 
    381500000, 217100000)), .Names = c("org", "time", "copy", 
"group", "se", "ybegin", "yend"), row.names = c(NA, -16L), class = "data.frame")

dput(Suz2)

structure(list(org = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("fungi", "bacteria"
), class = c("ordered", "factor")), time = structure(c(1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("0W", 
"6W"), class = "factor"), copy = c(97800000, 15500000, 40200000, 
10400000, 55100000, 14300000, 1.6e+07, 8640000, 2.98e+08, 77900000, 
2.33e+08, 2.2e+08, 3.37e+08, 88400000, 3.24e+08, 1.89e+08), group = structure(c(3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), .Label = c("Native D0", 
"Native D707", "Notill D0", "Notill D707"), class = "factor"), 
    se = c(11100000, 2810000, 7110000, 2910000, 1.7e+07, 1500000, 
    1930000, 2980000, 43900000, 20100000, 56400000, 41200000, 
    75700000, 22500000, 57500000, 28100000)), .Names = c("org", 
"time", "copy", "group", "se"), row.names = c(NA, -16L), class = "data.frame")

Suz2

  org time     copy       group       se
1     fungi   0W 9.78e+07   Notill D0 11100000
2     fungi   0W 1.55e+07 Notill D707  2810000
3     fungi   0W 4.02e+07   Native D0  7110000
4     fungi   0W 1.04e+07 Native D707  2910000
5     fungi   6W 5.51e+07   Notill D0 17000000
6     fungi   6W 1.43e+07 Notill D707  1500000
7     fungi   6W 1.60e+07   Native D0  1930000
8     fungi   6W 8.64e+06 Native D707  2980000
9  bacteria   0W 2.98e+08   Notill D0 43900000
10 bacteria   0W 7.79e+07 Notill D707 20100000
11 bacteria   0W 2.33e+08   Native D0 56400000
12 bacteria   0W 2.20e+08 Native D707 41200000
13 bacteria   6W 3.37e+08   Notill D0 75700000
14 bacteria   6W 8.84e+07 Notill D707 22500000
15 bacteria   6W 3.24e+08   Native D0 57500000
16 bacteria   6W 1.89e+08 Native D707 28100000

解决方案

The values for both ybegin and yend, the range of the errorbar, are too low for the bacteria data. Since the bars for bacteria are on top of the fungi bars, the height of the fungi bars (plydat$copy[plydat$org == "fungi"]) has to be added to the errorbar values of the bacteria data.

plydat[plydat$org == "bacteria", ] 
   <- transform(plydat[plydat$org == "bacteria", ],
                ybegin = ybegin + plydat[plydat$org == "fungi", "copy"], 
                yend = yend + plydat[plydat$org == "fungi", "copy"])

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

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