I'm struggling to understand how to use geom_smooth()
together with gganimate
.
The reprex is available below. When running the plot statement excluding the transition_time
function I visualize the static plot as expected.
The issue appears when I try to make this plot dynamic using the transition_time()
as well as shadow_mark()
for letting the points to remain. The following error arises:
Error in
$<-.data.frame
(*tmp*
, "group", value = ""): replacement has 1 row, data has 0
library(readr)
library(tidyr)
library(ggplot2)
library(gifski)
library(gganimate)
library(dplyr)
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()
))
tst %>%
select(date, house, PP, PSOE, UP, Cs, VOX) %>%
gather(key = partido, voto, PP, PSOE, UP, Cs, VOX) %>%
ggplot(aes(x = date, y = voto, color = partido)) +
geom_point() +
geom_smooth(method = 'loess', formula = 'y ~ x', se = FALSE) +
transition_time(time = date) + shadow_mark()
Error in `$<-.data.frame`(`*tmp*`, "group", value = ""): replacement has 1 row, data has 0 In addition: There were 16 warnings (use warnings() to see them)
Created on 2019-04-10 by the reprex package (v0.2.1)
What I'm expecting is something similar to this (source):