Exercise 3: Test your knowledge

After working through Exercise 3, you’ll…

  • be able to see a graph and recreate it with ggplot2
  • be able to see a problem and customize a plot with ggplot2 to solve it

Task 1

The data set glbwarm comes pre-installed with the processR package that we will be working with soon. The data comprises 815 US individuals (417 females, 398 males) who agreed to engage in online questionnaires. They roughly represent the population of the United States.

Let’s install / activate the processR package first and assign the glbwarm data to a source object.

# installing/loading the package:
if(!require(processR)) {
  install.packages("processR"); 
  require(processR)
} #load / install+load processR

data <- processR::glbwarm

If you can’t install processR (it can be a bit buggy with newer versions of R) then just download the glbwarm.csv from Moodle and don’t use the code above (material folders). Load the data into R by following our tutorial: Import data from your working directory, but remember the difference between read_csv2 (simcolon-separated) and read_csv (comma-separated) and use the appropriate command.

Familiarize yourself with the data set (Hint: use the help() / ? function or visit this Google page of the help function) and then try to reproduce this plot with dplyr and ggplot2. (Hint: You can hide the legend by adding theme(legend.position = "none") to your plot.)

You will need to do some data management with dplyr to create the correct labels for your ggplot2 graph.

Are you stuck and don’t know where to start? Watch this video to help you get started: Click here.

Task 2

Now, try to reproduce this graph. (Hint: You will need to recode the ideology variable in a way that higher values represent stronger attitudes, independent of partisanship.)

Task 3

Can you make a chart that breaks down the relationship between age, negative emotions about climate change, and ideological extremity for the different sexes AND parties?

Hint: You can play around with which variables that you want to use as x, y, and as color / size variables. But if you want to have a recommendation to get you started: x = age, y = negative emotions about climate change, size = ideological extremity. But other combinations are valid, too! For instance, you could decide to map x = ideological extremity and size = age.

When you’re ready to look at the solutions, you can find them here: Solutions for Exercise 3.