4

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):

from github link

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
jlopezper
  • 41
  • 6

1 Answers1

-1

Try this out:

tmp <- tst %>% 
  select(date, house, PP, PSOE, UP, Cs, VOX) %>% 
  gather(key = partido, voto, PP, PSOE, UP, Cs, VOX) %>%
  filter(!is.na(voto))

ggplot(tmp, aes(x = date, y = voto, color = partido)) + 
  geom_smooth(method = "loess", formula = 'y ~ x', se = FALSE) + 
  geom_point(aes(group = rev(seq_along(date)))) +
  transition_reveal(rev(seq_along(date))) + shadow_mark()
Raoul Duke
  • 435
  • 3
  • 13
  • Thanks @Raoul Duke! I think this is getting close what I expected. Just in case, do you know how to make them appear at the same time? – jlopezper Apr 11 '19 at 07:47
  • My bad here @Raoul Duke, by "them" I mean all plot lines moving forward at the same time and not one after the other – jlopezper Apr 11 '19 at 14:02
  • No problem, I got your intent just after making the comment. I am able to get just the points using `ggplot(tmp, aes(x = date, y = voto, color = partido, group = partido)) + geom_point() + transition_components(date) + shadow_mark()`. Since path layers are not supported by `transition_components`, I'm not sure how to add the smooth. – Raoul Duke Apr 11 '19 at 14:30
  • 11
    I'm trying to replicate this, but getting ```Error in `$<-.data.frame`(`*tmp*`, "group", value = "") : replacement has 1 row, data has 0 In addition: There were 13 warnings (use warnings() to see them)``` – jO. May 20 '19 at 19:23