thomasp85/gganimate

Problem with transition_reveal() in 1.0.8

mgei opened this issue · 1 comments

mgei commented

Here's a minimal example and below are the outputs from the different versions of gganimate.

library(dplyr)
library(gganimate)

packageVersion("gganimate")

set.seed(1)
dat <- tibble(date = seq(from = as.Date("2010-01-01"), length.out = 100, by = "day"),
              ret = rnorm(sd = 0.2, n = 100),
              perf = cumprod(1+ret)-1,
              event = ifelse(date == as.Date("2010-03-01"), "Day X", NA_character_))


p <- ggplot(dat, aes(x = date, y = perf)) +
  geom_line() +
  geom_vline(data = dat %>% filter(!is.na(event)),
             aes(xintercept = date)) +
  geom_label(data = dat %>% filter(!is.na(event)),
             aes(label = event))

p <- p +
  transition_reveal(date)

animate(p, nframes = 50)

gganimate 1.0.7 gives this (desired output):
vers07

gganimate 1.0.8 gives this:
vers08

I installed the previous version using versions package.

The reason for using data = in geom_label is that with gganimate there NA labels are printed "NA" (while in the static ggplot output, the NA labels are not shown).

> R.version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          2.1                         
year           2022                        
month          06                          
day            23                          
svn rev        82513                       
language       R                           
version.string R version 4.2.1 (2022-06-23)
nickname       Funny-Looking Kid   

EDIT: Here's a workaround:

p <- ggplot(dat, aes(x = date, y = perf)) +
  geom_line() +
  geom_vline(aes(xintercept = ifelse(!is.na(event), date, Sys.Date()))) +
  geom_label(aes(label = event))

p <- p +
  transition_reveal(date) +
  gganimate::shadow_wake(wake_length = 1, 
                         exclude_layer = 1,
                         wrap = F, size = F, alpha = F)

animate(p, nframes = 50)

This has been fixed in the dev version