Skip to content

Instantly share code, notes, and snippets.

@Nixinova
Created October 1, 2021 21:17
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 Nixinova/0e2dd9d9ef15fbd029b0f93a7b99b63f to your computer and use it in GitHub Desktop.
Save Nixinova/0e2dd9d9ef15fbd029b0f93a7b99b63f to your computer and use it in GitHub Desktop.
Preferred prime minister polling, New Zealand
# In RStudio, Ctrl+A then Run
library(ggplot2)
library(dplyr)
library(svglite)
setwd("xxxxx") # replace with own working directory
# Read data
csvData <- read.csv("poll-data.csv")
pollingData <- arrange(csvData, desc(as.Date(Date, '%Y/%m/%d')))
# Plot data
spansize <- 0.45 # higher = smoother
ggplot(pollingData, aes(x = as.Date(Date, '%Y/%m/%d'))) +
theme_bw() +
# Ardern
geom_point(aes(y = Ardern, colour = "Ardern"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = Ardern, colour = "Ardern"), span = spansize, se = FALSE) +
# Collins
geom_point(aes(y = Collins, colour = "Collins"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = Collins, colour = "Collins"), span = spansize, se = FALSE) +
# Muller
geom_point(aes(y = Muller, colour = "Muller"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = Muller, colour = "Muller"), span = spansize, se = FALSE) +
# Bridges
geom_point(aes(y = Bridges, colour = "Bridges"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = Bridges, colour = "Bridges"), span = spansize, se = FALSE) +
# English
geom_point(aes(y = English, colour = "English"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = English, colour = "English"), span = spansize, se = FALSE) +
# Peters
geom_point(aes(y = Peters, colour = "Peters"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = Peters, colour = "Peters"), span = spansize, se = FALSE) +
# Seymour
geom_point(aes(y = Seymour, colour = "Seymour"), size = 2, alpha = 0.3) +
geom_smooth(aes(y = Seymour, colour = "Seymour"), span = spansize, se = FALSE) +
# Y-axis
scale_y_continuous(limits = c(0, 70), breaks = c(0,10,20,30,40,50,60,70), minor_breaks = waiver(), expand = c(0, 0)) +
# X-axis
scale_x_date(date_breaks = "4 months", date_labels = "%b '%y", minor_breaks = "1 month") +
# Axis styling
theme(
plot.background = element_rect(fill = "white", color = NA),
axis.text.x = element_text(angle = 0, vjust = 0.5, size = 10),
axis.text.y = element_text(size = 12),
axis.title.y = element_text(size = 12)
) +
# Axis labels
labs(y = "Support (%)", x = NULL) +
# Colors and key
scale_color_manual(
name = "",
# Legend
labels = c(
"Jacinda Ardern",
"Judith Collins",
"Todd Muller",
"Simon Bridges",
"Bill English",
"Winston Peters",
"David Seymour"
),
# Color mapping
values = c(
# For color values see https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf p.3
"Ardern"="red",
"Collins"="dodgerblue",
"Muller"="darkslateblue",
"Bridges"="blue",
"English"="cyan",
"Peters"="black",
"Seymour"="gold"
)
) +
theme(
legend.position = "bottom",
legend.text = element_text(size = 12)
)
# Save graph
ggsave(
"polling-graph.svg",
width = 12,
height = 7
)
Date Ardern Collins Muller Bridges English Peters Seymour
2017/12/05 37 0.7 0.3 28 5
2018/01/28 37.9 0.2 0.5 25.7 5.7
2018/02/14 41 0.4 1 20 4 0.1
2018/04/11 37 2 10 2 5 0.3
2018/05/23 41 2 12 0.9 4 0.1
2018/05/24 40.2 3.7 9 4.2 4.6
2018/08/01 40 2 10 0.9 5 0.2
2018/10/19 42 5 7 1 4
2018/11/28 39 6 7 0.4 4 0.1
2019/02/02 41.8 6.2 5
2019/02/13 44 6 6 0.4 3 0.1
2019/04/10 51 5 5 0.3 3 0.2
2019/06/07 49 7.1 4.2
2019/06/08 45 6 5 0.1 5 0.5
2019/07/24 41 6 0.1 6 0.2 2 0.4
2019/10/09 38.4 5.2 6.7
2019/10/09 38 5 0.2 9 0.1 4 0.4
2019/11/27 36 4 10 3 0.6
2020/02/01 38.7 10.6
2020/02/12 42 3 11 0.2 3 0.8
2020/04/27 65 7 7 3
2020/05/16 59.5 3.1 4.5
2020/05/20 63 3 0.2 5 0.1 1 0.4
2020/06/01 65 13
2020/06/24 54 2 13 0.4 0.2 2 0.8
2020/07/24 62 14.6
2020/07/29 54 20 0.2 1 1
2020/09/21 54 18 0.1 0.3 2 2
2020/09/23 53.2 17.7
2020/09/27 54 23 0.1 0.1 1 2
2020/10/07 50 23 0.1 0.2 0.2 1 2
2020/10/14 55 20 0.4 1 3
2020/10/15 52.6 18.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment