In case you need it, here’s the data loading routine again:

library(dplyr)
library(haven)

gp_covid <- 
  read_sav(
    "./data/ZA5667_v1-1-0.sav"
  ) %>% 
  sjlabelled::set_na(na = c(-1:-99, 97:98))

As we have discussed in the session on Exploratory Data Analysis, data exploration is not only about creating numbers and summary statistics. Sometimes a good plot can reveal more insights than a whole data frame filled with numbers (especially to the human eye). In this exercise, we use what we’ve just learned about plots with ggplot2. We will also repeat some of the content from the sessions on data wrangling in the following exercises (as this is typically part of a pipeline for data visualization).

This time we are going to use the Gapminder data on GDP per capita again. Hence, we we need to first load the Gapminder GDP data from the CSV file and convert it to long format.

library(dplyr)
library(tidyr)

gapminder_ggplot_input <-
  readr::read_csv("./data/gdppercapita_us_inflation_adjusted.csv") %>%
  pivot_longer(-country, names_to = "year", values_to = "GDP") %>% 
  filter(!is.na(GDP)) %>%
  arrange(year, GDP) %>%
  group_by(year) %>%
  summarise(GDP_over_all_countries = mean(GDP)) %>% 
  ungroup()
## 
## -- Column specification ---------------------------------------------------------------------------------------------------------------
## cols(
##   .default = col_double(),
##   country = col_character()
## )
## i Use `spec()` for the full column specifications.

Our aim is to analyze how the GDP has developed over time. The nice thing about plots is that we can use the whole range of years and still identify differences between various periods. Our plot of choice for this is a line plot to visualize the data as a time series.

1

Using ggplot2, plot the Gapminder GDP per capita data as a line plot to display a time series. One important note here: In the aesthetics definition for this plot you should define a grouping variable group = 1. Otherwise, ggplot assumes that you want to plot one line for each year.
The name of the geom we need is geom_line.

Admittedly, this may not be the best approach to identify differences between the periods directly. We don’t know when our periods start and when they end. Luckily, this can be done in two relatively straightforward steps. Let’s start with the first one: using different colors for different periods. For this purpose, we need an indicator variable as a grouping variable to use different colors for the line at each period.

2

Create an indicator variable for the periods 1960-1969, 2002-2018 and the time in between.
A combination of mutate() and case_when() lets you create the new variables we need. To get some sensible legend labels later, you should specify the indicator variables as strings.

After we’re set up with our indicator variable, it’s plotting time again. We can simply reuse our code from before and define a grouping color in the aesthetics definition.

3

Plot the line plot once again, but this time with different colors for the different periods.
In the aesthetics definition aes(), you can choose the option color = indicator_variable to define the grouping.

Now we can see some visual differences between the different periods. One last thing, however, is that there are way too many labels on the x-axis. Maybe a more sensible axis labeling approach would be to create axis breaks for ten-year-steps. NB: The next one is an advanced exercise as we did not talk about manipulating axes before. If you’re not feeling adventurous you can just skip this one.

Bonus

Create some prettier, i.e., more sensible breaks for the x-axis.
You can modify the x-axis with scale_x_discrete() and its breaks with the option breaks = breaks_vector. You can check the help file (?scale_discrete) for some more information. A helpful additional function to use here is seq() from base R.