Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

r - Display different time elements at different speeds in gganimate

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...