3

I am playing around with gganimate and I do believe it is acting slightly funky when it comes to labels (I've basically followed this example).

I am generating the following .gif with this code snippet (you can find the data here, didn't want post length to explode).

library(gganimate)
library(dplyr)

df <- read.csv("https://pastebin.com/raw/QvhdVqwM", stringsAsFactors = FALSE) %>% 
  mutate(date = as.Date(date))

countries_anim <- df %>%
  filter(country_code == "de") %>% 
  ggplot(aes(date, value, colour = city_name)) +
  geom_line() +
  geom_segment(aes(xend = max(date) - 30, yend = value), linetype = 2,
               colour = "grey") +
  geom_text(aes(x = max(date) - 29, label = city_name), hjust = 0) +
  theme(legend.position = "bottom") +
  guides(colour = guide_legend(title.position = "top")) +
  transition_reveal(date)

n_days <- as.integer(max(df$date) - min(df$date))

anim <- animate(plot = countries_anim, duration = 10,
                      renderer = gifski_renderer(file = 'figures/de.gif'))

Everything works pretty well except one minor annoyance: at the very beginning of the animation, some annotations (which are supposed to follow time series trend) get permanently printed in the plot area. I've tried to change renderer but the issue seems to be completely uncorrelated.

enter image description here

I am not that versed on gganimate internals and I'm wondering how I could go debugging the issue.

anddt
  • 1,589
  • 1
  • 9
  • 26
  • Can you create a simple reproducible example so we can better help? – caldwellst May 29 '20 at 12:48
  • My bad, just referenced data without importing it in the snippet. Just updated the snippet so it's runnable, everything should be fine now – anddt May 29 '20 at 13:08

1 Answers1

0

Been struggling in debugging this for a few hours but I seem to have found a solution. Apparently animated annotations are affected by how data is ordered; as you can see in the example below, my dataset was arranged in descending order (by date). Changing the order seems to help annotations to behave better:

library(dplyr)
library(gganimate)
library(ggplot2)

df <- read.csv("https://pastebin.com/raw/QvhdVqwM", stringsAsFactors = FALSE) %>% 
  mutate(date = as.Date(date))

# Dates are in descending order
df %>%
  filter(country_code == "de") %>% 
  head %>% 
  as_tibble()

#> # A tibble: 6 x 10
#>   big_change change_from_pre… date       type  region_id value city_name
#>   <lgl>                 <int> <date>     <chr> <chr>     <int> <chr>    
#> 1 FALSE                    -3 2020-05-28 one_… de-berlin    28 Berlin   
#> 2 FALSE                     3 2020-05-28 one_… de-hambu…    32 Hamburg  
#> 3 FALSE                     2 2020-05-28 one_… de-rhine…    31 Rhine-Ru…
#> 4 FALSE                     2 2020-05-27 one_… de-berlin    32 Berlin   
#> 5 FALSE                    -3 2020-05-27 one_… de-hambu…    28 Hamburg  
#> 6 FALSE                     3 2020-05-27 one_… de-rhine…    28 Rhine-Ru…
#> # … with 3 more variables: country_code <chr>, note <chr>, country <chr>

countries_anim <- df %>%
  filter(country_code == "de") %>% 
  arrange(date) %>%  # arranging by date solves the problem.
  ggplot(aes(date, value, colour = city_name)) +
  geom_line() +
  geom_segment(aes(xend = max(date) - 30, yend = value), linetype = 2,
               colour = "grey") +
  geom_text(aes(x = max(date) - 29, label = city_name), hjust = 0) +
  theme(legend.position = "bottom") +
  guides(colour = guide_legend(title.position = "top")) +
  transition_reveal(date)

country_anim <- animate(plot = countries_anim, duration = 10,
                        renderer = gifski_renderer(file = 'figures/countries.gif'))

I am not quite sure why this happens as data order doesn't really upset gpplot2.

enter image description here

anddt
  • 1,589
  • 1
  • 9
  • 26