1. Additional aesthetics
You've learned how to create a scatter plot to compare
2. Scatter plots
two variables within your data using two visual aesthetics: GDP per capita on the x-axis, and life expectancy on the y-axis. But
3. Additional variables
these aren't the only variables in the gapminder dataset: for example, you also have continent and population. You may want to examine relationships among all these variables in the same plot.
You already used the x-axis to represent GDP per capita and the y-axis to represent life expectancy. Now you'll learn to add two more aesthetics- color and size- to communicate even more information in your scatter plot.
Continent is a categorical variable: it has a few specific values, such as Asia and Europe. A good way to represent a categorical variable in a scatterplot
4. The color aesthetic
is the color of your points, like you see here. To use this aesthetic, you add color equals continent inside the aes, next to x equals gdpPercap and y equals life expectancy. The code is otherwise the same, including the plus geom_point and the + scale x log10. Notice that that ggplot2 automatically adds a legend to the plot, indicating which color represents which continent.
This communicates a lot about differences between continents. The average life expectancy and GDP per capita tends to be lowest for African countries, shown in red, and highest for European countries, shown in blue. Another variable you may want to include in the graph is population, represented by the pop variable in the dataset. This is a numeric variable, so a good way to represent it is with the size
5. The size aesthestic
of the points in the scatterplot, with higher population countries getting larger points.
Just like x, y, and color, you add size = pop within the aes parentheses.
Note that to keep the length of each of the code lines reasonable, we put the size aesthetic on a second line, but this doesn't make any difference, and you don't have to do that in the exercises.
6. Aesthetics
You've now learned to use four aesthetics in a plot: x, y, color, and size: to communicate information about four variables in your dataset. In the exercises,
7. Let's practice!
you'll learn to mix and match aesthetics and variables to further explore the statistical state of the world within one year.