DescriptionOpinion polling for the 2011 Russian legislative election.svg |
English: Opinion polling for the 2011 Russian legislative 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.49 # general smoothing parameter for trend line
nnum <- 600 # number of points used for trendline (resolution)
startdate <- '2007-12-02' # date of previous election
enddate <- '2011-12-05' # (latest) date of next election
# retrieve party names from CSV
party1 <- colnames(polls)[2]
party2 <- colnames(polls)[3]
party3 <- colnames(polls)[4]
party4 <- colnames(polls)[5]
party5 <- colnames(polls)[6]
party6 <- colnames(polls)[7]
# define party colors (taken from https://wiki.riteme.site/wiki/Category:Germany_political_party_colour_templates)
col1 <- '#2E4EA4'
col2 <- '#CC1111'
col3 <- '#4488CC'
col4 <- '#FFC003'
col5 <- '#00A23D'
col6 <- '#E0B559'
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=5, yend=5), color='#666666bb', linetype='dashed')+ # horizontal line (election threshold 5%)
# add poll points
geom_point(aes_string(x='polldate',y=party1),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col1,transp),fill=paste0(col1,transp))+
geom_point(aes_string(x='polldate',y=party2),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col2,transp),fill=paste0(col2,transp))+
geom_point(aes_string(x='polldate',y=party3),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col3,transp),fill=paste0(col3,transp))+
geom_point(aes_string(x='polldate',y=party4),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col4,transp),fill=paste0(col4,transp))+
geom_point(aes_string(x='polldate',y=party5),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col5,transp),fill=paste0(col5,transp))+
geom_point(aes_string(x='polldate',y=party6),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col6,transp),fill=paste0(col6,transp))+
# add trend lines
# the "span" (smoothing parameter) should be manually changed for individual parties that have less polling data
geom_smooth(aes_string(x='polldate',y=party1,color=shQuote('col1')),method="loess",span=spansize,n=nnum,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party2,color=shQuote('col2')),method="loess",span=spansize,n=nnum,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party3,color=shQuote('col3')),method="loess",span=spansize,n=nnum,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party4,color=shQuote('col4')),method="loess",span=spansize,n=nnum,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party5,color=shQuote('col5')),method="loess",span=spansize,n=nnum,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party6,color=shQuote('col6')),method="loess",span=spansize,n=nnum,se=FALSE)+
scale_y_continuous(labels = function(x) paste0(x, "%"),limits=c(0,65))+ # 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','col4','col5','col6'),
labels = c(party1,party2,party3,party4,party5,party6),
values = c('col1'=col1,'col2'=col2,'col3'=col3,'col4'=col4,'col5'=col5,'col6'=col6))+
# 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,ER,KPRF,LDPR,SR,Yabloko,PD
2011-12-05,49.31,19.19,11.68,13.25,3.43,0.6
2007-12-02,64.30,11.57,8.14,7.74,1.59,
2011-11-20,41,10,9,8,1,0.5
2011-11-20,39,12,10,9,1,0.5
2011-11-13,39,13,11,6,1,0.5
2011-11-12,40,13,9,7,1,0.5
2011-11-10,32,15,9,8,2,1
2011-11-06,42,12,9,6,1,0.5
2011-11-04,43,12,9,5,1,1
2011-10-20,30,11,6,8,,
2011-10-30,40,12,10,5,1,1
2011-10-29,43,14,9,4,1,1
2011-10-23,42,12,10,5,1,0.5
2011-10-22,45,13,7,5,2,0.5
2011-10-16,41,13,10,6,1,0.5
2011-10-17,32,15,8,7,2,0.5
2011-10-09,41,11,10,5,1,0.5
2011-10-02,42,13,9,4,1,0.5
2011-09-15,42,11,10,5,1,1
2011-08-15,42,10,9,4,1,1
2011-07-15,43,10,9,5,1,1
2011-06-15,43,10,9,5,1,1
2011-05-15,45,11,9,4,1,0.5
2011-04-15,45,10,9,5,1,0.5
2011-03-15,46,10,9,5,1,0.5
2011-02-15,46,10,8,5,1,0.5
2011-02-02,35,12,6,3,0.5,0.5
2011-01-15,50,9,8,4,1,0.5
2011-01-13,45,10,6,4,2,1
2010-11-15,52,9,5,4,1,0.5
2010-10-15,50,7,5,5,1,0.5
2010-09-15,51,7,5,4,1,0.5
2010-09-29,41,10,5,5,1,0.5
2010-08-15,50,7,5,5,1,0.5
2010-07-15,44,13,7,3,0.5,0.5
2010-06-15,52,7,5,4,1,0.5
2010-05-15,52,8,5,5,1,1
2010-04-15,52,8,6,4,1,0.5
2010-04-15,38,14,8,5,1,0.5
2010-03-15,53,8,5,4,1,1
2010-02-15,52,8,5,4,1,1
2010-02-15,44,11,7,5,1,0.5
2010-01-15,54,7,4,3,1,0.5
2009-04-15,46,12,8,4,1,0.5
2011-12-04,48.5,19.8,11.42,12.8,4.17,1.1
2011-12-04,45.5,21.0,13.2,14.0,3.6,0.6
2011-12-04,43.1,21.6,13.5,14.4,4.5,0.7
2011-12-04,38.1,23.8,13.7,14.1,5.3,0.9
| |