Healy Chapter 7 Part 1

Harold Nelson

10/29/2018

library(tidyverse)
## ── Attaching packages ── tidyverse 1.2.1 ──
## ✔ ggplot2 3.0.0     ✔ purrr   0.2.5
## ✔ tibble  1.4.2     ✔ dplyr   0.7.6
## ✔ tidyr   0.8.1     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ───── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(socviz)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggmap)
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind
library(maps)
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map

Look at the election dataframe from socviz.

glimpse(election)
## Observations: 51
## Variables: 22
## $ state        <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "Cali...
## $ st           <chr> "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "...
## $ fips         <dbl> 1, 2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, ...
## $ total_vote   <dbl> 2123372, 318608, 2604657, 1130635, 14237884, 2780...
## $ vote_margin  <dbl> 588708, 46933, 91234, 304378, 4269978, 136386, 22...
## $ winner       <chr> "Trump", "Trump", "Trump", "Trump", "Clinton", "C...
## $ party        <chr> "Republican", "Republican", "Republican", "Republ...
## $ pct_margin   <dbl> 27.73, 14.73, 3.50, 26.92, 29.99, 4.91, 13.64, 11...
## $ r_points     <dbl> 27.72, 14.73, 3.50, 26.92, -29.99, -4.91, -13.64,...
## $ d_points     <dbl> -27.72, -14.73, -3.50, -26.92, 29.99, 4.91, 13.64...
## $ pct_clinton  <dbl> 34.36, 36.55, 44.58, 33.65, 61.48, 48.16, 54.57, ...
## $ pct_trump    <dbl> 62.08, 51.28, 48.08, 60.57, 31.49, 43.25, 40.93, ...
## $ pct_johnson  <dbl> 2.09, 5.88, 4.08, 2.64, 3.36, 5.18, 2.96, 3.33, 1...
## $ pct_other    <dbl> 1.46, 6.29, 3.25, 3.13, 3.66, 3.41, 1.55, 1.88, 3...
## $ clinton_vote <dbl> 729547, 116454, 1161167, 380494, 8753788, 1338870...
## $ trump_vote   <dbl> 1318255, 163387, 1252401, 684872, 4483810, 120248...
## $ johnson_vote <dbl> 44467, 18725, 106327, 29829, 478500, 144121, 4867...
## $ other_vote   <dbl> 31103, 20042, 84762, 35440, 521786, 94772, 25457,...
## $ ev_dem       <dbl> 9, 3, 11, 6, 0, 0, 0, 0, 0, 29, 16, 0, 4, 0, 11, ...
## $ ev_rep       <dbl> 0, 0, 0, 0, 55, 9, 7, 3, 3, 0, 0, 3, 0, 20, 0, 0,...
## $ ev_oth       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0...
## $ census       <chr> "South", "West", "West", "South", "West", "West",...

Exercise

Look at Healy’s graphic using facet_wrap and separate dotplots for each region. Fiddle with fig.height and fig.width to get something you’re pleased with.

Answer

party_colors <- c("#2E74C0", "#CB454A") 

p0 <- ggplot(data = subset(election, st %nin% "DC"),
             mapping = aes(x = r_points,
                           y = reorder(state, r_points),
                           color = party))

p1 <- p0 + geom_vline(xintercept = 0, color = "gray30") +
    geom_point(size = 2)

p2 <- p1 + scale_color_manual(values = party_colors)

p3 <- p2 + scale_x_continuous(breaks = c(-30, -20, -10, 0, 10, 20, 30, 40),
                              labels = c("30\n (Clinton)", "20", "10", "0",
                                         "10", "20", "30", "40\n(Trump)"))

p3 + facet_wrap(~ census, ncol=1, scales="free_y") +
    guides(color=FALSE) + labs(x = "Point Margin", y = "") +
    theme(axis.text=element_text(size=8))

Exercise

The first ggplot object won’t show anything since it has no geom. What does p1 add?

Answer

p1

Exercise

What does p2 add?

Answer

p2