Often, we’ll want to show not only the mean of a value but also its standard error. This tells us how much variation there is around the mean – are most values close to the averages shown, or is there a wide range of values above and below the average?
To add error bars, we need to first calculate our standard errors. Continuing with our
msleep dataset, the code below calculates means and standard errors for hours asleep by diet. We compute new variables
mean.hours representing the mean of
mean.se representing the standard error of our means, and
se.max representing the lower and upper bounds of our error range.
msleep_error_df <- msleep %>% subset(status = "asleep") %>% na.omit() %>% group_by(diet) %>% summarize(mean.hours = mean(hours), mean.se = std.error(hours)) %>% mutate(se.min = mean.hours - mean.se, se.max = mean.hours + mean.se)
We can then create our bar plot showing means and standard errors as follows. We add a
geom_errorbar() layer specifying
ymax variables for our error bar and set its width to
0.2 of the bar’s width. Because we’ve already calculated mean values in our summary data frame, we specify
stat = "identity" to display the
mean.hours values as is.
msleep_sebar <- ggplot(msleep_error_df, aes(x = diet, y = mean.hours)) + geom_bar(stat = "identity") + geom_errorbar(aes(ymin = se.min, ymax = se.max), width = 0.2) + labs(title = "Mean Hours Asleep by Diet")
This produces the following plot, which should look familiar from the last exercise! Now that we have error bars, we can see how much variation there is around the mean hours asleep for each diet group.
We now want to add error bars to our plot of mean graduation rates by year. We’ve created summary data frame called
graduation_error_df containing means and standard errors for graduation rates by year. Examine
graduation_error_df using the
Create a bar plot named
Year to the
x axis and
Mean.Pct to the
y axis, using our newly created
graduation_error_df data frame. First, add a
geom_bar() layer with the appropriate
stat. Second, add a
geom_errorbar() layer mapping
ymax with a width of
graduation_sebar to see what it looks like.