gganimate中有许多(> 50)状态的问题 [英] Problem with many (>50) states in gganimate

查看:128
本文介绍了gganimate中有许多(> 50)状态的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gganimate为涵盖90年的数据集创建GIF,即我想让GIF贯穿90个州/年.但是,gganimate似乎只能处理少于50个状态.

I'm trying to create a GIF using gganimate for a data set covering 90 years, i.e. I want to have a GIF running through 90 states/years. However, it seems like gganimate is only able to deal with less than 50 states.

所以这是一个例子:

library(tidyverse)
# devtools::install_github('thomasp85/gganimate')
library(gganimate)

df = expand.grid(  x = 1,
                   y = c(2,3),
                year = 1670:1760) %>% mutate( z = 0.03* year,
                                              u = .2 * year)

这一切都可以正常工作49年:

this all works fine for 49 years:

ggplot(data=df %>% filter(., year %in% 1670:1719) , aes()) + 
  geom_point( aes(x = x, y = y, fill = z, size = u), shape = 21 ) + 
  labs( title = 'Year: {closest_state}') +
  enter_appear() +
  transition_states(year, transition_length = 1, state_length = 2) 

但是,当我包含50(或更多)年时,它会变得很奇怪:

Yet, it gets weird when I include 50 (or more) years:

ggplot(data=df %>% filter(., year %in% 1670:1720) , aes()) + 
  geom_point( aes(x = x, y = y, fill = z, size = u), shape = 21 ) + 
  labs( title = 'Year: {closest_state}') +
  enter_appear() +
  transition_states(year, transition_length = 1, state_length = 2) 

如何创建90年的GIF?欢迎任何想法!
我还是gganimate的新手,我是否错误地使用了transition_states?

How can I create a GIF for all 90 years? Any ideas are welcome!
I'm still new to gganimate, am I using transition_states incorrectly?

推荐答案

这与gganmiate使用固定数量的100帧动画有关.长达50年(请注意1670:1719的长度为50,而不是49),这是可以的,但是如果要绘制更多的年份,则需要更多的帧.您可以通过显式调用animate()来控制帧数.

This has to do with the fact that gganmiate uses a fixed number of 100 frames for the animation. For up to 50 years (note that 1670:1719 has length 50, not 49), this is alright, but if you want to plot more years, you need more frames. You can control the number of frames by calling animate() explicitly.

对于您的示例,这意味着您应该首先将绘图存储在变量中:

For your example, this means that you should first store your plot in a variable:

p <- ggplot(df) + 
      geom_point(aes(x = x, y = y, fill = z, size = u), shape = 21) + 
      labs( title = 'Year: {closest_state}') +
      enter_appear() +
      transition_states(year, transition_length = 1, state_length = 2)

然后您可以通过键入以下任意一个来开始动画

You can then start the animation by typing any of the following

p
animate(p)
animate(p, nframes = 100)

这三行等效.第一个是您在示例中所做的:这将隐式调用animate()来渲染动画.第二行显式调用animate(),第三行还将显式设置帧数为100.由于nframes = 100是默认值,因此最后一行与其他行相同.

These three lines are equivalent. The first one is what you did in your example: this will implicitly call animate() to render the animation. The second line makes the call to animate() explicit and the third also explicitly sets the number of frames to 100. Since nframes = 100 is the default value, also this last line does the same as the others.

为了使动画有效,您需要设置更多的帧数. 100帧可以工作50年,因此在整个数据帧中,182帧应该可以工作91年.同样,以下两行相同:

In order to make the animation work, you need to set a higher number of frames. 100 frames worked for 50 years, so 182 frames should work for the 91 years in your full data frame. Again, the following two lines are the same:

animate(p, nframes = 182)
animate(p, nframes = 2 * length(unique(df$year)))

现在它可以工作了:

我不确定为什么您需要的帧数是年数的两倍,但是在阅读了transition_states()

I don't know for certain why you need twice the number of frames as you have years, but after reading the following statement from the documentation on transition_states()

然后在定义的状态之间进行补间,并在每个状态下暂停.

It then tweens between the defined states and pauses at each state.

我猜想一帧用于两年之间的过渡,一帧用于表示给定年份的日期.

I would guess that one frame is used for the transition between two years and one frame is used to represent the date for a given year.

这意味着您实际上需要的帧数少于年数的两倍,因为上一年之后的过渡不需要任何帧.实际上,gganimate()对于nframes = 100nframes = 182的输出分别是:

This would mean that you actually need one frame less than twice the number of years, because there is no frame needed for the transition after the last year. Indeed, the output from gganimate() for nframes = 100 and nframes = 182, respectively, is:

Frame 99 (100%)
Finalizing encoding... done!

Frame 181 (100%)
Finalizing encoding... done!

因此,如果我的猜测正确的话,它确实可以准确地创建预期的帧数.

So it is indeed creating exactly the number of frames to be expected if my guess was correct.

这篇关于gganimate中有许多(> 50)状态的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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