A collection of useful tools for UK politics, including base maps and datasets. Initially inspired by Bhaskar Karambelkar's tilegrams
package, but with the ability to create a hexagonal map of UK parliamentary constituencies. The package also includes functions for data retrieval of current MPs and their consituency details (as it requires calls to two different APIs, this function is not included in my hansard
or mnis
packages), and data from the 2015 UK General Election, courtesy of the British Election Study.
The most up-to-date documentation for the development version will always be at http://docs.evanodell.com/parlitools/.
parlitools
is available on CRAN. To install on CRAN run:
install.packages("parlitools")
Or, if you use pacman:
pacman::p_load(parlitools)
To install the development version from GitHub, run:
#install.packages("parlitools") devtools::install_github("evanodell/parlitools")
For more details see the full documentation or the vignettes.
party_colour
- A tibble with the ID, name and hex code for the official colour of a variety of political parties, taken from Wikipedia. Includes all political parties with MPs and a number without MPs. (Sources: https://en.wikipedia.org/wiki/Wikipedia:Index_of_United_Kingdom_political_parties_meta_attributes, mnis::ref_parties()
)
bes_2015
- A tibble with the British Election Study 2015 Constituency Results Version 2.2. For information on all the variables in this dataset, see the bes-2015 vignette (Source: http://www.britishelectionstudy.com/data-object/2015-bes-constituency-results-with-census-and-candidate-data/)
bes_2017
- A tibble with Great Britain constituencies results from the 2017 general election. This data can be linked to 2011 census information in census_11
.
census_11
- A tibble with constituency-level census data.
leave_votes_west
- The percentage of votes cast for leave in the 2016 EU referendum. Some constituencies have actual results and others only have estimates by Chris Hanretty; in cases where the actual cote count is known, both the estimates and the actual results are reported. (Sources: https://secondreading.uk/brexit/brexit-votes-by-constituency/ http://www.bbc.co.uk/news/uk-northern-ireland-36616830)
current_mps
- Uses functions from hansard
and mnis
to create a tibble with data on all current MPs, their party affiliation and their constituency.
mps_on_date
- Uses functions from hansard
and mnis
to create a tibble with data on all MPs from a given date, their party affiliation and their constituency.
west_hex_map
- A hexagonal cartogram, stored as a simple feature and data frame, of Westminster parliamentary constituencies. west_hex_map
can be used to create maps like this:
library(leaflet) library(sf) library(htmlwidgets) 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 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>", "Party: ", west_hex_map$party_name, "</br>", "MP: ", west_hex_map$display_as, "</br>", "Most Recent Result: ", west_hex_map$result_of_election, "</br>", "Current Majority: ", west_hex_map$majority, " votes" ) %>% lapply(htmltools::HTML) # Creating the map itself leaflet(options=leafletOptions( dragging = FALSE, zoomControl = FALSE, tap = FALSE, minZoom = 6, maxZoom = 6, maxBounds = list(list(2.5,-7.75),list(58.25,50.0)), attributionControl = FALSE), west_hex_map) %>% addPolygons( color = "grey", weight=0.75, opacity = 0.5, fillOpacity = 1, fillColor = ~party_colour, label=labels) %>% htmlwidgets::onRender( "function(x, y) { var myMap = this; myMap._container.style['background'] = '#fff'; }")
local_hex_map
- Hexagonal cartogram, A hexagonal cartogram, stored as a simple feature and data frame, of all Local Authorities in England, Wales and Scotland.
parlitools
Instructions and examples of each function, along with maps and datasets, are available here.
For more details, please see the introductory vignette, using parlitools
with cartogram
, mapping local authorities and the vignette detailing the British Election Study 2015 and British Election Study 2017 variables.
There are a variety of potentially relevant data sources and datasets on UK politics, far too many for me to include them all in this package.
Electoral Commission - Electoral results dating back to 2005.
British Election Study - A large selection of open data, including panel surveys, linked data and aggregated Twitter data, covering elections and referenda.
My hansard
& mnis
data retrieval packages for parliamentary APIs.
The Qualitative Election Study of Britain - A collection of qualitiative interviews and questionnares covering the 2010, 2015 and 2017 General Elections, the 2016 EU referendum and the 2014 Scottish independence referendum.
Open Council Data has data on the names, parties, and wards of all UK councillors, updated more or less weekly.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.