解説Opinion polling for the 2024 Russian presidential election.svg |
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.55 # general smoothing parameter for trend line
nnum <- 600 # number of points used for trendline (resolution)
startdate <- '2023-11-27' # date of previous election
enddate <- '2024-03-18' # (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-two.iwiki.icu/wiki/Category:Germany_political_party_colour_templates)
col1 <- '#808080'
col2 <- '#1B3E89'
col3 <- '#CC1111'
col4 <- '#4488CC'
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')),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)+
scale_y_continuous(labels = function(x) paste0(x, "%"),limits=c(0,88))+ # add %, manual limits on y-axis
scale_x_date(limits = as.Date(c(startdate,enddate)), date_minor_breaks = "7 days", date_breaks = "1 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=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,Putin,Nadezhdin,Kharitonov,Slutsky,Davankov
2024-03-18,87.28,,4.31,3.20,3.85
2024-03-17,73,,4,2,17
2024-03-17,88,,5,2,4
2024-03-17,87,,5,3,4
2024-03-12,55,,2,2,3
2024-03-10,56,,5,3,9
2024-03-10,55,,5,4,4
2024-03-06,58,,4,4,4
2024-03-06,56,,4,4,3
2024-03-05,57,,3,1,3
2024-03-05,61,,6,3,5
2024-03-05,56,,3,2,6
2024-03-04,66,,5,4,6
2024-03-03,60,,3,2,5
2024-03-03,72,,2,3,2
2024-02-25,71,,3,2,2
2024-02-22,59,,4,3,4
2024-02-22,53,,2,2,2
2024-02-21,61,,2,2,5
2024-02-18,73,,2,3,2
2024-02-18,62,,6,3,4
2024-02-15,61,,3,2,3
2024-02-14,56,2,2,2,2
2024-02-11,74,,3,3,2
2024-02-08,57,,3,3,4
2024-02-07,63,6,,,
2024-01-30,62,8,3,3,1
2024-01-30,61,6,2,1,
2024-01-28,70,7,4,3,0.3
2023-12-28,74,1,4,3,
2023-11-29,69,1,,,
| |