【问题标题】:Using tweenr and gganimate with bar plots将 tweenr 和 gganimate 与条形图一起使用
【发布时间】:2019-06-07 14:16:48
【问题描述】:

如何使用 tweenr 和 gganimate 在下面的两个条形图之间创建非常流畅的动画?

library(tweenr)
library(gganimate)
library(tidyverse)

df <- tibble(
  decile = c("lowest", "second", "third", "lowest", "second", "third"),
  change = c(1, 2, -0.5, -2, -3, 4),
  year = c(2001L, 2001L, 2001L, 2002L, 2002L, 2002L)
)

df2001 <- filter(df, year == 2001)
df2002 <- filter(df, year == 2002)

ggplot(df2001, aes(x = decile, y = change)) +
  geom_col() +
  scale_y_continuous(limits = c(-5, 5)) +
  theme_minimal()

ggplot(df2002, aes(x = decile, y = change)) +
  geom_col() +
  scale_y_continuous(limits = c(-5, 5)) +
  theme_minimal()

【问题讨论】:

    标签: r gganimate


    【解决方案1】:

    编辑:更改 transition_statesease_aes 以在过渡上花费更多时间,增加字体大小,并更改 animate 术语以延长持续时间并减慢移动速度。

    a <- ggplot(df, aes(x = decile, y = change/2, 
                        height = change, width = 0.9, group = decile)) +
      geom_tile() +
      scale_y_continuous(limits = c(-5, 5), name = "change") +
      theme_minimal(base_size = 16) +
      transition_states(year, wrap = T, transition_length = 10, state_length = 1) +
      ease_aes("cubic-in-out")
    
    animate(a, fps = 30, duration = 10, width = 500, height = 300)
    # Use up to two of fps, nframes, and duration to define the
    #   length and frame rate of the animation.
    


    请注意,我在上面使用了geom_tile,因为geom_col 在转换时产生了这种非常规的行为。我怀疑绘制的geom_col 没有区分其基线和范围,而是区分其最小值和最大值,从而导致“滑动”动画。 (好奇其他人是否看到了更简单的解决方法。)

    a <- ggplot(df, aes(x = decile, y = change)) +
      geom_col() +
      ...
    

    【讨论】:

    • 一些问题:你为什么从 geom_col 改为 geom_tile?为什么图形本身的分辨率这么低?即轴标签。最重要的是,你如何做一个缓慢、流畅的版本。如果将 fps 降低到 10,则动画很慢,但很生涩。这就是为什么我认为需要 tweenr 来创建中间帧。
    • 已在上面编辑。您可以使用主题控制字体大小。您可以在animate 调用中控制分辨率,您还可以根据需要将分辨率设置为尽可能长的持续时间和尽可能高的帧速率。 gganimate 在引擎盖下使用了 tweenr,均由受人尊敬的 Thomas Pedersen:github.com/thomasp85/gganimate/blob/master/DESCRIPTION
    • 谢谢。我仍然不知道如何从动画中“控制分辨率”。尺寸?是的。分辨率???
    • 分辨率,你说的是dpi吗?我不相信 GIF 有办法控制这一点,而只是总尺寸。如果您使用其他设备,例如 av_renderer(),例如制作 mp4,您可以控制 dpi,但语法将取决于渲染器。查看animate 的选项以了解更多信息...
    • 静态ggplot的文本清晰度远大于animate的动画gif输出。我会按照你的建议调查 av_renderer()。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2018-06-17
    • 2023-03-17
    • 2019-08-17
    • 2019-03-20
    • 2011-01-02
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多