首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动画化向ggplot2绘图添加层的过程

动画化向ggplot2绘图添加层的过程
EN

Stack Overflow用户
提问于 2016-10-06 15:59:08
回答 3查看 1.6K关注 0票数 6

我开始熟悉gganimate,但我想进一步扩展gifs。

例如,我可以在frame中的一个变量上抛出一个gganimate,但是如果我想动画化添加全新层/geoms/变量的过程呢?

下面是一个标准的gganimate示例:

代码语言:javascript
复制
library(tidyverse)
library(gganimate)

p <- ggplot(mtcars, aes(x = hp, y = mpg, frame = cyl)) +
    geom_point()

gg_animate(p)

但如果我想让礼物有生气呢:

代码语言:javascript
复制
# 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")

如何才能做到这一点?

EN

Stack Overflow用户

发布于 2021-01-26 20:26:39

前面的答案中的gganimate命令在2021年被废弃,无法完成OP的任务。

在Mark代码的基础上,您现在可以简单地创建一个具有多个分层geoms的静态ggplot对象,然后添加gganimate::transition_layers函数来创建一个在静态地块中从一层过渡到另一层的动画。像enter_fade()enter_grow()这样的推特功能控制着元素在框架中的变化。

代码语言:javascript
复制
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)
票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39900656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档