2

Using the code from this answer, How to make dots in gganimate appear and not transition, as a MWE, say we have this gganimate:

library(ggplot2)
library(gganimate)
a <- ggplot(airquality, aes(Day, Temp, 
                            group = interaction(Month, Day))) +
  geom_point(color = 'red', size = 1) +
  transition_time(Month) +
  shadow_mark(colour = 'black', size = 0.75) +
  enter_fade()  
animate(a, nframes = 100)

or

animate(a, fps=5)

Is it possible to control the speed of each Month (time element)? For example, display Month 5 very quickly, ..., Month 9 very slowly.

bill999
  • 2,147
  • 8
  • 51
  • 103
  • 1
    Just use a custom variable for `transition_time`. – M-- Jun 02 '19 at 03:37
  • I want to be able to label based on the transition time variable—`labs(title = 'Month: {frame_time}')`. Would using a custom variable for transition_time allow this? – bill999 Jun 02 '19 at 03:50
  • 1
    I think so. Off the top of my head, you can have the whole May for month 5 and just couple days of September for month 9 and then format the label to show only the month and not the whole date. – M-- Jun 02 '19 at 03:51
  • Isn't the way the time works is that it displays each time period (eg., month equally)? So let's say I had 100 months and I wanted to display the 50th month 10x slower. One way I thought I could do this—and maybe this is what you are saying—is to create a new variable that is, say 1 for month 1, 2 for month 2, 49 for month 49, 50 for month 50, 60 for month 51, 61 for month 62, and so on. I think this would slow it down how I want. But then the issue is that for the label, the {frame_time}, I think, would be the new variable. Can I label it with the month the new variable corresponds to? – bill999 Jun 02 '19 at 18:27

1 Answers1

4

This is my rudimentary try by making a helper column which can be used as our transition_time to show how we can have different time step but desired labels.

You can later spend some more time to make a better *timestep columns which is more sophisticated and precisely meets your needs.

The main idea/point here is that we can use functions on the frame_time to get the labels as needed while the transition_time can be manipulated.

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

g <- airquality %>%
  group_by(Month) %>%
  mutate(timestep = if_else(Month==5, ((1:n())-1)/2 + Month, 15 + Month)) %>%
  ggplot(aes(Day, Temp, group = interaction(Month, Day))) +
  geom_point(color = 'red', size = 1) +
  transition_time(timestep) +
  shadow_mark(colour = 'black', size = 0.75) +
  enter_fade() +
  labs(title = 'Month: {if_else(frame_time<21,5, ceiling(frame_time-15))}')

animate(g, nframes = 100)

Created on 2019-06-02 by the reprex package (v0.3.0)

M--
  • 25,431
  • 8
  • 61
  • 93
  • This is brilliant! One quick follow-up which you should feel free to ignore: Is there another function similar to `if_else` that would allow multiple if's? For example, if I wanted it to display some custom text, for example "Blue" for 5, "Red" for 6, "Black" for 7, etc. It would be easy to do this for just two conditions using `if_else`, but it is unclear to me what to do if there were, say, thousands of conditions. – bill999 Jun 03 '19 at 21:47
  • 1
    @bill999 `dplyr::case_when()`. I think that's what you are looking for. – M-- Jun 03 '19 at 22:07