Created
July 29, 2018 13:07
-
-
Save chrishanretty/08ebc5453c38393fe1f3749d2bd7b979 to your computer and use it in GitHub Desktop.
How rebelliousness are Kate Hoey and Frank Field?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(reshape2) | |
library(tidyverse) | |
library(hrbrthemes) | |
library(ggridges) | |
most_common <- function(x) { | |
retval <- names(sort(table(x, useNA = "no"), | |
decreasing=TRUE))[1] | |
retval <- ifelse(is.null(retval), | |
NA_character_, | |
retval) | |
} | |
years <- c("1997", "2001", "2005", "2010", | |
"2015", "2017") | |
## You will need first to have downloaded the contents of | |
## https://www.publicwhip.org.uk/data/ | |
holder <- list() | |
for (y in years) { | |
infile <- paste0("votematrix-", y, ".dat") | |
tmp <- read.delim(infile, sep = "\t", quote = "") | |
tmp$X <- NULL | |
tmp <- melt(tmp, | |
id.vars = c("rowid", "date", "voteno", "Bill"), | |
variable.name = "mpid", | |
value.name = "vote") %>% | |
mutate(vote = dplyr::recode(vote, | |
`1` = "Aye", | |
`2` = "Aye", | |
`-9` = NA_character_, | |
`4` = "Nay", | |
`5` = "Nay", | |
`3` = "Abstain", | |
.default = NA_character_), | |
mpid = gsub("mpid", "", mpid)) | |
## Get in the IDs | |
aux <- read.delim(paste0("votematrix-", y, ".txt"), | |
skip = 19, sep = "\t") | |
aux$mpid <- as.character(aux$mpid) | |
old <- nrow(tmp) | |
tmp <- merge(tmp, aux, by = "mpid", all.x = TRUE, all.y = FALSE) | |
new <- nrow(tmp) | |
stopifnot(old == new) | |
## Calculate rebellion rates | |
tmp <- tmp %>% | |
group_by(party, rowid) %>% | |
mutate(party_line = most_common(vote)) %>% | |
ungroup() %>% | |
mutate(rebelled = vote != party_line) %>% | |
group_by(mpid, firstname, surname, party) %>% | |
summarize(rebelliousness = mean(rebelled, na.rm = TRUE)) | |
tmp$session <- y | |
holder[[y]] <- tmp | |
} | |
dat <- bind_rows(holder) | |
plot.df <- dat %>% | |
filter(party == "Lab") | |
frank.field.ids <- c("197", "883", "1410", | |
"40079", "41187", "41397") | |
kate.hoey.ids <- c("282", "966", "1897", | |
"40624", "40828", "41992") | |
highlight.df <- subset(plot.df, mpid %in% c(frank.field.ids, kate.hoey.ids)) | |
p1 <- ggplot(plot.df, aes(x = session, y = rebelliousness, | |
group = interaction(firstname, surname, party))) + | |
geom_line(alpha = 0.1) + | |
geom_line(data = highlight.df, alpha = 1, colour = 'red') + | |
geom_text(data = subset(highlight.df, session == 2017), | |
aes(label = surname), | |
hjust = 0, | |
nudge_x = 0.1) + | |
scale_x_discrete("Parliament") + | |
scale_y_sqrt("Rebelliousness (%)\n(square-root scale)") + | |
labs(title = "Rates of rebellion amongst Labour MPs, 1997-2018", | |
subtitle = "Hoey and Field have been consistently rebellious", | |
caption = "Data: www.publicwhip.org.uk") + | |
theme_ipsum_rc() | |
png(file = "rebrates.png", width = 800, height = 640) | |
p1 | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment