Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active November 1, 2019 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cavedave/817ee2dfb87fdaba70984aca4ade6f20 to your computer and use it in GitHub Desktop.
Save cavedave/817ee2dfb87fdaba70984aca4ade6f20 to your computer and use it in GitHub Desktop.
UK election visualisations
---
title: "uk election"
output: html_notebook
---
Inspired by this great graph by Alasdair Rae
https://twitter.com/undertheraedar/status/1190192038274310144 i want to make an election visualisation
data from https://commonslibrary.parliament.uk/parliament-and-elections/elections-elections/constituency-data-election-results/
```{r}
library("readxl")
# xls files
election<-read_excel("Current-Parliament-Election-Results.xlsx", sheet = 3)
head(election)
```
I will use ConstituencyName, MajorityRank,
Elected TRUE
and that CandidateParty
First filter by elected
```{r}
library(tidyverse)
election <-
election %>%
filter(Elected=="TRUE")
head(election)
```
1% difference does not seem right lets look at the max value to see if it is true
```{r}
#max(election$MajorityPercentageValue, na.rm = TRUE)
election[which.max(election$MajorityPercentageValue),]
```
ah its not .77 it is 77%
So I need to multiply by 100
```{r}
election <-
mutate(election,
MajorityPercentageValue=MajorityPercentageValue*100)
head(election)
```
Now get colors of parties
https://en.wikipedia.org/wiki/Wikipedia:Index_of_United_Kingdom_political_parties_meta_attributes
```{r}
library(ggplot2)
```
Start making graphs changing them each step
```{r}
ggplot(election, aes(x = ConstituencyName, y = MajorityPercentageValue)) + geom_bar(stat = "identity")
```
order by majority
```{r}
ggplot(election, aes(x =reorder(ConstituencyName, -MajorityPercentageValue) , y = MajorityPercentageValue)) + geom_bar(stat = "identity")
```
color it in
```{r}
ggplot(election, aes(x =reorder(ConstituencyName, -MajorityPercentageValue) , y = MajorityPercentageValue, fill=CandidateParty)) + geom_bar(stat = "identity")
```
Lets combine small parties in a way that might be cheating
speaker
```{r}
#election['CandidateParty']<-str_replace_all(election['CandidateParty'], "Labour and Co-operative", "Labour")
election$CandidateParty[election$CandidateParty=="Labour and Co-operative"] <- "Labour"
election$CandidateParty[election$CandidateParty=="Green"] <- "Independent"
election <-
election %>%
filter(CandidateParty !="Speaker")
head(election)
```
now put in the colors for the actual party
```{r}
p<-ggplot(election, aes(x =reorder(ConstituencyName, -MajorityPercentageValue) , y = MajorityPercentageValue, fill=CandidateParty)) + geom_bar(stat = "identity")
p<-p+ scale_fill_manual(values=c('#0087DC','#D46A4C','#DDDDDD','#b22222','#FAA61A','#008142','#FDF38E','#326760'))
p
#cons #0087DC
#DUP #D46A4C
#green #6AB023 gone
#ind #DDDDDD
#lab #b22222
#lab ind #b22222 gone
#lib dem #FAA61A
#plaid #008142
#snp #FDF38E
#sf #326760
#speaker
```
now change labels
```{r}
p<-p + ggtitle("Majorities in English Constituencies") +
xlab("Constituency") + ylab("Majority %") + theme(plot.title = element_text(hjust = 0.5))
p
```
all the constituency names will not fit in so remove them
```{r}
p<-p+ theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
plot.title = element_text(size = 20, face = "bold"))
p
```
```{r}
ggsave("majorityEngland.png",height=10, width=15)
```
Now do scatterplot with deprivation
Find values in region
RegionName
```{r}
unique(election$RegionName)
```
remove regions not in England
```{r}
target <- c("West Midlands", "London","South West","North West","Yorkshire and The Humber","South East","East Midlands","East" ,"North East")
england <- filter(election, RegionName %in% target)
head(england)
```
```{r}
p<-ggplot(england, aes(x =reorder(ConstituencyName, -MajorityPercentageValue) , y = MajorityPercentageValue, fill=CandidateParty)) + geom_bar(stat = "identity")
p<-p+ scale_fill_manual(values=c('#0087DC','#DDDDDD','#b22222','#FAA61A','#326760'))
p
#cons #0087DC
#DUP #D46A4C
#green #6AB023 gone
#ind #DDDDDD
#lab #b22222
#lab ind #b22222 gone
#lib dem #FAA61A
#plaid #008142
#snp #FDF38E
#sf #326760
#speaker
```
Now get the deprevation data
https://researchbriefings.parliament.uk/ResearchBriefing/Summary/CBP-7327
CBP7327.xlsx
```{r}
depr<-read_excel("CBP7327.xlsx", sheet = 2, skip=6)
head(depr)
```
```{r}
head(england)
```
```{r}
both<-merge(england, depr, by.x="ConstituencyName", by.y="Constituency")
head(both)
```
Index of Multiple Deprivation
p<-ggplot(election, aes(x =reorder(ConstituencyName, -MajorityPercentageValue) , y = MajorityPercentageValue, fill=CandidateParty)) + geom_bar(stat = "identity")
```{r}
both<-both %>%
rename(
Deprivation = "Index of Multiple Deprivation"
)
```
```{r}
r<-ggplot(both, aes(x = Deprivation , y = MajorityPercentageValue,color=CandidateParty))+ geom_point(size = 2)
r<-r+ scale_color_manual(values=c('#0087DC','#DDDDDD','#b22222','#FAA61A'))
r
#cons #0087DC
#DUP #D46A4C
#green #6AB023 gone
#ind #DDDDDD
#lab #b22222
#lab ind #b22222 gone
#lib dem #FAA61A
#plaid #008142
#snp #FDF38E
#sf #326760
#speaker
```
```{r}
r<-r + ggtitle("Majorities and Deprivation\n in English Constituencies") +
xlab("Deprivation") + ylab("Majority %") + theme(plot.title = element_text(hjust = 0.5))
r
```
```{r}
r<-r + theme_classic()
r<-r+ theme(
plot.title = element_text(size = 17, face = "bold"))
r
```
```{r}
ggsave("majorityEnglandDeprivation3.png",height=7, width=10)
```
@cavedave
Copy link
Author

cavedave commented Nov 1, 2019

majority

@cavedave
Copy link
Author

cavedave commented Nov 1, 2019

majorityEnglandDeprivation3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment