File:NewarkGraph.svg

Summary

Description
English: United Kingdom general election results for Newark constituency.
Date
Source Own work
Author JeremyA
SVG development
InfoField
 The source code of this SVG is invalid due to an error.
 This W3C-invalid chart was created with R.
Category:Invalid SVG created with R#1000NewarkGraph.svg
 Category:Translation possible - SVGThis file uses embedded text that can be easily translated using a text editor.

Notes

The Background colour indicates the party of the sitting MP at any given year. Gaps in the data indicate elections where only a single candidate stood, and was returned unopposed.

Graph drawn with R

Parties:

Data:

YearConservativeLabourLiberalUKIPOther
1885.9660NA40NANA
190064.3NA35.7NANA
1900.81NANANANANA
1906.6151.8NA48.2NANA
1910.1154.3NA45.7NANA
1910.9754NA46NANA
1918.95NANANANANA
1922.8764.835.2NANANA
1923.9355.9NA44.1NANA
1924.8360.521.817.7NANA
1929.4145.523.331.2NANA
1931.8270.1329.87NANANA
1935.8762.4137.59NANANA
194361.17NANANA38.84
1945.5145.0942.3512.56NANA
1950.14840.2854.25.52NANA
1951.8242.8157.19NANANA
1955.447.5752.43NANANA
1959.7748.0951.91NANANA
1964.7945.6454.36NANANA
1966.2543.2856.72NANANA
1970.4648.8251.18NANANA
1974.1646.1753.83NANANA
1974.7837.547.8914.61NANA
1979.3445.9542.9511.21NANA
1983.4453.8324.6320.6NA0.95
1987.4453.5427.7118.75NANA
1992.2750.435.813NA0.8
1997.3339.445.211.5NA3.9
2001.4346.537.513.2NA2.8
2005.344833.915.92.2NA
2010.3553.922.3203.8NA
20144517.72.625.98.8
2015.355721.74.6124.6
2017.4462.729.75.12.6NA

Source code

The logo of R – programming language for statistical analysis
The logo of R – programming language for statistical analysis
This media was created with R (programming language for statistical analysis)Category:Images with R source code
Here is a listing of the source used to create this file.

Deutsch  English  +/− The graph was produced with R. The following code will reproduce the graph using the data on this page.

  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:NewarkGraph.svg")
  
ggsave("NewarkGraph.svg", device = "svg", units = "cm", width = 20, height = 11, dpi = 120)

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Category:CC-BY-SA-4.0#NewarkGraph.svgCategory:Self-published work
Category:Politics of Nottinghamshire
Category:CC-BY-SA-4.0 Category:Images with R source code Category:Invalid SVG created with R Category:Politics of Nottinghamshire Category:Self-published work Category:Translation possible - SVG