如何使用tweenr和gganimate在下面的两个条形图之间创建一个非常流畅的动画?
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()发布于 2019-06-07 02:39:56
编辑:更改transition_states和ease_aes,使其花费更多时间在转换上,增加字体大小,并更改animate术语以延长持续时间&移动速度更慢。
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,从而导致了“滑动”动画。(好奇的是,其他人是否能找到更简单的解决办法。)
a <- ggplot(df, aes(x = decile, y = change)) +
geom_col() +
...

https://stackoverflow.com/questions/56487195
复制相似问题