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", lib.loc="~/R/x86_64-pc-linux-gnu-library/3.5")
library("ggrepel", lib.loc="~/R/x86_64-pc-linux-gnu-library/3.5")

geom_text_repel

Adds text directly to the plot. Text labels repel away from each other and away from the data points.

str(elections_historic)
## Classes 'tbl_df', 'tbl' and 'data.frame':    49 obs. of  19 variables:
##  $ election      : int  10 11 12 13 14 15 16 17 18 19 ...
##  $ year          : int  1824 1828 1832 1836 1840 1844 1848 1852 1856 1860 ...
##  $ winner        : chr  "John Quincy Adams" "Andrew Jackson" "Andrew Jackson" "Martin Van Buren" ...
##  $ win_party     : chr  "D.-R." "Dem." "Dem." "Dem." ...
##  $ ec_pct        : num  0.322 0.682 0.766 0.578 0.796 ...
##  $ popular_pct   : num  0.309 0.559 0.547 0.508 0.529 ...
##  $ popular_margin: num  -0.1044 0.1225 0.1781 0.142 0.0605 ...
##  $ votes         : int  113142 642806 702735 763291 1275583 1339570 1360235 1605943 1835140 1855993 ...
##  $ margin        : int  -38221 140839 228628 213384 145938 39413 137882 219525 494472 474049 ...
##  $ runner_up     : chr  "Andrew Jackson" "John Quincy Adams" "Henry Clay" "William Henry Harrison" ...
##  $ ru_part       : chr  "D.-R." "N. R." "N. R." "Whig" ...
##  $ turnout_pct   : num  0.269 0.573 0.57 0.565 0.803 0.792 0.728 0.695 0.794 0.818 ...
##  $ winner_lname  : chr  "Adams" "Jackson" "Jackson" "Buren" ...
##  $ winner_label  : chr  "Adams 1824" "Jackson 1828" "Jackson 1832" "Buren 1836" ...
##  $ ru_lname      : chr  "Jackson" "Adams" "Clay" "Harrison" ...
##  $ ru_label      : chr  "Jackson 1824" "Adams 1828" "Clay 1832" "Harrison 1836" ...
##  $ two_term      : logi  FALSE TRUE TRUE FALSE FALSE FALSE ...
##  $ ec_votes      : num  84 178 219 170 234 170 163 254 174 180 ...
##  $ ec_denom      : num  261 261 286 294 294 275 290 296 296 303 ...
elections_historic %>% select(2:7)
## # A tibble: 49 x 6
##     year winner                 win_party ec_pct popular_pct popular_margin
##    <int> <chr>                  <chr>      <dbl>       <dbl>          <dbl>
##  1  1824 John Quincy Adams      D.-R.      0.322       0.309        -0.104 
##  2  1828 Andrew Jackson         Dem.       0.682       0.559         0.122 
##  3  1832 Andrew Jackson         Dem.       0.766       0.547         0.178 
##  4  1836 Martin Van Buren       Dem.       0.578       0.508         0.142 
##  5  1840 William Henry Harrison Whig       0.796       0.529         0.0605
##  6  1844 James Polk             Dem.       0.618       0.495         0.0145
##  7  1848 Zachary Taylor         Whig       0.562       0.473         0.0479
##  8  1852 Franklin Pierce        Dem.       0.858       0.508         0.0695
##  9  1856 James Buchanan         Dem.       0.588       0.453         0.122 
## 10  1860 Abraham Lincoln        Rep.       0.594       0.396         0.101 
## # ... with 39 more rows

Basic Plot

ggplot(elections_historic, aes(x=popular_pct,y=ec_pct)) +
  geom_point()

Basic Plot with Labels Added and Scales Adjusted

p_title <- "Presidential Elections: Popular & Electoral College Margins"
p_subtitle <- "1824-2016"
p_caption <- "Data for 2016 are provisional."
x_label <- "Winner's share of Popular Vote"
y_label <- "Winnter's share of Electoral College Votes"

ggplot(elections_historic, aes(x=popular_pct,y=ec_pct)) +
  geom_point() +
  scale_x_continuous(labels = scales::percent) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = x_label, y = y_label, title = p_title, subtitle = p_subtitle, caption = p_caption)

Include geom_text_repel

Requires aes argument “label = winner_label”

ggplot(elections_historic, aes(x=popular_pct,y=ec_pct, label = winner_label)) +
  geom_point() +
  geom_text_repel() +
  scale_x_continuous(labels = scales::percent) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = x_label, y = y_label, title = p_title, subtitle = p_subtitle, caption = p_caption)

Change point label size and add lines to form quadrants

Change the point label size using the geom_text_repel “size” argument use geom_hline & geom_vline to create lines at the 50% line

ggplot(elections_historic, aes(x=popular_pct,y=ec_pct, label = winner_label)) +
  geom_hline(yintercept = 0.5, size = 1.4, color = "gray80") +
  geom_vline(xintercept = 0.5, size = 1.4, color = "gray80") +
  geom_point() +
  geom_text_repel(size=3) +
  scale_x_continuous(labels = scales::percent) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = x_label, y = y_label, title = p_title, subtitle = p_subtitle, caption = p_caption)

Limit the data

Split the dataset into Democratic & Republican parties

dems <- elections_historic %>% filter(win_party == "Dem.")
p_subtitle <- "Democratic Party, 1824-2016"

ggplot(dems, aes(x=popular_pct,y=ec_pct, label = winner_label)) +
  geom_hline(yintercept = 0.5, size = 1.4, color = "gray80") +
  geom_vline(xintercept = 0.5, size = 1.4, color = "gray80") +
  geom_point(color = "blue") +
  geom_text_repel(size=3) +
  scale_x_continuous(labels = scales::percent) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = x_label, y = y_label, title = p_title, subtitle = p_subtitle, caption = p_caption)

reps <- elections_historic %>% filter(win_party == "Rep.")
p_subtitle <- "Republican Party, 1824-2016"

ggplot(reps, aes(x=popular_pct,y=ec_pct, label = winner_label)) +
  geom_hline(yintercept = 0.5, size = 1.4, color = "gray80") +
  geom_vline(xintercept = 0.5, size = 1.4, color = "gray80") +
  geom_point(color="red") +
  geom_text_repel(size=3) +
  scale_x_continuous(labels = scales::percent) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = x_label, y = y_label, title = p_title, subtitle = p_subtitle, caption = p_caption)

Basic point plot with all winning parties

p_title <- "Presidential Elections: Popular & Electoral College Margins (All)"
ggplot(elections_historic, aes(x=popular_pct,y=ec_pct, color = win_party)) +
  geom_hline(yintercept = 0.5, size = 1.4, color = "gray80") +
  geom_vline(xintercept = 0.5, size = 1.4, color = "gray80") +
  geom_point() +
  scale_x_continuous(labels = scales::percent) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = x_label, y = y_label, title = p_title, subtitle = p_subtitle, caption = p_caption)