I am trying to make a horizontal grouped barplot with 95% CIs in R. I created a CSV file that contains all the data (see below). My code so far is:
geom_bar(aes(fill = Descriptor), position = "dodge", stat = "identity") +
geom_errorbar(aes(ymax=Upper_CI, ymin=Lower_CI), position= position_dodge(0.9), width=0.25)
ylab("Prevalence (%)") +
ylim(0,100)
When I enter this code on R, I get the result "NULL". Can you please help me? Let me know if you need more information. Thanks!
See above for description.
You don't seem to have a ggplot
call before your layers. If we take a reproducible version of your data frame:
df <- data.frame(Descriptor = c("Not painful", "Not painful", "Not painful",
"Painful", "Painful", "Painful"),
Severity = c("Clear/mild", "Moderate", "Severe", "Clear/mild",
"Moderate", "Severe"),
Value = c(92.6, 77.9, 54.1, 7.4, 22.1, 45.9),
Lower_CI = c(86.2, 68.7, 45.1, 3.9, 15.1, 37.2),
Upper_CI = c(96.1, 84.9, 62.8, 13.8, 31.3, 54.9))
Then the code would be something like this:
library(ggplot2)
ggplot(df, aes(Severity, Value)) +
geom_col(aes(fill = Descriptor), position = position_dodge(0.8),
color = "black", alpha = 0.7, width = 0.6) +
geom_errorbar(aes(ymin = Lower_CI, ymax = Upper_CI, group = Descriptor),
position = position_dodge(0.8), width = 0.2) +
scale_fill_manual(values = c("orange2", "deepskyblue4")) +
theme_minimal(base_size = 20) +
theme(panel.grid.major.x = element_blank()) +
labs(x = NULL, y = "Percentage")