Exercises – ggplot2

Author
Affiliation

Jürgen Wilbert

University of Münster

Published

January 29, 2026

Task 1

  • Using the dataset mpg, create a scatterplot with
  • displ on the x-axis and
  • hwy on the y-axis.
  • class should not be mapped to color.

Task 2

  • Using mpg, create a scatterplot of displ vs. hwy, and facet the plot by drv.

Task 3

  • Using mtcars, create a boxplot showing mileage (mpg) by number of cylinders (cyl, treated as a factor).

Task 4

  • Using mtcars, create a scatterplot of weight (wt) vs. mileage (mpg) and add a smoothing line.

Task 5

  • Using starwars, create a bar chart showing the number of characters per species.
  • Exclude missing values of species.

Task 6

  • Using gapminder, create a scatterplot for the year 2007 with
  • fertility on the x-axis,
  • life_expectancy on the y-axis,
  • continent mapped to color,
  • population mapped to point size.

Task 7

  • Using starwars, create a scatterplot with height on the x-axis, mass on the y-axis, and map gender to color.
  • Ignore observations with missing height or mass.

Task 8

  • Using gapminder, create a line plot showing life_expectancy over year for Germany only.

Task 9

  • Using gapminder, create a line plot showing life_expectancy over year for all countries in Europe.
  • Each country should be a separate line.

Task 10

  • Using gapminder, create a scatterplot using the year 2007 only with gdp on the x-axis (use a log10 scale by adding the layer scale_x_log10()), life_expectancy on the y-axis, population mapped to point size, continent mapped to color.
  • Exclude rows with missing gdp, life_expectancy, or population.