Brexit vote analysis

I wanted a better understanding of the Brexit vote and so thought I’d analyse it. In this post, I therefore plot the Brexit vote and winning parliamentary party from 2016 by constituency, leaning heavily on the parlitools and ggiraph packages.

Robin Penfold
2016-10-12


For completeness (and reproducibility), here’s the code that I used to calculate what’s above.


suppressMessages(library(tidyverse))
library(ggiraph)
library(parlitools)
library(scales)

map_details <- west_hex_map

brexitData <- leave_votes_west %>% 
  mutate(
    `Leave Vote` = figure_to_use,
    `Party of MP` = as_factor(party_2016), 
    `Party of MP` = fct_lump(
      `Party of MP`,
      n = 7, 
      other_level = "Other")
    ) %>% 
  left_join(
    map_details,
    by = c("ons_const_id" = "gss_code")
    ) %>% 
  mutate(
    constituency = ifelse(
      is.na(constituency_name.x), 
      constituency_name.y, 
      constituency_name.x
      )
  ) %>% 
  select(constituency, `Leave Vote`, `Party of MP`, ons_const_id, geometry) %>% 
  mutate(
    `Party of MP` = 
      recode(
        `Party of MP`,
        `Scottish National Party` = "SNP", 
        `Liberal Democrat` = "LibDem"
        )
    ) %>% 
  arrange(constituency)

gg <- brexitData %>% 
  ggplot() + 
  geom_sf_interactive(
    aes(
      group = constituency,
      geometry = geometry,
      fill = `Leave Vote`,
      colour = `Party of MP`,
      tooltip = paste0(
        constituency, 
        "\n MP: ",
        `Party of MP`,
        "\n Leave vote: ",
        round(`Leave Vote` * 100, 0),
        "%"
        )
    )
  ) + 
  scale_fill_gradient_interactive(
    low = "white", 
    high = "#63666a",
    labels = percent_format(accuracy = 1)
    ) +
  scale_colour_manual_interactive(
    values = c(
      Conservative = "#0087DC",
      DUP = "#D46A4C",
      Independent = "#6AB023",
      Other = "#AFAFAFAF",
      Labour = "#DC241F",
      LibDem = "#FDBB30",
      SNP = "#FFFF00",
      `Sinn Fein` = "#008800"
    )
  ) +
  theme_void() +
  labs(
    title = "Leave vote and winning party, by constituency", 
    subtitle = "Hover over a point for the details"
  ) + 
  theme(
    legend.position = "right",
    legend.text = element_text(size = 6, hjust = 0),
    legend.title = element_text(size = 8)
    )

girafe(ggobj = gg)

System settings


R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.15

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] gdtools_0.2.1    scales_1.0.0     parlitools_0.3.4
 [4] ggiraph_0.7.0    forcats_0.4.0    stringr_1.4.0   
 [7] dplyr_0.8.3      purrr_0.3.3      readr_1.3.1     
[10] tidyr_1.0.0      tibble_2.1.3     ggplot2_3.2.1   
[13] tidyverse_1.2.1 

loaded via a namespace (and not attached):
 [1] hansard_0.7.1      Rcpp_1.0.3         lubridate_1.7.4   
 [4] lattice_0.20-38    class_7.3-15       assertthat_0.2.1  
 [7] zeallot_0.1.0      digest_0.6.22      R6_2.4.0          
[10] cellranger_1.1.0   backports_1.1.5    evaluate_0.14     
[13] e1071_1.7-2        httr_1.4.1         pillar_1.4.2      
[16] mnis_0.2.7         rlang_0.4.1        lazyeval_0.2.2    
[19] readxl_1.3.1       uuid_0.1-2         rstudioapi_0.10   
[22] rmarkdown_1.16     labeling_0.3       htmlwidgets_1.5.1 
[25] munsell_0.5.0      broom_0.5.2        compiler_3.6.0    
[28] modelr_0.1.5       xfun_0.9           pkgconfig_2.0.3   
[31] systemfonts_0.1.1  htmltools_0.4.0    tidyselect_0.2.5  
[34] crayon_1.3.4       withr_2.1.2        sf_0.8-0          
[37] grid_3.6.0         nlme_3.1-139       jsonlite_1.6      
[40] gtable_0.3.0       lifecycle_0.1.0    DBI_1.0.0         
[43] magrittr_1.5       units_0.6-5        KernSmooth_2.23-15
[46] cli_1.1.0          stringi_1.4.3      snakecase_0.11.0  
[49] xml2_1.2.2         ellipsis_0.3.0     generics_0.0.2    
[52] vctrs_0.2.0        distill_0.7        tools_3.6.0       
[55] glue_1.3.1         hms_0.5.1          yaml_2.2.0        
[58] colorspace_1.4-1   classInt_0.4-1     rvest_0.3.4       
[61] knitr_1.25         haven_2.1.1