1

I have a stacked bar graph that shows the differences in classes between skeleton and tissue. The total of the two will always be 100 and their standard errors are the same. As such, the top error bar is superfluous and adds confusion. Is there a way to only have the standard error for the bottom group? This link shows how to get a single bar for the top of the stack but isn't quite what I need: Single error bar on stacked bar plot ggplot Thanks.

enter image description here

Code:

library(reshape2)
library(Rmisc)
library(ggplot2)

melt <- melt(file, id=c("TREATMENT", "Species"), 
             value.name="Amount", variable.name = "Class")

x1 <- summarySE(melt, measurevar = "Amount", 
                groupvars = c("Species", "TREATMENT", "Class"), na.rm=TRUE)
x2 <- within(x1,lit2 <- ave(Amount, Class, Species, FUN = cumsum))

p10 <- ggplot(x2, aes(y = Amount, x = Class, fill = TREATMENT)) +
  geom_bar(stat = "identity", colour = "black") + 
  geom_errorbar(aes(ymin = lit2-se, ymax = lit2+se), size = .5, width = .25)
p10

Data:

structure(list(TREATMENT = c("SKELETON", "SKELETON", "SKELETON", 
"SKELETON", "TISSUE", "TISSUE", "TISSUE", "TISSUE"), Species = c("A", 
"A", "A", "A", "A", "A", "A", "A"), `1` = c(42.1958615095789, 
73.6083881998577, 62.1025409404354, 21.5264243794993, 57.8041384904211, 
26.3916118001423, 37.8974590595646, 78.4735756205007), `2` = c(46.9398719372755, 
89.6865089817669, 55.9907366318623, 18.1145895471236, 53.0601280627245, 
10.3134910182331, 44.0092633681377, 81.8854104528764), `3` = c(55.4637732254405, 
75.0933095632366, 20, 18.402199079204, 44.5362267745594, 24.9066904367634, 
80, 81.597800920796)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L), .Names = c("TREATMENT", "Species", 
"1", "2", "3"))
Leonardo
  • 2,439
  • 33
  • 17
  • 31
J.Con
  • 4,101
  • 4
  • 36
  • 64
  • 2
    What do you mean by saying "This link shows how to get a single bar for the top of the stack but isn't quite what I need"? I wonder if you want to subset data for the error bar and draw a graphic. – jazzurro Sep 18 '16 at 08:11
  • 1
    Exaclty what jazzurro said. every graphics layer supports overriding data so change you geom_errorbar to `geom_errorbar(aes(ymin=lit2-se, ymax=lit2+se), size=.5, width=.25, data=subset(x2, TREATMENT=='TISSUE'))` – Sergej Andrejev Sep 18 '16 at 08:27
  • +1 apiece for the first 2 comments. Perfecto! @csgillespie `summarySE` is an `Rmisc` function that generates a `data.frame` of mean, sd, se etc. – J.Con Sep 18 '16 at 22:16

0 Answers0