在gganimate中以不同的速度显示不同的时间元素 [英] Display different time elements at different speeds in gganimate

查看:18
本文介绍了在gganimate中以不同的速度显示不同的时间元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此答案中的代码, (v0.3.0) 于 2019 年 6 月 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.

解决方案

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)

这篇关于在gganimate中以不同的速度显示不同的时间元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆