I am trying to plot a bar chart with multiple error bars that align with the relevant values for each factor level on the x axis.
I have followed instructions as per - Grouped barplot in R with error bars - but I cannot figure out why my script isn't cooperating.
Here is the result I get, where obviously I want the errorbars to align with the relevant data.
Here is my data:
structure(list(key = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("2030", "2050", "2100"
), class = "factor"), value = c(0.4, 0.4, 0.1, 0.1, 0.1, 3.4,
4.5, 6.8, 3.6, 3, 5.7, 12.4, 14.9, 9.5, 10.8), scenario = structure(c(4L,
2L, 1L, 6L, 5L, 4L, 2L, 1L, 6L, 5L, 4L, 2L, 1L, 6L, 5L), .Label = c("1.5°C-high-OS",
"1.5°C-limited-OS", "1.5°C-low-OS", "Below-1.5°C", "Higher-2°C",
"Lower-2°C"), class = "factor"), Lower.Quartile = c(0, 0, 0,
0, 0, 0, 3.4, 3.7, 1.8, 1.6, 0, 6.4, 12.1, 6.9, 8.2), Upper.Quartile = c(1.1,
1, 0.4, 0.3, 0.2, 8.3, 6.3, 9.5, 4.6, 4.9, 13.4, 15, 16.3, 12.1,
15.3)), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 11L, 12L,
13L, 15L, 16L, 17L, 18L), class = "data.frame")
Here is my code:
library(ggplot2)
library(tidyr)
ggplot(Global.BECCS.mean, aes(x = key)) + geom_bar(aes(fill = scenario, y = value), position = position_dodge(), stat='identity', col = "black") + theme_classic() +
xlab('Year') + ylab(expression(paste(GtC,' sequestered by BECCS per year'))) + geom_hline(yintercept = 5.5, linetype = 'dashed') + geom_hline(yintercept = 16, linetype = 'dotted') +
geom_errorbar(aes(ymin = Global.BECCS.mean$Lower.Quartile, ymax = Global.BECCS.mean$Upper.Quartile), width = 0.1, position = position_dodge())