【问题标题】:Change label of gganimate frame title更改 gganimate 框架标题的标签
【发布时间】:2016-09-20 16:57:33
【问题描述】:

浏览gganimate 的选项,可以在调用gg_animate() 渲染动画序列时设置,似乎没有更改帧标题的选项,以便让观察者更清楚什么是框架所基于的参数。

换句话说,假设frame = year 在一个层中:如何使框架的标题为year: #### 其中#### 是当前框架的年份?我错过了什么还是gganimate 库的限制?

您将如何通过变通方法获得相同的结果? 谢谢你的建议。

【问题讨论】:

    标签: r animation ggplot2 gganimate


    【解决方案1】:

    更新新的gganimate API

    gganimate 已重新设计为 new API。现在可以使用下面的代码对框架标题进行动画处理。 state_lengthtransition_length 设置在给定“状态”(这里指的是给定值 cyl)和状态之间转换的相对时间量。 closest_state 表示在状态之间转换时(在这种情况下是 cyl 的整数值),应该显示最接近当前转换值的 cyl 值(例如,因为 cyl 可以是 4、6、或 8,例如 4 和 5 之间的值显示为 4,而 5 和 6 之间的值显示为 6):

    p = ggplot(mtcars, aes(wt, mpg)) + 
      geom_point() + 
      transition_states(cyl, transition_length=1, state_length=30) +
      labs(title = 'Cylinders: {closest_state}')
    
    animate(p, nframes=40)
    

    gganimate可以通过运行devtools::install_github('thomasp85/gganimate')github安装

    vignette 提供了有关如何使用新 API 的更多详细信息。

    原答案

    框架的子集值附加到任何预先存在的标题。因此,您可以添加带有解释性文本的标题。例如:

    library(gganimate)
    
    p = ggplot(mtcars, aes(wt, mpg, frame=cyl)) + geom_point() + 
        ggtitle("Cylinders: ")
    
    gg_animate(p)
    

    正如您在下面的 GIF 中所见,前缀“Cylinders:”现在添加到标题中 cyl: 的值之前:

    【讨论】:

    • 这正是我所需要的。谢谢!你能指出一些关于 gganimate 的其他资源吗?
    • 如果使用 transition_time 而不是 transition_states 你可以将 '{closest_state}' 换成 '{frame_time}'
    • @eipi10 您可能想在此处删除评论中的第一个教程链接。
    • 谢谢@Jojo!以前的链接没有发生过这种情况!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多