I have a dataset that I want to plot the mean and confidence limits of in a dynamite plot (I am aware of the drawbacks of dynamite plots but I work in a corporate environment where simple summary statistics are often more useful than representing the distribution.) I have calculated the summary statistics prior to plotting as such:
library(gmodels)
lci <- function(data) {
as.numeric(ci(data)[2])
}
uci <- function(data) {
as.numeric(ci(data)[3])
}
ID <- seq(1:140)
avg_outcome <- c(rnorm(100, 100, 20), rnorm(40, 60, 30))
avg_outcome[avg_outcome<0] <- 0
group <- c( rep("Red", 100), rep("Blue", 40) )
area <- c(rep("North", 50),rep("South", 10), rep("West", 15), rep("East", 25), rep("North", 15), rep("South", 5), rep("East", 20))
reprex <- data.frame(ID, avg_outcome, group, area)
reprex_avg <- reprex %>%
group_by(group, area) %>%
summarise_if(is.numeric, funs(mean, lci, uci))
But I can't work out how to get the error bars to successfully stack in the way the bars are:
ggplot(reprex_avg, aes(x=area, fill=group, y=avg_outcome_mean, ymin=avg_outcome_lci, ymax=avg_outcome_uci)) +
geom_col(color="black") +
geom_errorbar()
gives:
I tried explicitly setting the position e.g.
ggplot(reprex_avg, aes(x=area, fill=group, y=avg_outcome_mean, ymin=avg_outcome_lci, ymax=avg_outcome_uci)) +
geom_col(color="black", position=position_stack(0.5)) +
#geom_errorbar() +
#geom_errorbar(aes(group=group)) + # this does nothing
#geom_errorbar(aes(group=group, linetype=group)) + # different linetypes but still not stacked
geom_errorbar(aes(group=group, linetype=group), position=position_stack(0.5)) +
geom_hline(yintercept=0) +
theme_bw()
which gives:
along with the warning message:
Warning message:
Stacking not well defined when not anchored on the axis
Which I don't really understand as I have set aesthetics for both axes.
I know I could get around this by using a dodged dynamite plot or by faceting by group but in this case I would much rather stack, because there are a lot of different areas in my real dataset so dodging would look confusing and also because the smaller group is only present for a small subset of them, so faceting would result in a lot of unused space.
Does anyone know how I can stack the error bars successfully?