Advertisement
Julio974

PollGraphs1

Oct 12th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.38 KB | None | 0 0
  1. library("tidyverse")
  2.  
  3. polls <- read.csv("polls_nz.csv")
  4.  
  5. polls <- polls %>%
  6.   mutate(
  7.     date = format(as.Date(c(paste(year,month, day, sep="-")), by = "days"))
  8.   )
  9.  
  10. for(i in c("NAT","LAB","NZF","GRN","ACT","TOP","MRI","NCP","ANZ")) {
  11.   polls <- within(polls, {
  12.     assign(paste0("ci_", i), 1.96 * sqrt(( get(i) * (100 - get(i))) / n))
  13.   }
  14.   )
  15. }
  16.  
  17. polls.10m <- polls[polls$date > seq(as.Date(Sys.Date()), length = 2, by = "-10 months")[2],]
  18. polls.100 <- polls[order(as.Date(polls$date)),] %>% top_n(5000, as.Date(polls$date))
  19. PreviousResults = data.frame(date = as.Date("2017-09-23"), support = c(44.4,36.9,7.2,6.3,0.5,2.4,1.2,0.2))
  20.  
  21. svg('Opinion polling for the 2020 New Zealand general election.svg', width = 12, height = 6)
  22. polls.100 %>%
  23.   gather(party, support, c(NAT,LAB,NZF,GRN,ACT,TOP,MRI,NCP), factor_key=TRUE) %>%
  24.   ggplot(aes(x=as.Date(date), y=support, color=party)) +
  25.   geom_point(size=2.5, alpha=0.275) +
  26.   geom_smooth(se=FALSE, method="loess", span=0.625) +
  27.   geom_hline(yintercept=5,color="#808080") +
  28.   labs(y = NULL,
  29.        x = NULL) +
  30.   guides(color = guide_legend(ncol = 1, override.aes = list(linetype = 0, size = 3, alpha = 1))) +
  31.   scale_color_manual(labels = c("National","Labour","NZ First", "Green","ACT","TOP","Maori","NCP"), breaks = c("NAT","LAB","NZF", "GRN","ACT","TOP","MRI","NCP"), values = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF")) +#,"#CFEBFC")) +
  32.   theme(
  33.     plot.margin = margin(t = 0, unit = "cm"),
  34.     plot.background = element_blank(), panel.background = element_rect(fill = "grey92", color = NA),
  35.     panel.border = element_blank(), legend.background = element_rect(fill = "transparent", color = NA),
  36.     legend.key = element_rect(fill = "transparent", color = NA), legend.title = element_blank(),
  37.     strip.background = element_rect(fill = "transparent", color = NA),
  38.     panel.grid.major = element_line(color = "#FFFFFF"), panel.grid.minor = element_line(color = "#FFFFFF", size = 0.25),
  39.     axis.ticks = element_line(color = "grey20"), axis.line = element_blank(),
  40.     plot.title = element_text(size = 12, hjust = 0),
  41.     plot.subtitle = element_text(size = 12, hjust = 0),
  42.     plot.caption = element_text(size = 12, color = "#212121"),
  43.     axis.title = element_text(size = 12, face = "plain"), axis.text = element_text(size = 12, face = "plain", color = "grey30"),
  44.     legend.position = "right",
  45.     legend.text = element_text(size = 12), strip.text = element_text(size = 12, face = "plain"),
  46.     legend.margin = margin(t = 0, unit = "cm"),
  47.   ) +
  48.   scale_y_continuous(breaks = seq(0,65,5), minor_breaks = seq(0,65,1), limits = c(0, 65), expand = c(0, 0)) +
  49.   scale_x_date(breaks="6 months", minor_breaks="1 month", expand = c(0, 0)) +
  50.   geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=5, shape=5) +
  51.   geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=4.5, shape=18)
  52. dev.off()
  53.  
  54.  
  55.  
  56.  
  57. svg('Opinion polling for the 2020 New Zealand general election (zoomed on smaller parties).svg', width = 12, height = 6)
  58. polls.100 %>%
  59.   gather(party, support, c(NZF,GRN,ACT,TOP,MRI,NCP), factor_key=TRUE) %>%
  60.   ggplot(aes(x=as.Date(date), y=support, color=party)) +
  61.   geom_point(size=2.5, alpha=0.275) +
  62.   geom_smooth(se=FALSE, method="loess", span=0.625) +
  63.   geom_hline(yintercept=5,color="#808080") +
  64.   labs(y = NULL,
  65.        x = NULL) +
  66.   guides(color = guide_legend(ncol = 1, override.aes = list(linetype = 0, size = 3, alpha = 1))) +
  67.   scale_color_manual(labels = c("NZ First", "Green","ACT","TOP","Maori","NCP"), breaks = c("NZF", "GRN","ACT","TOP","MRI","NCP"), values = c("#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF")) +#,"#CFEBFC")) +
  68.   theme(
  69.     plot.margin = margin(t = 0, unit = "cm"),
  70.     plot.background = element_blank(), panel.background = element_rect(fill = "grey92", color = NA),
  71.     panel.border = element_blank(), legend.background = element_rect(fill = "transparent", color = NA),
  72.     legend.key = element_rect(fill = "transparent", color = NA), legend.title = element_blank(),
  73.     strip.background = element_rect(fill = "transparent", color = NA),
  74.     panel.grid.major = element_line(color = "#FFFFFF"), panel.grid.minor = element_line(color = "#FFFFFF", size = 0.25),
  75.     axis.ticks = element_line(color = "grey20"), axis.line = element_blank(),
  76.     plot.title = element_text(size = 12, hjust = 0),
  77.     plot.subtitle = element_text(size = 12, hjust = 0),
  78.     plot.caption = element_text(size = 12, color = "#212121"),
  79.     axis.title = element_text(size = 12, face = "plain"), axis.text = element_text(size = 12, face = "plain", color = "grey30"),
  80.     legend.position = "right",
  81.     legend.text = element_text(size = 12), strip.text = element_text(size = 12, face = "plain"),
  82.     legend.margin = margin(t = 0, unit = "cm"),
  83.   ) +
  84.   scale_y_continuous(breaks = seq(0,10,1), minor_breaks = seq(0,10,1), limits = c(0, 10), expand = c(0, 0)) +
  85.   scale_x_date(breaks="6 months", minor_breaks="1 month", expand = c(0, 0)) +
  86.   geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=5, shape=5) +
  87.   geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=4.5, shape=18)
  88. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement