Bar plots: Using aggregated data
If it is appropriate to use bar plots (see the video!), then it nice to give an impression of the number of values in each group.
stat_summary()
doesn't keep track of the count. stat_sum()
does (that's the whole point), but it's difficult to access. It's more straightforward to calculate exactly what we want to plot ourselves.
Here, we've created a summary data frame called mtcars_by_cyl
which contains the average (mean_wt
), standard deviations (sd_wt
) and count (n_wt
) of car weights, for each cylinder group, cyl
. It also contains the proportion (prop
) of each cylinder represented in the entire dataset. Use the console to familiarize yourself with the mtcars_by_cyl
data frame.
This exercise is part of the course
Intermediate Data Visualization with ggplot2
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Using mtcars_cyl, plot mean_wt vs. cyl
___ +
# Add a bar layer with identity stat, filled skyblue
___