Search code examples
rgganimate

How to animate geom_segment in gganimate chart


As a slight follow-up question to this, I'm having a bit of trouble with animating a geom_segment together with all of the other contents in my chart. I understand that I can animate the geom_text and geom_rect using an ifelse, but I'm struggling to think of how I can use the ifelse to animate the geom_segment so that it only appears from the frame when 2023-07-10 is reached. Any suggestions?

library(tidyverse)
library(gganimate)

data <- structure(list(Date = structure(c(19541, 19542, 19543, 19544, 
                                          19545, 19548, 19549, 19550, 19551, 19552, 19555, 19556, 19557, 
                                          19558, 19559, 19562), class = "Date"), Value = c(18.7414, 18.6471, 
                                                                                           18.7749, 19.1089, 18.867, 18.7948, 18.4902, 18.135, 17.9282, 
                                                                                           18.1077, 18.0356, 17.8571, 17.8682, 17.9244, 17.96, 17.9832)), row.names = c(NA, 
                                                                                                                                                                        -16L), class = c("tbl_df", "tbl", "data.frame"))


p <- ggplot(mutate(data, reveal_col = row_number()),
            aes(x = Date, y = Value)) +
  geom_rect(aes(xmin = max(Date) - days(3), xmax = max(Date),
                ymin = -Inf, ymax = Inf, 
                fill = ifelse(reveal_col > nrow(data) - 1, NA, "#FF000060")),
            na.rm = TRUE) +
  geom_line(linewidth = 1) +
  geom_text(aes(x = Date, label = sprintf("%.2f", Value)),
            hjust = 0,show.legend = F,
            fontface='bold',color='#16161E') +
  geom_text(aes(x = as.Date('2023-07-10'), y = 19, 
                label = ifelse(reveal_col > match(as.Date("2023-07-10"), Date),
                               'I want these words to be animated', "")), 
            hjust = 0, fontface = 'bold', color = '#16161E') +
  geom_segment(aes(x = as.Date('2023-07-10'), y = 18.8, xend = as.Date('2023-07-10'), yend = 19)) +
  theme_light() +
  scale_fill_identity() +
  coord_cartesian(clip = "off") +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.margin = unit(c(1, 1, 1, 1), "lines"), 
        plot.title = element_text(colour = '#16161E'),
        legend.title = element_blank(), legend.position = 'bottom',
        plot.caption = element_text(hjust = 0)) +
  transition_reveal(reveal_col)

animate(p, height = 4, width = 7, units = "in", res = 150)


enter image description here


Solution

  • We could do the same thing, where one of the necessary geom_segment parameters (e.g. x) is NA until the time we want it plotted.

    p <- ggplot(mutate(data, reveal_col = row_number()),
                aes(x = Date, y = Value)) +
      geom_rect(aes(xmin = max(Date) - lubridate::days(3), xmax = max(Date),
                    ymin = -Inf, ymax = Inf, 
                    fill = ifelse(reveal_col > nrow(data) - 1, NA, "#FF000060")),
                na.rm = TRUE) +
      geom_line(linewidth = 1) +
      geom_text(aes(x = Date, label = sprintf("%.2f", Value)),
                hjust = 0,show.legend = F,
                fontface='bold',color='#16161E') +
      geom_text(aes(x = as.Date('2023-07-10'), y = 19, 
                    label = ifelse(Date > as.Date("2023-07-10"),
                                   'I want these words to be animated', "")), 
                hjust = 0, fontface = 'bold', color = '#16161E') +
      geom_segment(aes(x = if_else(Date > as.Date('2023-07-10'), as.Date('2023-07-10'), NA), 
                       y = 18.8, xend = as.Date('2023-07-10'), yend = 19)) +
      theme_light() +
      scale_fill_identity() +
      coord_cartesian(clip = "off") +
      theme(panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            plot.margin = unit(c(1, 1, 1, 1), "lines"), 
            plot.title = element_text(colour = '#16161E'),
            legend.title = element_blank(), legend.position = 'bottom',
            plot.caption = element_text(hjust = 0)) +
      transition_reveal(reveal_col)
    
    animate(p, height = 4, width = 7, units = "in", res = 150, fps = 24)
    

    enter image description here