Introduction to parlitools

Evan Odell

2017-05-03

Creating a hexagonal map of constituencies

Create a hexagonal map of all constituency MPs, at the time Parliament was dissolved for the 2017 General Election.

Use the code below to create the hexagonal map of current (as of 19 April 2017) UK constituency MPs.

library(leaflet)
library(sf)
library(dplyr)
library(hansard)
library(mnis)
library(parlitools)

west_hex_map <- parlitools::west_hex_map

party_colour <- parlitools::party_colour

mps <- mps_on_date("2017-04-19")

mps_colours <- left_join(mps, party_colour, by = "party_id") #Join to current MP data
## Warning in left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y): joining
## factor and character vector, coercing into character vector
west_hex_map <- left_join(west_hex_map, mps_colours, by = "gss_code") #Join colours to hexagon map

# Creating map labels
labels <- paste0(
  "<strong>", west_hex_map$constituency_name, "</strong>", "</br>",
  west_hex_map$party_name, "</br>",
  west_hex_map$display_as, "</br>",
  "2015 Result: ", west_hex_map$result_of_election, "</br>",
  "2015 Majority: ", west_hex_map$majority
) %>% lapply(htmltools::HTML)

# Creating the map itself
leaflet(
  west_hex_map) %>%
  addPolygons(
    color = "grey",
    weight=0.75,
    opacity = 0.5,
    fillOpacity = 1,
    fillColor = ~party_colour,
    label=labels) 

Mapping Parliamentary Petitions

The UK Parliamentary Petition site has facilities for mapping which parliamentary constituencies signatories of petitions live in. However, it only produces geographic maps, not hexagrams.

I have chosen two competing petitions: ‘Donald Trump should make a State Visit to the United Kingdom’ and ‘Prevent Donald Trump from making a State Visit to the United Kingdom’, both of which attracted large numbers of signatures: 317,492 for the pro-Trump petition, and 1,863,565 on the anti-Trump petition. The two maps use different colour scales, as using the same colour scale for both maps, which can aid direct comparison, renders the pro-Trump map too faint to read.

Pro-Trump petition signatories

library(leaflet)
library(sf)
library(dplyr)
library(hansard)

west_hex_map <- parlitools::west_hex_map #Base map

trump_yes <- epetition(ID = 680905, by_constituency=TRUE) #Download pro-inviting Trump signatures

pal = colorNumeric("Oranges", trump_yes$number_of_signatures)

west_trump_yes <- left_join(west_hex_map, trump_yes, by = "gss_code") #Joining to base map

label_yes <- paste0(
  "<strong>", west_trump_yes$constituency_name, "</strong>", "</br>",
  "Signatures: ", west_trump_yes$number_of_signatures
) %>% lapply(htmltools::HTML)

leaflet(
  west_trump_yes) %>%
  addPolygons(
    color = "grey",
    weight=0.75,
    opacity = 0.5,
    fillOpacity = 1,
    fillColor = ~pal(number_of_signatures),
    label = label_yes) %>%
  addLegend("topright", pal = pal, values = ~number_of_signatures,
    title = "Number of Signatures",
    opacity = 1
  )

Anti-Trump petition signatories

library(leaflet)
library(sf)
library(dplyr)
library(hansard)

west_hex_map <- parlitools::west_hex_map #Base map

trump_no <- epetition(ID = 648278, by_constituency=TRUE) #Download anti-inviting Trump signatures

west_trump_no <- left_join(west_hex_map, trump_no, by = "gss_code") #Joining to base map

pal = colorNumeric("Blues", trump_no$number_of_signatures)

label_no <- paste0(
  "<strong>", west_trump_no$constituency_name, "</strong>", "</br>",
  "Signatures: ", west_trump_no$number_of_signatures
) %>% lapply(htmltools::HTML)

leaflet(
  west_trump_no) %>%
  addPolygons(
    color = "grey",
    weight=0.75,
    opacity = 0.5,
    fillOpacity = 1,
    fillColor = ~pal(number_of_signatures),
    label = label_no) %>%
  addLegend("topright", pal = pal, values = ~number_of_signatures,
    title = "Number of Signatures",
    opacity = 1
  )