Created
May 12, 2019 20:08
-
-
Save leemacaulay/644831251a06c230363496f2a861d6f0 to your computer and use it in GitHub Desktop.
Who asks what - using R to visualise Commons questions
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(hansard) | |
library(mnis) | |
library(tidyr) | |
library(dplyr) | |
library(lubridate) | |
library(stringr) | |
library(ggplot2) | |
library(bbplot) | |
library(RColorBrewer) | |
library(forcats) | |
cumbria <- mnis_all_members(house = "commons") %>% | |
filter(member_from %in% c("Carlisle", "Workington", "Penrith and The Border", | |
"Westmorland and Lonsdale", "Barrow and Furness", "Copeland")) %>% | |
filter(current_status_is_active == "True") | |
questions <- mp_questions(cumbria$member_id) | |
qs <- questions %>% | |
mutate(year = year(answer_date_value)) %>% | |
mutate(tabling_member = str_remove(questions$tabling_member_label_value, "Biography information for ")) | |
unique(dep_qs$answering_body) # Figure out how many bodies we need to categorise. | |
health <- c("Department of Health", "Department of Health and Social Care") | |
transport <- c("Department for Transport") | |
localgov <- c("Department for Communities and Local Government", "Scotland Office", | |
"Deputy Prime Minister", "Northern Ireland Office", "Wales Office", | |
"Ministry of Housing, Communities and Local Government") | |
finance <- c("HM Treasury", "Treasury") | |
law <- c("Attorney General", "Ministry of Justice") | |
business <- c("Department for Business, Innovation and Skills", | |
"Department for Energy and Climate Change", | |
"Department for Business, Energy and Industrial Strategy") | |
environment <- c("Department for Environment, Food and Rural Affairs") | |
government <- c("Prime Minister", "Cabinet Office", "House of Commons Commission", | |
"Leader of the House", "Church Commissioners", | |
"Speaker's Committee on the Electoral Commission") | |
education <- c("Department for Education") | |
home <- c("Home Office", "Women and Equalities") | |
foreign <- c("Foreign and Commonwealth Office", "Department for International Development") | |
brexit <- c("Department for Exiting the European Union", | |
"Department for Exiting the European Union ", "Department for International Trade") | |
culture <- c("Department for Culture Media and Sport", | |
"Department for Culture, Media and Sport", | |
"Department for Digital, Culture, Media and Sport") | |
welfare <- c("Department for Work and Pensions") | |
defence <- c("Ministry of Defence") | |
dep_qs <- qs %>% | |
mutate(year = as.factor(year)) %>% | |
mutate(topic = case_when( | |
answering_body %in% brexit ~ "brexit", | |
answering_body %in% business ~ "business", | |
answering_body %in% culture ~ "culture", | |
answering_body %in% defence ~ "defence", | |
answering_body %in% education ~ "education", | |
answering_body %in% environment ~ "environment", | |
answering_body %in% finance ~ "finance", | |
answering_body %in% foreign ~ "foreign", | |
answering_body %in% government ~ "government", | |
answering_body %in% health ~ "health", | |
answering_body %in% home ~ "home", | |
answering_body %in% law ~ "law", | |
answering_body %in% localgov ~ "localgov", | |
answering_body %in% transport ~ "transport", | |
answering_body %in% welfare ~ "welfare", | |
TRUE ~ "other" | |
)) %>% | |
group_by(tabling_member, year, topic) %>% | |
summarise(total = sum(n())) | |
linechart <- ggplot(pct_qs, aes(x = year, y = total, colour = tabling_member)) + | |
geom_line(size = 1) + | |
geom_hline(yintercept = 0, size = 1, colour="#333333") + | |
bbc_style() | |
# Define the number of colors you want | |
nb.cols <- 16 | |
mycolors <- colorRampPalette(brewer.pal(12, "Paired"))(nb.cols) | |
barchart <- ggplot(dep_qs, aes(x = fct_rev(year), y = total, fill = topic)) + | |
geom_bar(stat = "identity", position = "fill") + | |
bbc_style() + | |
scale_y_continuous(labels = NULL) + | |
scale_fill_manual(values = mycolors, labels = function(x) paste0(" ", x, " ")) + | |
geom_hline(yintercept = 0, size = 1, colour = "#333333") + | |
coord_flip() + | |
facet_wrap(tabling_member ~ .) | |
finalise_plot(plot_name = barchart, | |
source = "Source: UK Parliament", | |
save_filepath = "output_data/whoaskswhat.png", | |
width_pixels = 640, | |
height_pixels = 550) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment