Adding Error Bars to Line Graph

Hey guys,

Really hoping you can help. I have a data set with dengue incidence over time for all 33 departments of Colombia. I have created a simple line graph showing the mean annual incidence for all departments (2007-2018) using a data frame that has only 12 observations and 2 variables (incidence and year).

However I want to add error bars to this graph based on the annual incidence of each department, so drawing from a different data frame. Is this possible?

The code i'm using does not come up with any errors but doesn't do anything either:

ggplot(data=mean_all_dept,aes(x=Year, y=log10(CI100k)))+
geom_line(color="purple")+
geom_point()+
labs(title = "Mean annual dengue incidence for 33 departments")+
labs(y="Cumulative Incidence (per 100,000)", x="Year")

##line plot of CI for all departments against time with error bars
library(ggplot2)
df <- mean_all_dept
df$Year <- as.factor(df$Year)
head(df)

mean_summary <- function(meanz, CI100k, Year, Dept) {
require(plyr)
summary_func <- function(x, col) {
c(mean = mean(x[[col]], na.rm=TRUE),
SD = sd(x[[col]], na.rm=TRUE))
}
data_sum <- ddply(meanz, Year, Dept, .fun=summary_func,
CI100k)
data_sum <- rename(data_sum, c("Mean" = CI100k))
return(data_sum)
}

##Summarise the data
df3 <- mean_summary(meanz, varname="CI100k",
groupnames=c("Dept", "Year"))
head(df3)

p <- ggplot(df3, aes(x=Year, y=log10(CI100k)))+
geom_line(color="purple")+
geom_point+
geom_errorbar(aes(ymin=CI100k-sd, ymax=CI100k+sd), width=.2,
position=position_dodge(0.05))
p+labs(title = "Mean annual dengue incidence for 33 departments", y="Cumulative Incidence (per 100,000)", x="Year")

Thank you so much for any help!!

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.