Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library("tidyverse")
- polls <- read.csv("polls_nz.csv")
- polls <- polls %>%
- mutate(
- date = format(as.Date(c(paste(year,month, day, sep="-")), by = "days"))
- )
- for(i in c("NAT","LAB","NZF","GRN","ACT","TOP","MRI","NCP","ANZ")) {
- polls <- within(polls, {
- assign(paste0("ci_", i), 1.96 * sqrt(( get(i) * (100 - get(i))) / n))
- }
- )
- }
- polls.10m <- polls[polls$date > seq(as.Date(Sys.Date()), length = 2, by = "-10 months")[2],]
- polls.100 <- polls[order(as.Date(polls$date)),] %>% top_n(5000, as.Date(polls$date))
- 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))
- svg('Opinion polling for the 2020 New Zealand general election.svg', width = 12, height = 6)
- polls.100 %>%
- gather(party, support, c(NAT,LAB,NZF,GRN,ACT,TOP,MRI,NCP), factor_key=TRUE) %>%
- ggplot(aes(x=as.Date(date), y=support, color=party)) +
- geom_point(size=2.5, alpha=0.275) +
- geom_smooth(se=FALSE, method="loess", span=0.625) +
- geom_hline(yintercept=5,color="#808080") +
- labs(y = NULL,
- x = NULL) +
- guides(color = guide_legend(ncol = 1, override.aes = list(linetype = 0, size = 3, alpha = 1))) +
- 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")) +
- theme(
- plot.margin = margin(t = 0, unit = "cm"),
- plot.background = element_blank(), panel.background = element_rect(fill = "grey92", color = NA),
- panel.border = element_blank(), legend.background = element_rect(fill = "transparent", color = NA),
- legend.key = element_rect(fill = "transparent", color = NA), legend.title = element_blank(),
- strip.background = element_rect(fill = "transparent", color = NA),
- panel.grid.major = element_line(color = "#FFFFFF"), panel.grid.minor = element_line(color = "#FFFFFF", size = 0.25),
- axis.ticks = element_line(color = "grey20"), axis.line = element_blank(),
- plot.title = element_text(size = 12, hjust = 0),
- plot.subtitle = element_text(size = 12, hjust = 0),
- plot.caption = element_text(size = 12, color = "#212121"),
- axis.title = element_text(size = 12, face = "plain"), axis.text = element_text(size = 12, face = "plain", color = "grey30"),
- legend.position = "right",
- legend.text = element_text(size = 12), strip.text = element_text(size = 12, face = "plain"),
- legend.margin = margin(t = 0, unit = "cm"),
- ) +
- scale_y_continuous(breaks = seq(0,65,5), minor_breaks = seq(0,65,1), limits = c(0, 65), expand = c(0, 0)) +
- scale_x_date(breaks="6 months", minor_breaks="1 month", expand = c(0, 0)) +
- geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=5, shape=5) +
- geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=4.5, shape=18)
- dev.off()
- svg('Opinion polling for the 2020 New Zealand general election (zoomed on smaller parties).svg', width = 12, height = 6)
- polls.100 %>%
- gather(party, support, c(NZF,GRN,ACT,TOP,MRI,NCP), factor_key=TRUE) %>%
- ggplot(aes(x=as.Date(date), y=support, color=party)) +
- geom_point(size=2.5, alpha=0.275) +
- geom_smooth(se=FALSE, method="loess", span=0.625) +
- geom_hline(yintercept=5,color="#808080") +
- labs(y = NULL,
- x = NULL) +
- guides(color = guide_legend(ncol = 1, override.aes = list(linetype = 0, size = 3, alpha = 1))) +
- 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")) +
- theme(
- plot.margin = margin(t = 0, unit = "cm"),
- plot.background = element_blank(), panel.background = element_rect(fill = "grey92", color = NA),
- panel.border = element_blank(), legend.background = element_rect(fill = "transparent", color = NA),
- legend.key = element_rect(fill = "transparent", color = NA), legend.title = element_blank(),
- strip.background = element_rect(fill = "transparent", color = NA),
- panel.grid.major = element_line(color = "#FFFFFF"), panel.grid.minor = element_line(color = "#FFFFFF", size = 0.25),
- axis.ticks = element_line(color = "grey20"), axis.line = element_blank(),
- plot.title = element_text(size = 12, hjust = 0),
- plot.subtitle = element_text(size = 12, hjust = 0),
- plot.caption = element_text(size = 12, color = "#212121"),
- axis.title = element_text(size = 12, face = "plain"), axis.text = element_text(size = 12, face = "plain", color = "grey30"),
- legend.position = "right",
- legend.text = element_text(size = 12), strip.text = element_text(size = 12, face = "plain"),
- legend.margin = margin(t = 0, unit = "cm"),
- ) +
- scale_y_continuous(breaks = seq(0,10,1), minor_breaks = seq(0,10,1), limits = c(0, 10), expand = c(0, 0)) +
- scale_x_date(breaks="6 months", minor_breaks="1 month", expand = c(0, 0)) +
- geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=5, shape=5) +
- geom_point(data = PreviousResults, color = c("#00529F", "#D82A20", "#000000", "#098137", "#FDE401", "#32DAC3","#EF4A42","#00AEEF"), size=4.5, shape=18)
- dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement