Exercise 6: More plotting options#
In this homework, you’ll practice more advanced plotting options from ggplot
.
Just like the prior homework, we will be using the gapminder dataset. It contains information about population, life expectancy and per capita GDP by country over time.
1. Multipanel figures (7 points)#
Last week, we visualized the change in life expectancy over time for every country in one plot. An alternative method is to use facet panels to split each country into its own plot.
To start, load the gapminder
dataset from the last set of exercises, as well as the tidyverse
.
# INSERT CODE HERE
Next, create a subset of data that only includes the 25 countries from the Americas
continent. Now we can add a facet_wrap
layer to our line plot to create the panels by country. Additionally, to make the x-axis values easier to read, add a theme
layer to rotate the text by 45 degrees.
# INSERT CODE HERE
Imagine that we wanted to publish this figure in a manuscript. Make the following changes to the code from the previous question to create a cleaner figure:
Change the y-axis label (using
labs
layer)Add a figure title (using
labs
layer)Add a legend (using
labs
layer)Rotate x-axis value text to 90 degrees
Hint: legend titles are set using the same names used in the aes specification. So, we need to add a color
aes mapping to the global plot definition and reference this in the labs
layer to specify the legend name.
# INSERT CODE HERE
Now let’s look at how life expectancy varies by continent over time. Create a multipanel figure where each panel contains five boxplots (one for each continent) and each panel represents one year of data. Include a legend instead of x-axis title and labels.
Hint: the theme
layer allows you to set attributes as element_blank()
.
# INSERT CODE HERE
2. Increasing data density (3 pts)#
The above plots have nice readability, but they probably use more ink than is necessary. Take your boxplots from the last question and edit them to improve the data-to-ink ratio.
Hint: Double check your axis labels!
# INSERT CODE HERE
DUE: 5pm EST, Feb 19, 2024
IMPORTANT Did you collaborate with anyone on this assignment? If so, list their names here.
Someone’s Name