File:MansfieldGraph.svg: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
User created page with UploadWizard |
Add notes with source data and code for reproducing the graph with R |
||
Line 8: | Line 8: | ||
|other versions= |
|other versions= |
||
}} |
}} |
||
==Notes== |
|||
The Background colour indicates the party of the sitting MP at any given year. |
|||
Graph drawn with [[:en:R (programming language)|R]] |
|||
Parties: |
|||
*Conservative = [[:en:Conservative Party (UK)|Conservative Party]] (1885–1918; 1964–present); Conservative and [[:en:Liberal Unionist Party|Liberal Unionist]] (1922–1959). |
|||
*Labour = [[:en:Labour Party (UK)|Labour Party]] |
|||
*Liberal = [[:en:Liberal Party (UK)|Liberal Party]] (1885–1979); [[:en:SDP-Liberal Alliance|SDP-Liberal Alliance]] (1983–1987); [[:en:Liberal Democrats (UK)|Liberal Democrats]] (1992–present). |
|||
Data: |
|||
{| class="wikitable" |
|||
! Year !! Conservative !! Labour !! Liberal !! UKIP !! Other |
|||
|- |
|||
| 1885.96 || 27.4 || NA || 72.6 || NA || NA |
|||
|- |
|||
| 1886.57 || 36.7 || NA || 63.3 || NA || NA |
|||
|- |
|||
| 1892.57 || 36.1 || NA || 63.9 || NA || NA |
|||
|- |
|||
| 1895.6 || 43 || NA || 57 || NA || NA |
|||
|- |
|||
| 1900.81 || 38.8 || NA || 61.2 || NA || NA |
|||
|- |
|||
| 1906.61 || NA || NA || NA || NA || NA |
|||
|- |
|||
| 1910.11 || 25.8 || NA || 74.2 || NA || NA |
|||
|- |
|||
| 1910.97 || 27 || NA || 73 || NA || NA |
|||
|- |
|||
| 1916 || NA || NA || 63 || NA || 37 |
|||
|- |
|||
| 1918.95 || NA || 43.6 || 19.5 || NA || 36.9 |
|||
|- |
|||
| 1922.87 || NA || 47.95 || 52.05 || NA || NA |
|||
|- |
|||
| 1923.93 || NA || 57.8 || 42.2 || NA || NA |
|||
|- |
|||
| 1929.41 || 18.7 || 58.5 || 21.7 || NA || 1.1 |
|||
|- |
|||
| 1931.82 || 44.23 || 55.77 || NA || NA || NA |
|||
|- |
|||
| 1935.87 || 31.99 || 68.01 || NA || NA || NA |
|||
|- |
|||
| 1941 || NA || NA || NA || NA || NA |
|||
|- |
|||
| 1945.51 || 24.91 || 75.09 || NA || NA || NA |
|||
|- |
|||
| 1950.148 || 22.99 || 66.65 || 9.47 || NA || 0.89 |
|||
|- |
|||
| 1951.82 || 30.08 || 69.92 || NA || NA || NA |
|||
|- |
|||
| 1955.4 || 31.54 || 68.46 || NA || NA || NA |
|||
|- |
|||
| 1959.77 || 32.12 || 67.88 || NA || NA || NA |
|||
|- |
|||
| 1964.79 || 21.93 || 63.57 || 14.5 || NA || NA |
|||
|- |
|||
| 1966.25 || 22.24 || 64.24 || 12.21 || NA || 1.31 |
|||
|- |
|||
| 1970.46 || 32.52 || 66.12 || NA || NA || 1.36 |
|||
|- |
|||
| 1974.16 || 34.22 || 64.51 || NA || NA || 1.27 |
|||
|- |
|||
| 1974.78 || 23.16 || 57.41 || 18.55 || NA || 0.89 |
|||
|- |
|||
| 1979.34 || 31.89 || 52.28 || 15.36 || NA || 0.47 |
|||
|- |
|||
| 1983.44 || 35.6 || 40.5 || 23.9 || NA || NA |
|||
|- |
|||
| 1987.44 || 37.4 || 37.5 || 22.2 || NA || 3 |
|||
|- |
|||
| 1992.27 || 33.1 || 54.4 || 12.6 || NA || NA |
|||
|- |
|||
| 1997.33 || 21.2 || 64.4 || 11.1 || NA || 3.3 |
|||
|- |
|||
| 2001.43 || 27.2 || 57.1 || 15.7 || NA || NA |
|||
|- |
|||
| 2005.34 || 18.4 || 48.1 || 13.9 || NA || 19.7 |
|||
|- |
|||
| 2010.35 || 26.3 || 38.7 || 15.4 || 6.2 || 13.4 |
|||
|- |
|||
| 2015.35 || 28.2 || 39.4 || 3.5 || 25.1 || 3.8 |
|||
|- |
|||
| 2017.44 || 46.6 || 44.5 || 1.4 || 5.3 || 2.2 |
|||
|} |
|||
Code: |
|||
The graph was produced with [[:en:R (programming language)|R]]. The following code will reproduce the graph using the data on this page. |
|||
<source lang="rout"> |
|||
library(tidyverse) |
|||
library(htmltab) |
|||
library(lubridate) |
|||
election_graph <- function(pageURL) { |
|||
election <- htmltab(pageURL, |
|||
which = 2, |
|||
rm_nodata_cols = F) |
|||
election <- as.tibble(lapply(election, function(x) {gsub("unopp", "100", x)})) |
|||
tidy_election <- gather(election, "Party", "Votes", 2:length(election)) |
|||
tidy_election$Year <- as.numeric(tidy_election$Year) |
|||
tidy_election$Party <- factor(tidy_election$Party, levels = c("Conservative", "Labour", "Liberal", "Green", "SNP", "UKIP", "Other")) |
|||
tidy_election$Votes <- as.numeric(tidy_election$Votes) |
|||
election_victor <- tidy_election %>% filter(is.na(Votes) == FALSE) %>% group_by(Year) %>% summarize(Party = Party[which(Votes == max(Votes))]) |
|||
election_victor$Year <- as.numeric(election_victor$Year) |
|||
election_victor$start_year <- election_victor$Year |
|||
election_victor$end_year <- c(election_victor$Year[-1], ceiling(election_victor$Year[length(election_victor$Year)] + 1)) |
|||
election_victor[1,3] <- floor(election_victor[1,3] - 1) |
|||
tidy_election$Votes <- as.numeric(sapply(tidy_election$Votes, function(x) {gsub(100, NA, x)})) |
|||
party_colours <- c("#0087DC", "#DC241F", "#FAA61A", "#008066", "#FFF95D", "#EFE600", "dark grey") |
|||
names(party_colours) <- c("Conservative", "Labour", "Liberal", "Green", "SNP", "UKIP", "Other") |
|||
ggplot(tidy_election) + |
|||
geom_rect(data = election_victor, |
|||
aes(xmin = start_year,xmax = end_year, ymin = -Inf, ymax = Inf, fill = Party), |
|||
alpha = 0.35, |
|||
show.legend = F) + |
|||
geom_line(aes(x = Year, y = Votes, colour = Party), size = 0.703) + |
|||
geom_point(aes(x = Year, y = Votes, colour = Party)) + |
|||
geom_hline(yintercept = 100, color="black", size = 1.5) + |
|||
geom_vline(xintercept = 2019, color="black", size = 1.5) + |
|||
scale_colour_manual(values = party_colours) + |
|||
scale_fill_manual(values = party_colours) + |
|||
theme(text = element_text(color="black", size = 14), |
|||
axis.text = element_text(color="black", size = 11), |
|||
axis.line.x = element_line(color="black", size = 0.703), |
|||
axis.ticks.x = element_line(color="black", size = 0.703), |
|||
axis.line.y = element_line(color="black", size = 0.703), |
|||
axis.ticks.y = element_line(color="black", size = 0.703), |
|||
axis.ticks.length = unit(5, "points"), |
|||
panel.grid.major = element_line(color="blue", size = 0.5, linetype = 3), |
|||
panel.grid.minor = element_blank(), |
|||
panel.background = element_blank(), |
|||
legend.position = c(.98, .97), |
|||
legend.direction = "horizontal", |
|||
legend.text = element_text(color="black", size = 11), |
|||
legend.title=element_blank(), |
|||
legend.justification = c("right", "top"), |
|||
legend.box.just = "right", |
|||
legend.key = element_blank(), |
|||
legend.background = element_rect(fill = "white", colour = "black"), |
|||
legend.margin = margin(0, 4, 4, 4)) + |
|||
scale_x_continuous(expand = c(0, 0), limits = c(election_victor[[1,3]], election_victor$end_year[length(election_victor$end_year)]), breaks = seq(1890, 2010, 10)) + |
|||
scale_y_continuous(expand = c(0, 0), limits = c(0, 100), breaks = seq(0, 100, 20)) + |
|||
labs(x = "Year", y = "Percentage Vote") |
|||
} |
|||
election_graph("https://commons.wikimedia.org/wiki/File:MansfieldGraph.svg") |
|||
ggsave("MansfieldGraph.svg", device = "svg", units = "cm", width = 20, height = 11, dpi = 120) |
|||
</source> |
|||
=={{int:license-header}}== |
=={{int:license-header}}== |