---
title: "Data visualization exercise"
format:
  typst:
    mainfont: Georgia
    toc: false
    margin:
      x: 1.25cm
      y: 1.25cm
    number-sections: true
execute:
  message: false
  warning: false
---

Download this file's source ([day-3-viz-ex.qmd](https://princeton-ddss.github.io/r-bootcamp/notes/day-3-viz-ex.qmd)) and open it in RStudio, Positron, or VS Code. Work through the code and associated questions. **_Render as you go to ward off problems as early as possible._**\

**_For your own good, avoid AI tools for this task._** Use only `?function_name`, the [ggplot2 reference](https://ggplot2.tidyverse.org/reference/), and any other R documentation.

## Setup (given) {.unnumbered}

```{r}
#| label: setup

library(tidyverse)

counties <- left_join(
  read_csv("https://princeton-ddss.github.io/r-bootcamp/files/county_data.csv"),
  read_csv("https://princeton-ddss.github.io/r-bootcamp/files/county_population.csv"),
  join_by(GEOID == fips)
)
```

# Part 1: the county data

## Rent and income distributions by group

Plot the distribution of rent and *median household income* by region using `geom_density()` and a `facet_*` function.

```{r}
#| label: task-1

```

## Publication-ready

Save the last plot as a PDF.

```{r}
#| label: task-2

```

# Part 2: mapping America's partisan shift

## Fetching data

1.  Go to the MIT Election Lab's [County Presidential Election Returns 2000–2024](https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/VOQCHQ) on the Harvard Dataverse.
2.  Download `countypres_2000-2024.tab` as a `.csv` or `.tab` file.
3.  Save it in a `data/` folder next to this `.qmd`.
4.  Load the data and inspect it however you wish.
5.  Once you understand how the table is structured, move to the next section.

```{r}
#| label: task-3

```

## Fixing problems

Are all of the data types correct? If so, skip this subsection. If not, fix them.

There are also some bookkeeping rows in a few states in 2024 (`TOTAL VOTES CAST`, `OVERVOTES`, …). They all have an empty `party`. Get rid of them.

```{r}
#| label: task-4

```

## Republican vote share over time

Build a new data frame called `rep_votes` with one row per county per year. Add a column `rep_share` that records the Republican **two party vote share** [meaning, R votes / (R votes + D votes)] for that county--year, then pivot the data wide and add a column `shift` recording the difference between the 2024 and 2012 Republican two-party vote share for each county.

```{r}
#| label: task-5

```

## Visualizing county-level shifts

Plot 2024 share against 2012 share with a reference 45° line (representing no shift). What does a county above the 45° line mean?

```{r}
#| label: task-6

```

## Mapping the shifts

Load the county boundaries from this morning's session and map the county-level shifts. Your color scale should use red and blue properly to indicate partisan shift. Hint: Use one of [these](https://ggplot2.tidyverse.org/reference/scale_gradient.html) functions. (Which one?)

```{r}
#| label: task-7

```

# Part 3: Putting it together

## Republican shifts and county-level income

Join the income data to the shape data (with its `shift` variable). Create a plot showing the relationship between county-level income and the 2012--2024 Republican vote share shift.

```{r}
#| label: task-8
```

## Thinking like a social scientist

How _might_ this be a misleading analysis? Hint: Look at [[Gelman et al. 2010, _Red State, Blue State, Rich State, Poor State_, Chapter 1](https://assets.press.princeton.edu/chapters/s9030.pdf)]{.underline}. How would we examine this relationship properly?

------------------------------------------------------------------------

*Election data: MIT Election Data and Science Lab, "County Presidential Election Returns 2000–2024", [doi:10.7910/DVN/VOQCHQ](https://doi.org/10.7910/DVN/VOQCHQ) (CC0).*
