首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用tweenr和gganimate作图

用tweenr和gganimate作图
EN

Stack Overflow用户
提问于 2019-06-07 02:17:08
回答 1查看 186关注 0票数 1

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-07 02:39:56

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

代码语言:javascript
复制
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没有区分它的基线和范围,而是区分它的min和max,从而导致了“滑动”动画。(好奇的是,其他人是否能找到更简单的解决办法。)

代码语言:javascript
复制
a <- ggplot(df, aes(x = decile, y = change)) +
  geom_col() +
  ...

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56487195

复制
相关文章

相似问题

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