Instead of a histogram, we are going to make a smooth density plot. In this case we will not make an object, but instead render the plot with one line of code. Change the geometry in the code previously used to make a smooth density instead of a histogram.
1. Now we are going to make a density plot for males and females
separately. We can do this using the group
argument in aes
. We assign groups
via the global aesthetic mapping as each point needs to a group before making
the calculations needed to estimate a density. Store the resulting ggplot object
in p_group
2. We can also assign groups through the color
argument. This distinguishes the groups by changing the color of the density outline. Change the
code above to use color. Store the resulting ggplot object in p_color
.
3. We can also assign groups through the fill
argument. This distinguishes the groups by filling the geometry with different colors, like this:
heights %>%
ggplot(aes(height, fill = sex)) +
geom_density()
However, here the second density is drawn over the other. We can make
the curves more visible by using alpha blending to add transparency. Set
the alpha parameter to 0.2 in the geom_density
function to make this
change. Store the resulting ggplot object in p_fill
.