Skip to content

Instantly share code, notes, and snippets.

@jlopezper
Last active April 13, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlopezper/c6f3e8a0966fb1af2708c5aa87e83547 to your computer and use it in GitHub Desktop.
Save jlopezper/c6f3e8a0966fb1af2708c5aa87e83547 to your computer and use it in GitHub Desktop.
# load packages
library(readr)
library(tidyr)
library(ggplot2)
library(gifski)
library(gganimate)
library(dplyr)
# load data
tst <- readr::read_csv("https://elpais.com/especiales/2019/elecciones-generales/encuestas-electorales/ficheros/tabla.csv?1554807308",
col_types = cols(
date = col_date(format = "%d/%m/%y"),
house = col_character(),
sample = col_double(),
turnout = col_double(),
PP = col_double(),
PSOE = col_double(),
UP = col_double(),
Cs = col_double(),
ERC = col_double(),
PDC = col_double(),
PNV = col_double(),
PAC = col_double(),
BIL = col_double(),
CC = col_double(),
VOX = col_double(),
COM = col_double()
))
# preparing and plotting
gga <-
tst %>%
select(date, house, PP, PSOE, UP, Cs, VOX) %>%
gather(key = partido, voto, PP, PSOE, UP, Cs, VOX) %>%
filter(!is.na(voto) & house %in% c('cis', 'celeste-tel', 'metroscopia',
'gad3', 'invymark', 'sigma dos')) %>%
ggplot(aes(x = date, y = voto, color = partido)) +
geom_line() +
geom_point() +
theme_minimal() +
scale_color_manual(values=c("#EE9A00", "#27408B", "#8B1A1A","#A020F0", "#458B00")) +
labs(x = 'Date', y = 'Vote estimation (%)', color = 'Political Party') +
theme(legend.position = "bottom",
axis.text = element_text(size=12),
axis.title = element_text(size=14, face="bold"),
legend.title = element_text(size = 12, face="bold"),
legend.text = element_text(size = 12),
strip.text = element_text(size = 14)) +
facet_wrap(~ house) +
transition_reveal(date) +
shadow_mark()
# plot
gganimate::animate(gga, height = 550, width = 900, nframes = 150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment