Skip to content

Instantly share code, notes, and snippets.

@zmeers
Created July 12, 2018 22:14
Show Gist options
  • Save zmeers/019c40533e60a704ffaba5b38deba545 to your computer and use it in GitHub Desktop.
Save zmeers/019c40533e60a704ffaba5b38deba545 to your computer and use it in GitHub Desktop.
ggparliament example
# ggparliament example
# Zoe Meers
# 12 July 2018
#
library(tidyverse)
library(ggparliament) # download from github using devtools.
# predictions from NY Times -- see https://www.nytimes.com/interactive/2018/03/26/us/elections/house-races-midterms.html
# Regarding order -- from the POV of the speaker, Republicans sit to the left of the chamber and Democrats to the right.
# This is pretty counterintuitive for a lot of people.
# I'm not sure whether to change to Democrats on the left and Republicans to the right...
midterm_seats <- c(172, 26, 16, 22, 10, 7, 182)
midterm_seats_labels <- c("Solid Rep", "Likely Rep", "Lean Rep", "Tossup","Leaning Dem", "Likely Dem", "Solid Dem")
midterm_seats_colours <- c("#D75C5C", "#E49292", "#F4D0D1","#F3C654", "#AFD6F5", "#7BB0DC", "#3989CB")
congress_16_18_pred <- ggparliament::election_data %>%
filter(country == "USA" &
house == "Representatives" &
year == "2016") %>%
ggparliament::parliament_data(election_data=.,
total_seats = sum(.$seats),
party_names = .$party_long,
parl_rows = 8,
type = 'semicircle') %>%
mutate(pred_midterms = rep(midterm_seats_labels, midterm_seats)) %>%
mutate(pred_colours = rep(midterm_seats_colours, midterm_seats))
ggplot(data = congress_16_18_pred, aes(x, y)) +
ggparliament::draw_majoritythreshold(n = 218,
type = 'semicircle') +
ggparliament::geom_parliament_seats(aes(colour = pred_midterms), size = 15) +
ggparliament::geom_highlight_government(government == 1) +
scale_colour_manual(values = congress_16_18_pred$pred_colours,
limits = congress_16_18_pred$pred_midterms) +
labs(colour = NULL,
title = "United States Congress",
subtitle = "2018 Midterms Predictions",
caption = "Note: Current GOP seats are circled in black.
Source: To Reclaim the House, Democrats Need to Flip 24 G.O.P. Seats.
25 Are in Clinton Territory.
https://www.nytimes.com/interactive/2018/03/26/us/elections/house-races-midterms.html") +
theme_void() +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
text = element_text(family = "universLight"))
#ggsave(last_plot(), file = "~/predicted_midterms.png", width = 6, height = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment