r - Error bars on stacked bar ggplot2 -


i'm struggling put error bars correct place on stacked bar. read on earlier post used ddply in order stack error bars. changed order of stacking ordered factor. appears error bars correct on 1 set of bars not other. want graph looks below, standard error shown error bars. i'm listing dput of original data , ddply data data set.enter image description here

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 both ybegin , yend, range of errorbar, low bacteria data. since bars bacteria on top of fungi bars, height of fungi bars (plydat$copy[plydat$org == "fungi"]) has added errorbar values of 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"]) 

enter image description here


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -