Exercise 21. Two smooth density plots 2
In the previous exercise we made the two density plots, one for each sex, using:
heights %>%
ggplot(aes(height, group = sex)) +
geom_density()
We can also assign groups through the color
or fill
argument. For example, if you type color = sex
ggplot knows you want a different color for each sex. So two densities must be drawn. You can therefore skip the group = sex
mapping.
Using color
has the added benefit that it uses color to distinguish the groups.
This exercise is part of the course
Data Science Visualization - Module 2
Exercise instructions
Change the density plots from the previous exercise to add color.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
## edit the next line to use color instead of group then add a density layer
heights %>%
ggplot(aes(height, group = sex))