DescriptionOpinion polling for the 2026 Russian legislative election.svg |
English: Opinion polling for the 2026 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)
polls <- read.table("CAT.csv", header=T, sep=",", fileEncoding="UTF-8", stringsAsFactor=F)
polls$polldate <- as.Date(anydate(polls$polldate))
spansize <- 0.8 # general smoothing parameter for trend line
startdate <- '2021-09-19' # date of previous election
enddate <- '2026-09-20' # (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]
# define party colors (taken from https://en.wikipedia.org/wiki/Category:Germany_political_party_colour_templates)
col1 <- '#2E4EA4'
col2 <- '#CC1111'
col3 <- '#4488CC'
col4 <- '#FFC003'
col5 <- '#0AD1C9'
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))+
# 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')),span=spansize,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party2,color=shQuote('col2')),span=spansize,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party3,color=shQuote('col3')),span=spansize,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party4,color=shQuote('col4')),span=spansize,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party5,color=shQuote('col5')),span=spansize,se=FALSE)+
scale_y_continuous(labels = function(x) paste0(x, "%"),limits=c(1,50))+ # 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'),
labels = c(party1,party2,party3,party4,party5),
values = c('col1'=col1,'col2'=col2,'col3'=col3,'col4'=col4,'col5'=col5))+
# 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=10)
# workaround since svglite doesn't properly work in Wikipedia
aaa=readLines("polls.svg",-1)
bbb <- gsub(".svglite ", "", aaa)
writeLines(bbb,"polls.svg")
|
CAT.csv
|
polldate,ER,KPRF,LDPR,SRZP,NL
2021-12-05,32,16,9,5,6
2021-12-05,28.3,18.2,7.7,6.9,8.6
2021-12-01,26,13,8,4,5
2021-11-28,31,15,9,5,6
2021-11-28,28.6,18,7.8,6.9,8.1
2021-11-21,30,15,8,5,6
2021-11-21,29.2,18.6,7.6,6.9,8.2
2021-11-14,30,15,9,5,6
2021-11-14,28.9,18.7,7.9,6.5,8.1
2021-11-07,31,15,10,6,7
2021-11-07,30.2,17.7,7.4,6.6,7.8
2021-10-31,31,16,9,6,6
2021-10-31,28.6,19.4,8.1,7.1,8.7
2021-10-27,29,14,8,5,5
2021-10-24,29,17,8,6,6
2021-10-24,29.8,19.4,7.3,6.9,8.5
2021-10-17,30,18,9,6,6
2021-10-17,30.1,19.8,7.7,6,8.6
2021-10-10,29.8,20.2,7.5,6.3,8.6
2021-10-03,29.8,20.2,7.3,6.1,8.4
2021-09-26,29.5,23.3,7.6,6.4,8.4
2021-09-19,49.82,18.93,7.55,7.46,5.32
| |