我开始熟悉gganimate,但我想进一步扩展gifs。
例如,我可以在frame中的一个变量上抛出一个gganimate,但是如果我想动画化添加全新层/geoms/变量的过程呢?
下面是一个标准的gganimate示例:
library(tidyverse)
library(gganimate)
p <- ggplot(mtcars, aes(x = hp, y = mpg, frame = cyl)) +
geom_point()
gg_animate(p)但如果我想让礼物有生气呢:
# frame 1
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point()
# frame 2
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(aes(color = factor(cyl)))
# frame 3
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(aes(color = factor(cyl), size = wt))
# frame 4
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point(aes(color = factor(cyl), size = wt)) +
labs(title = "MTCARS")如何才能做到这一点?
发布于 2021-01-26 20:26:39
前面的答案中的gganimate命令在2021年被废弃,无法完成OP的任务。
在Mark代码的基础上,您现在可以简单地创建一个具有多个分层geoms的静态ggplot对象,然后添加gganimate::transition_layers函数来创建一个在静态地块中从一层过渡到另一层的动画。像enter_fade()和enter_grow()这样的推特功能控制着元素在框架中的变化。
library(tidyverse)
library(gganimate)
anim <- ggplot(mtcars, aes(x = hp, y = mpg)) +
# Title
labs(title = "MTCARS") +
# Frame 1
geom_point() +
# Frame 2
geom_point(aes(color = factor(cyl))) +
# Frame 3
geom_point(aes(color = factor(cyl), size = wt)) +
# gganimate functions
transition_layers() + enter_fade() + enter_grow()
# Render animation
animate(anim)https://stackoverflow.com/questions/39900656
复制相似问题