DescriptionOpinion polling for the 2024 Moldovan presidential election.svg |
ggplot.R
|
Sys.setlocale("LC_TIME", "English")
library(ggplot2)
library(anytime)
library(tidyverse)
library(svglite)
library(Rcpp)
polls <- read.table("CAT.csv", header=T, sep=",", fileEncoding="UTF-8", stringsAsFactor=F)
polls$polldate <- as.Date(anydate(polls$polldate))
spansize <- 0.75 # general smoothing parameter for trend line
nnum <- 600 # number of points used for trendline (resolution)
startdate <- '2020-11-01' # date of previous election
enddate <- '2024-10-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]
party6 <- colnames(polls)[7]
party7 <- colnames(polls)[8]
party8 <- colnames(polls)[9]
# define party colors (taken from https://en.wikipedia.org/wiki/Category:Germany_political_party_colour_templates)
col1 <- '#FFDE00'
col2 <- '#8B0000'
col3 <- '#054D7F'
col4 <- '#097F66'
col5 <- '#D2222A'
col6 <- '#71004B'
col7 <- '#5CBB72'
col8 <- '#5FE0E6'
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_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))+
geom_point(aes_string(x='polldate',y=party7),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col7,transp),fill=paste0(col7,transp))+
geom_point(aes_string(x='polldate',y=party8),size=ifelse(polls$polldate==startdate | polls$polldate==enddate,3,1.5),shape=ifelse(polls$polldate==startdate | polls$polldate==enddate,23,21),color=paste0(col8,transp),fill=paste0(col8,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)+
geom_smooth(aes_string(x='polldate',y=party7,color=shQuote('col7')),method="loess",span=0.8,n=nnum,se=FALSE)+
geom_smooth(aes_string(x='polldate',y=party8,color=shQuote('col8')),method="loess",span=spansize,n=nnum,se=FALSE)+
scale_y_continuous(labels = function(x) paste0(x, "%"),limits=c(0,40))+ # 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','col7','col8'),
labels = c(party1,party2,party3,party4,party5,party6,party7,party8),
values = c('col1'=col1,'col2'=col2,'col3'=col3,'col4'=col4,'col5'=col5,'col6'=col6,'col7'=col7,'col8'=col8))+
# 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")
|
CAT.csv
|
polldate,Sandu,Dodon,Usatii,Ceban,Voronin,Chicu,SOR candidates,Vlah
2020-11-01,36.16,32.61,16.90,,,,6.49,
2024-05-27,30.4,14.3,3.9,3.0,2.5,6.1,,1.6
2024-05-19,35.2,16.4,4.7,5.9,5.3,5.7,,4.1
2024-04-13,35.1,15.8,3.9,5.4,4.6,5.6,1.7,4.5
2024-03-24,27.9,13.3,3.0,4.4,2.6,5.7,,4.3
2024-02-22,30,24,4,6,,5,4,4
2024-02-12,29.8,14.8,4.4,4.5,1.6,5.0,8.5,4.8
2024-01-30,24.1,,,,,,,
2023-12-16,30.1,24.0,4.1,8.1,2.7,6.1,,5.6
2023-09-24,27.8,16.0,6.0,6.0,4.3,5.9,,4.9
2023-08-23,29.4,18.1,5.1,5.6,1.5,4.1,3.2,
2023-07-28,32.6,17.8,4.3,5.3,4.3,2.9,6.3,
2023-06-19,37.9,14.2,4.7,6.2,4.7,5.0,6,
2023-05-19,28.5,23.9,3.8,8.3,3.7,7.2,2.7,
2023-05-08,30.4,18.3,4.5,6.7,,5.9,12.2,2.8
2023-04-13,38.3,18.4,3.2,6.2,3.2,4.9,4.7,
2023-03-26,29.4,17.6,1.7,7.2,0.9,5.4,9.2,3.1
2023-03-03,31.8,17.8,2.0,4.7,3.5,3.3,3.8,2.1
2023-02-23,25.2,20.4,3.4,8.2,4.9,5.4,4.3,
2023-01-26,28.2,17.6,3.5,8.5,3.9,3.9,6.6,1.8
2022-12-26,27.2,24.1,0.5,10.0,0.6,5.0,13.6,0.1
2022-11-29,26.9,19.6,2.2,8.8,4.0,7.1,2.8,
2022-11-10,27.3,15.4,2.9,7.1,4.3,,9.1,
2022-10-11,34.1,19.1,3.3,7.1,5.2,2.5,6.3,1.2
2022-07-18,24.4,25.4,2.8,9.2,7.1,5.2,3.6,
| |