File:OpinionPolling1992UnitedStatesPresidentialElection.svg

Summary

Description
English:
English: Opinion polling for the 1980 United States Presidential Election using local regressions (LOESS)
Code template: https://gitlab.com/gbuvn1/opinion-polling-graph
ggplot.R
Sys.setlocale("LC_TIME", "English")
library(ggplot2)
library(anytime)
library(tidyverse)
library(svglite)
library(Rcpp)

polls <- read.table("DE.csv", header=T, sep=",", fileEncoding="UTF-8", stringsAsFactor=F)
polls$polldate <- as.Date(anydate(polls$polldate))

spansize <- 0.50           # general smoothing parameter for trend line
nnum <- 600                 # number of points used for trendline (resolution)
startdate <- '1991-12-31'   # date of previous election
enddate <- '1992-11-03'     # (latest) date of next election

# retrieve party names from CSV
party1 <- colnames(polls)[2]
party2 <- colnames(polls)[3]
party3 <- colnames(polls)[4]

# define party colors (taken from https://en.wikipedia.org/wiki/Category:Germany_political_party_colour_templates)
col1 <- '#3333FF'
col2 <- '#E81B23'
col3 <- '#4F990C'

transp <-'55'       # transparency level of points

graph <- ggplot(polls)+
  geom_vline(xintercept = as.Date(startdate), color='#aaaaaabb')+       # vertical line (last election)
  geom_vline(xintercept = as.Date(enddate), color='#aaaaaabb')+         # vertical line (next election)
  geom_segment(aes(x=as.Date(startdate), xend=as.Date(enddate), y=50, yend=50), color='#666666bb', linetype='dashed')+      # horizontal line (election threshold 5%)
  # add poll points
  geom_point(aes(x = polldate, y = .data[[party1]]), size = 1.5, color = paste0(col1, transp), fill = paste0(col1, transp)) +
  geom_point(aes(x = polldate, y = .data[[party2]]), size = 1.5, color = paste0(col2, transp), fill = paste0(col2, transp)) +
  geom_point(aes(x = polldate, y = .data[[party3]]), size = 1.5, color = paste0(col3, transp), fill = paste0(col3, transp)) +
  # add trend lines
  # the "span" (smoothing parameter) should be manually changed for individual parties that have less polling data
  geom_smooth(aes(x = polldate, y = .data[[party1]]), method = "loess", span = spansize, n = nnum, se = FALSE, color = col1) +
  geom_smooth(aes(x = polldate, y = .data[[party2]]), method = "loess", span = spansize, n = nnum, se = FALSE, color = col2) +
  geom_smooth(aes(x = polldate, y = .data[[party3]]), method = "loess", span = spansize, n = nnum, se = FALSE, color = col3) +
  scale_y_continuous(labels = function(x) paste0(x, "%"), limits=c(0,69))+    # add %, manual limits on y-axis
  scale_x_date(limits = as.Date(c(startdate,enddate)), date_minor_breaks = "1 months", date_breaks = "3 months", date_labels = "%b %Y")+    # grid: 1 month, labels: 3 months
  labs(x = "", y = "")+
  scale_color_manual(name="",
                     breaks = c('col1','col2','col3'),
                     labels = c(party1,party2,party3),
					 values = c('col1'=col1,'col2'=col2,'col3'=col3))+
  # legend appearance
  theme(
    axis.text.x = element_text(size = 11),
    axis.text.y = element_text(size = 12),
    axis.title.y = element_text(size = 16),
    legend.position="right",
    legend.key.width=unit(24, "pt"),
    legend.key.height=unit(24, "pt"),
    legend.text = element_text(size=16, margin = margin(b = 5, t = 5, unit = "pt")))

graph + theme()

ggsave(file="polls.svg", plot=graph, width=18, height=8)

# workaround since svglite doesn't properly work in Wikipedia
aaa=readLines("polls.svg",-1)
bbb <- gsub(".svglite ", "", aaa)
writeLines(bbb,"polls.svg")
DE.csv
polldate,Clinton,Bush,Perot
12/30/1991,47,48,
2/9/1992,38,53,
2/20/1992,43,53,
3/12/1992,44,50,
3/22/1992,43,52,
3/29/1992,31,44,16
4/1/1992,25,44,24
4/23/1992,28,38,23
4/22/1992,26,41,25
5/10/1992,29,35,30
5/19/1992,27,35,30
5/20/1992,25,35,35
6/8/1992,25,31,39
6/10/1992,25,33,37
6/14/1992,24,32,34
6/20/1992,24,32,30
6/30/1992,27,33,32
7/7/1992,28,31,33
7/8/1992,28,35,30
7/11/1992,30,33,25
7/13/1992,35,31,24
7/14/1992,40,31,20
7/18/1992,56,34,
7/19/1992,58,29,
7/19/1992,63,33,
7/24/1992,56,36,
8/2/1992,57,32,
8/12/1992,58,35,
8/17/1992,58,35,
8/17/1992,51,35,
8/18/1992,51,40,
8/19/1992,48,43,
8/22/1992,52,42,
8/30/1992,56,36,
9/1/1992,50,45,
9/2/1992,54,39,
9/6/1992,48,43,
9/15/1992,51,42,
9/20/1992,50,40,
9/30/1992,52,35,7
10/4/1992,48,35,9
10/4/1992,53,36,9
10/5/1992,48,34,9
10/5/1992,46,32,10
10/8/1992,49,34,10
10/15/1992,47,34,13
10/21/1992,47,28,19
10/21/1992,45,31,20
10/22/1992,42,34,20
10/22/1992,46,32,18
10/22/1992,38,31,17
10/22/1992,43,31,18
10/23/1992,42,37,17
10/23/1992,42,30,22
10/31/1992,43,36,15
10/23/1992,42,38,16
11/1/1992,44,36,14
Date
Source Own work
Author Ariostos

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Category:CC-Zero#OpinionPolling1992UnitedStatesPresidentialElection.svgCategory:Self-published work
Category:Ariostos' Stuff
Category:Ariostos' Stuff Category:CC-Zero Category:Self-published work