Created
December 13, 2019 16:57
-
-
Save lashlee/a0dd0e9d8884c5e12cff473a975664a2 to your computer and use it in GitHub Desktop.
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(ggplot2) | |
library(ggthemes) | |
dat <- data.frame( | |
party = c('con', 'lab', 'snp', 'lib', 'dup', 'sf', 'pc', 'green', 'ukip_brexit'), | |
party_label = c('Conservative', 'Labour', 'Scottish National', 'Liberal Democrats', | |
'Democratic Unionist', 'Sinn Fein', 'Plaid Cymru', 'Green', 'Brexit & UKIP'), | |
vote_2017 = c(13636684, 12877918, 977568, 2371861, 292316, 238915, 164466, 525665, 594068), | |
vote_2019 = c(13941200, 10292054, 1242372, 3675342, 244128, 181853, 153265, 864743, 665120), | |
seats_2017 = c(317, 262, 35, 12, 10, 7, 4, 1, 0), | |
seats_2019 = c(364, 203, 48, 11, 8, 7, 4, 1, 0), | |
stringsAsFactors = FALSE | |
) | |
dat$seats_per_vote_2019 <- dat$seats_2019 / dat$vote_2019 | |
dat$votes_per_seat_2019 <- ifelse(dat$seats_per_vote_2019 != 0, 1/dat$seats_per_vote_2019, NA) | |
dat$abs_change <- dat$vote_2019 - dat$vote_2017 | |
dat$pct_change <- (dat$vote_2019 - dat$vote_2017) / dat$vote_2017 | |
dat$seat_change_per_vote_change_2017_to_2019 <- (dat$seats_2019 - dat$seats_2017) / dat$abs_change | |
dat$party_label_factor <- factor(dat$party_label, levels = dat$party_label[order(dat$vote_2017)]) | |
# | |
ggplot(dat) + | |
geom_col(aes(y = abs_change, x = party_label_factor), size = 1.5) + | |
geom_hline(yintercept = 0) + | |
theme_economist() + | |
scale_colour_fivethirtyeight() + | |
theme( | |
legend.position = "bottom", | |
legend.direction = "horizontal", | |
legend.title = element_blank() | |
) + | |
theme(axis.text.y = element_text(size = rel(1), face = 'bold')) + | |
scale_y_continuous( | |
labels = scales::unit_format(unit = 'K', scale = 1e-3, sep = ''), | |
breaks = seq(-2.6e6, 1.4e6, 4e5) | |
) + | |
labs( | |
x = '', | |
y = '', | |
caption = 'By @johnlashlee. Sources: BBC (2017), Google (2019). Accessed 2019-12-13.' | |
) + | |
ggtitle( | |
'Votes Gained from 2017 to 2019 (Thousand)' | |
) + | |
coord_flip() | |
ggplot(dat) + | |
geom_col(aes(y = pct_change, x = party_label_factor), size = 1.5) + | |
geom_hline(yintercept = 0) + | |
theme_economist() + | |
scale_colour_fivethirtyeight() + | |
theme( | |
legend.position = "bottom", | |
legend.direction = "horizontal", | |
legend.title = element_blank() | |
) + | |
theme(axis.text.y = element_text(size = rel(1), face = 'bold')) + | |
scale_y_continuous( | |
#labels = scales::unit_format(unit = 'K', scale = 1e-3, sep = ''), | |
#breaks = seq(-2.6e6, 1.4e6, 4e5) | |
labels = scales::percent | |
) + | |
labs( | |
x = '', | |
y = '', | |
caption = 'By @johnlashlee. Sources: BBC (2017), Google (2019). Accessed 2019-12-13.' | |
) + | |
ggtitle( | |
'Percentaged Change in Votes from 2017 to 2019' | |
) + | |
coord_flip() | |
ggplot(dat[dat$party != 'ukip_brexit',]) + | |
geom_col(aes(y = votes_per_seat_2019, x = party_label_factor), size = 1.5) + | |
geom_hline(yintercept = 0) + | |
theme_economist() + | |
scale_colour_fivethirtyeight() + | |
theme( | |
legend.position = "bottom", | |
legend.direction = "horizontal", | |
legend.title = element_blank() | |
) + | |
theme(axis.text.y = element_text(size = rel(1), face = 'bold')) + | |
scale_y_continuous( | |
labels = scales::unit_format(unit = 'K', scale = 1e-3, sep = '')#, | |
#breaks = seq(0, 340000, 20000) | |
) + | |
labs( | |
x = '', | |
y = '', | |
caption = 'By @johnlashlee. Sources: BBC (2017), Google (2019). Accessed 2019-12-13.' | |
) + | |
ggtitle( | |
'Votes Per Seat in 2019 (Thousands)' | |
) + | |
coord_flip() | |
ggplot(dat[!(dat$party %in% c('ukip_brexit', 'green', 'lib')), ]) + | |
geom_col(aes(y = votes_per_seat_2019, x = party_label_factor), size = 1.5) + | |
geom_hline(yintercept = 0) + | |
theme_economist() + | |
scale_colour_fivethirtyeight() + | |
theme( | |
legend.position = "bottom", | |
legend.direction = "horizontal", | |
legend.title = element_blank() | |
) + | |
theme(axis.text.y = element_text(size = rel(1), face = 'bold')) + | |
scale_y_continuous( | |
labels = scales::unit_format(unit = 'K', scale = 1e-3, sep = '') | |
) + | |
labs( | |
x = '', | |
y = '', | |
caption = 'By @johnlashlee. Sources: BBC (2017), Google (2019). Accessed 2019-12-13.' | |
) + | |
ggtitle( | |
'Votes Per Seat in 2019 (Thousands)' | |
) + | |
coord_flip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment