更新新的gganimate API
gganimate 已重新设计为 new API。现在可以使用下面的代码对框架标题进行动画处理。 state_length 和 transition_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: 的值之前: