Home > database >  How to make a stacked bar chart with overall error bar?
How to make a stacked bar chart with overall error bar?

Time:10-10

I'd like to make a bar chart similar to this one, from enter image description here enter image description here

Many thanks for any leads to help fixing my code.

CodePudding user response:

The problem is that geom_bar is constructing the histogram for you, and thus doesn't have access to all of the other columns in your dataframe. Here I use geom_col to construct the histogram myself, including calculating the total count and standard deviation for each group.

Another option (df2, second plot below) is to replace the mutate with a summarize, and pass that as a separate dataframe to geom_errorbar (and keep your geom_bar). I would prefer this, as it only draws the error bars once per bar, instead of overdrawing them for each color.

library(dplyr, warn.conflicts = FALSE)
library(ggplot2)

df <- diamonds %>% group_by(clarity, color) %>% 
    summarize(count = n()) %>% 
    group_by(clarity) %>% 
    mutate(sd = sqrt(var(count)), count_total = sum(count)) %>% 
    ungroup()
#> `summarise()` has grouped output by 'clarity'. You can override using the `.groups` argument.

ggplot(df, aes(clarity, y=count, fill=color, ymin=count_total-sd, ymax = count_total sd))    
    geom_col() 
    geom_errorbar()

                    

df2 <- diamonds %>% group_by(clarity, color) %>% 
    summarize(count = n()) %>% 
    group_by(clarity) %>% 
    summarize(sd = sqrt(var(count)), count_total = sum(count)) %>% 
    ungroup()           
#> `summarise()` has grouped output by 'clarity'. You can override using the `.groups` argument.

ggplot(diamonds, aes(clarity, fill=color))    
    geom_bar() 
    geom_errorbar(data = df2, aes(clarity, ymin=count_total-sd, ymax = count_total sd), inherit.aes = FALSE)

Created on 2021-10-10 by the reprex package (v2.0.1)

  • Related