stacked barplot with errorbars -> errorbars are displaced

Hi,

I'm trying to create a stacked faceted barplot with errorbars for percent data. The problem is that the errorbars seem to be off somehow.

Here's what I did: (I post the input data in the comment below.)

library(ggplot2); library(ggthemes); library(plyr); library(dplyr); library(patchwork); library(lemon)

DF$effect <- factor(DF$effect,levels = c("swimming", "metamorphosis", "settlement", "dead"))

DF$treatment = factor(DF$treatment, levels = c('NEG', 'CCA', '10', '20', '30', '40'))

DF$time = factor(DF$time, levels = c('T0','T13.5','T22', 'T31', 'T38.5', 'T55.5', 'T155.5'))

ggplot(data=DF, aes(x=treatment, y=mean, ymax=110, fill=effect)) +
geom_bar(stat="identity", position='stack', width = 0.5)+
labs(x='Treatment', y="% Response (Mean ± SE)", size=12)+
scale_fill_manual(values=c('steelblue2','gray60', 'coral', 'lightpink'))+
facet_rep_wrap(~ time, ncol=2, scales='fixed', repeat.tick.labels = 'top')+
geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=0.1, size=0.1, position='identity')+
scale_y_continuous(expand = c(0,0), limit=c(0, 110)) +
scale_color_manual(values=c('steelblue2','gray60', 'coral', 'lightpink'))+
theme_bw()

And here's the resulting plot with errorbars all over the place:

Then I tried the follwoing to re-arrange the stacked error-bars:

DF1 <- within(DF, mean2 <- ave(mean,time,treatment,FUN=cumsum))


ggplot(data=DF1, aes(x=treatment, y=mean, ymax=110, fill=effect)) +
  geom_bar(stat="identity", position='stack', width = 0.5)+
  labs(x='Treatment', y="% Response (Mean ± SE)", size=12)+ 
  scale_fill_manual(values=c('steelblue2','gray60', 'coral', 'lightpink'))+ 
  facet_rep_wrap(~ time, ncol=2, scales='fixed', repeat.tick.labels = 'top')+ 
  geom_errorbar(aes(ymin=mean2-se, ymax=mean2+se), width=0.1, size=0.1, position='identity')+
  scale_y_continuous(expand = c(0,0), limit=c(0, 110)) + 
  scale_color_manual(values=c('steelblue2','gray60', 'coral', 'lightpink'))+ 
  theme_bw()

And this results in the follwing plot whith errorbars still not where they should be. Does anyone know how to solve this problem?

Cheers,
Ingo

structure(list(time = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("T0", "T13.5", 
"T22", "T31", "T38.5", "T55.5", "T155.5"), class = "factor"), 
    treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 
    6L, 6L, 6L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 
    5L, 5L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L
    ), .Label = c("NEG", "CCA", "10 µg", "20 µg", "30 µg", "40 µg"
    ), class = "factor"), effect = structure(c(1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
    3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L, 1L, 2L, 3L, 4L), .Label = c("swimming", "metamorphosis", 
    "settlement", "dead"), class = "factor"), mean = c(100, 0, 
    0, 0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 100, 0, 0, 
    0, 80, 20, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 100, 0, 0, 0, 
    100, 0, 0, 0, 86.667, 6.667, 6.667, 0, 6.667, 60, 33.333, 
    0, 100, 0, 0, 0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 
    86.667, 6.667, 6.667, 0, 0, 13.333, 86.667, 0, 100, 0, 0, 
    0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 73.333, 6.667, 
    20, 0, 0, 6.667, 93.333, 0, 100, 0, 0, 0, 0, 0, 100, 0, 100, 
    0, 0, 0, 100, 0, 0, 0, 80, 0, 20, 0, 0, 0, 100, 0, 100, 0, 
    0, 0, 0, 0, 100, 0, 86.667, 6.667, 6.667, 0, 33.333, 5.333, 
    61, 0, 46.667, 0, 46.667, 6.667, 0, 0, 100, 0), se = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    11.547, 11.547, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 13.333, 6.667, 6.667, 0, 6.667, 23.094, 17.638, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13.333, 
    6.667, 6.667, 0, 0, 6.667, 6.667, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 17.638, 6.667, 20, 0, 0, 6.667, 
    6.667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    20, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13.333, 
    6.667, 6.667, 0, 33.333, 5.333, 30.892, 0, 24.037, 0, 17.638, 
    6.667, 0, 0, 0, 0)), row.names = 25:168, class = "data.frame")

Could you provide a reproducible example?

Hi, this should work:


DF <- structure(list(time = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                  2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                  4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
                                  6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
                                  6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
                                  7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("T0", "T13.5", 
                                                                                  "T22", "T31", "T38.5", "T55.5", "T155.5"), class = "factor"), 
               treatment = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
                                       3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 
                                       1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
                                       4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                       2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 
                                       6L, 6L, 6L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
                                       4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 
                                       1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 
                                       5L, 5L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
                                       3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L
               ), .Label = c("NEG", "CCA", "10 µg", "20 µg", "30 µg", "40 µg"
               ), class = "factor"), effect = structure(c(1L, 2L, 3L, 4L, 
                                                          1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
                                                          4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
                                                          3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
                                                          2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
                                                          1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
                                                          4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
                                                          3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
                                                          2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
                                                          1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
                                                          4L, 1L, 2L, 3L, 4L), .Label = c("swimming", "metamorphosis", 
                                                                                          "settlement", "dead"), class = "factor"), mean = c(100, 0, 
                                                                                                                                             0, 0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 100, 0, 0, 
                                                                                                                                             0, 80, 20, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 100, 0, 0, 0, 
                                                                                                                                             100, 0, 0, 0, 86.667, 6.667, 6.667, 0, 6.667, 60, 33.333, 
                                                                                                                                             0, 100, 0, 0, 0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 
                                                                                                                                             86.667, 6.667, 6.667, 0, 0, 13.333, 86.667, 0, 100, 0, 0, 
                                                                                                                                             0, 0, 0, 100, 0, 100, 0, 0, 0, 100, 0, 0, 0, 73.333, 6.667, 
                                                                                                                                             20, 0, 0, 6.667, 93.333, 0, 100, 0, 0, 0, 0, 0, 100, 0, 100, 
                                                                                                                                             0, 0, 0, 100, 0, 0, 0, 80, 0, 20, 0, 0, 0, 100, 0, 100, 0, 
                                                                                                                                             0, 0, 0, 0, 100, 0, 86.667, 6.667, 6.667, 0, 33.333, 5.333, 
                                                                                                                                             61, 0, 46.667, 0, 46.667, 6.667, 0, 0, 100, 0), se = c(0, 
                                                                                                                                                                                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                    11.547, 11.547, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                    0, 0, 0, 0, 13.333, 6.667, 6.667, 0, 6.667, 23.094, 17.638, 
                                                                                                                                                                                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13.333, 
                                                                                                                                                                                                    6.667, 6.667, 0, 0, 6.667, 6.667, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 17.638, 6.667, 20, 0, 0, 6.667, 
                                                                                                                                                                                                    6.667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                    20, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13.333, 
                                                                                                                                                                                                    6.667, 6.667, 0, 33.333, 5.333, 30.892, 0, 24.037, 0, 17.638, 
                                                                                                                                                                                                    6.667, 0, 0, 0, 0)), row.names = 25:168, class = "data.frame")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.