I would like to add a stacked bar to a dodged bar chart, showing the total. I don't want the column for the total to be a solid bar, but to consist of the stacked components.
I can add both geom_bars to the plot, but I haven't been able to move the total bar. I could add a dummy category with zero in the middle, but of course I'd prefer the total to be to the right of the components.
geom_bar(position= position_dodge(),stat="identity", alpha = 0.9, width = 0.25) +
geom_bar(position = position_stack(), stat = 'identity', alpha = 0.3, width = 0.125)
You could reshape your dataset and use facets to get the effect, although this will show all of your data and not just the largest value in each
Your dataset would need to be repeated twice, once for the the original
type and once for plotting the totals. You also need a new variable, which I called
df$type2 = df$type df2 = df df2$type2 = "Total"
Stack the two datasets together via
rbind, and then plot using
type2 as the x variable and the alpha variable.
ggplot(rbind(df, df2), aes(y = total, x = type2, fill = type, alpha = type2)) + geom_col(width = .9) + facet_wrap(~treatment, strip.position = "bottom") + scale_alpha_manual(values = c(.9, .9, .3), guide = "none") + theme(strip.background = element_blank(), axis.text.x = element_blank(), axis.ticks.length = unit(0, "mm"), panel.spacing = unit(0, "mm"), panel.grid.major.x = element_blank()) + scale_x_discrete(expand = c(1, 0))