Problem set: data visualization

Overview

Inside your camp project, write a script called code/01_set.R that answers the questions below. It is your script: for any line in it, you should be able to say what it does.

For all of your questions, do the following:

  • Make the graphs look good (add labels, a theme1)
  • Make your code look good (comment it, write it in chunks)
  • Save each of your plots in figures/ using ggsave() and here()
  • Make sure the script runs top to bottom, from a fresh R session, and generates everything you want

Nothing is turned in. Your figures/ folder is the record of what you did.

Questions

You’ll need the following packages. If socviz is not installed yet, run install.packages("socviz") once in the Console first.

library(tidyverse)
library(socviz)
library(juanr)
library(here)
  1. Copy the code snippet below to create the data object trade_2010. Use trade_2010 to make a scatterplot of imports (x-axis), exports (y-axis) (note: you can see the code book with ?trade). Add labels and a theme to clean it up. What trends to do you observe? And what is that outlier country? Add labels to the plot to identify it.
trade_2010 = trade |> 
  filter(year == 2010)
  1. Copy the code snippet below to create the data object country_agree, which shows you the average Big Five Agreeable score by country. Make a barplot of the average score across countries. Reorder the plot so the countries appear in descending “agreeableness” order (we did not cover reordering in class: look up fct_reorder(), or ask the agent and read what it gives you). What stands out? What happens if you switch geometries to points? Which is more effective?
country_agree = big_five |> 
  group_by(country) |> 
  summarise(agree = mean(agreeable_score, na.rm = TRUE), n = n()) |> 
  filter(n >= 150) |> 
  drop_na()
  1. Copy the code snippet below to create the data object childcare_four, which shows you childcare costs over time in four states (note: you can see the code book with ?childcare). Make a histogram of weekly childcare prices for infants and take a look. Next, map the state to the fill aesthetic and take a look. Finally, switch out your histogram geometry for geom_density() (new to you; run ?geom_density). Which looks better? What’s the pattern?
childcare_four = childcare |> 
  filter(state == "Mississippi" | state == "Virginia" | state == "California" | state == "Hawaii")
  1. Using oecd_sum, make a lined time series of the difference in life expectancy between the USA and other OECD countries over time and take a look (note: you can see the code book with ?oecd_sum). Color the lines by whether the USA is above or below the OECD average. Now switch your line to a column geometry. Which looks better? Why? And what’s the story?

  2. Using elections (note: each row is a county), use a boxplot to show the distribution of household income across regions (note: you can see the code book with ?elections). We did not cover boxplots in class: run ?geom_boxplot or ask the agent, then make sure you can explain every line it gives you. What is the general pattern? Say something about the outliers.

  3. Using any other dataset (e.g., see the ones in juanr) make the “nicest” plot that you can. Experiment with geometries and layers we did not cover in class. This is a good place to ask the agent for ideas, then read what it gives you.

Footnotes

  1. look up how to do this, or ask the agent↩︎