I want to add error bars to all the bars in my waterfall plots.
Here is a sample data and the code I am using to create the plots:
library(dplyr)
library(ggplot2)
set.seed(100)
df<-tibble(Year= c(2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003), Vals = abs(rnorm(12)*5), Affil = as.factor(c(1,2,1,2,1,2,1,2,1,2,1,2)), SD=rnorm(12, 1, 0.5))
df[c(T, T, F, F),]$Vals <- -(df[c(T, T, F, F),]$Vals)
p <- ggplot(data = df, aes(Year,Vals)) +
geom_bar(stat = "identity", aes(fill=Affil), position=position_dodge()) +
geom_errorbar(aes(ymin=Vals-SD, ymax=Vals+SD), width=.2, position=position_dodge(.9))
p + theme_bw(base_size = 16) + geom_hline(aes(yintercept=0), size=1.25)
For each year, there should be 2 pairs of error bars. Two for each Affil
and 2 for the positive and negative values associated with each Affil
each year. I have tried inline filtering suing dplyr
to add the error bars step by step but the solution is getting cumbersome and not working.