stacked barplot with errorbars -> errorbars are off

79 views
Skip to first unread message

Ingo Miller

unread,
Nov 10, 2021, 10:21:06 AM11/10/21
to ggplot2
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:

7b8394a1-2451-4a03-ae80-2c5c25c81c59.png

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 
1bcca0f1-b896-437b-adbe-2775b89a4c26.png


Ingo Miller

unread,
Nov 10, 2021, 10:21:41 AM11/10/21
to ggplot2
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,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L, 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")

Robert Winkelman

unread,
Oct 14, 2022, 7:19:37 PM10/14/22
to ggplot2
I think all the NA's are the cause of the problem. They result in multiple occurrences of what should be a unique row.

Crude patch:
> ggplot(data=DF %>% filter(!is.na(treatment)), ...

The source of the problem in the data:
> DF %>% group_by(time, treatment, effect) %>% mutate(counter = n()) %>% arrange(-counter)

# A tibble: 144 x 6
# Groups:   time, treatment, effect [72]
   time  treatment effect         mean    se counter
   <fct> <fct>     <fct>         <dbl> <dbl>   <int>
 1 T13.5 NA        swimming        100    10       4
 2 T13.5 NA        metamorphosis     0    10       4
 3 T13.5 NA        settlement        0    10       4
 4 T13.5 NA        dead              0    10       4
 5 T13.5 NA        swimming        100    10       4
 6 T13.5 NA        metamorphosis     0    10       4
 7 T13.5 NA        settlement        0    10       4

Reply all
Reply to author
Forward
0 new messages