Exercises

Download this quarto file with exercises. Solutions are on the Solutions page .

Code
library(ggplot2)
library(dplyr)
library(gapminder)

gap2007 <- gapminder %>% filter(year == 2007)

🎨 Colors

1.1 Take this plot and give it (a) a monochromatic color scheme, (b) a complementary two-color scheme for highlighting Europe vs. all other continents. Use coolors.co or paletton.com to pick the values, and scale_color_manual() to apply them.

Code
ggplot(gap2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10()

1.2 Run colorblindr::cvd_grid() on your version of the plot from 1.1(b). Is your highlight still visible under deuteranopia? If not, fix it without changing the colors (hint: redundant encoding).

1.3 You want to map unemployment rates (0–25%) across German districts. Should the fill scale be sequential, diverging, or qualitative? What if you instead map change in unemployment (-5 to +5 percentage points)? Write down your answer, then create both scales with scale_fill_viridis_c() and scale_fill_gradient2() on any choropleth from the course.

📊 Chart Choice

2.1 For each question, name the most suitable chart type and the aesthetic mappings (x, y, color, size, facet), no code needed:

  1. How is population distributed across countries in 2007?
  2. Did the gap in life expectancy between Africa and Europe narrow between 1952 and 2007?
  3. Which continent has the most within-continent inequality in GDP per capita?
  4. Is there a relationship between population size and life expectancy?

2.2 Now implement two of your answers from 2.1 in ggplot2.

2.3 A colleague presents the market shares of 12 toothpaste brands as a pie chart. Explain, using the Cleveland & McGill results from the Data Visualization page, why this is hard to read and produce a better alternative with fake data (tibble(brand = LETTERS[1:12], share = ...)).

✍️ Exploring Gapminder

3.1 Create a histogram of gdpPercap for 2007. It will look skewed. Fix this with a log scale and explain in one sentence what a reader must now keep in mind.

3.2 Build a line plot of population over time for the five most populous countries of 2007. Replace the legend with direct labels at the end of each line (ggrepel).

3.3 Compute mean life expectancy per continent per year, and plot it as a line chart. Annotate the most striking feature of the plot with annotate().

🧠 Challenge: Explore a Development Pattern

Before seeing any example plots, try to visualize the following:

How does a country’s population size relate to life expectancy and income level?

Your task: Create a visualization using the gapminder dataset for the year 2007 that captures the relationship between:

  • lifeExp: Life expectancy
  • gdpPercap: GDP per capita
  • pop: Population

You may use any type of plot or mapping of variables (e.g. color, size), but aim to show all three variables clearly. Then annotate it so it works without any accompanying text: a title that states the finding, direct labels for at least three notable countries, and a source caption.

🗺️ Bonus: Spatial

5.1 Using rnaturalearth and the 2007 Gapminder data (see Applied Examples), map population instead of GDP. Population is even more skewed than GDP what do you need to change about the fill scale to make the map informative?