1

我从以下站点获得以下代码:https ://www.r-graph-gallery.com/288-animated-barplot-transition/

# libraries:
library(tidyverse)
library(tweenr)
library(gganimate)

# Make 2 basic barplots
a=data.frame(group=c("A","B","C"), values=c(3,2,4), frame=rep('a',3))
b=data.frame(group=c("A","B","C"), values=c(5,3,7), frame=rep('b',3))
data=rbind(a,b)

# Basic barplot:
ggplot(a, aes(x=group, y=values, fill=group)) +
  geom_bar(stat='identity')

# Interpolate data with tweenr
ts <- list(a, b, a)
tf <- tween_states(ts, tweenlength = 0.02, statelength = 0.001, ease = c('cubic-in-out'), nframes = 30)
tf

# Make a barplot with frame
p=ggplot(tf, aes(x=group, y=values, fill=group, frame= .frame)) +
  geom_bar(stat='identity', position = "identity")
gganimate(p, interval = .1, title_frame = F, filename="#288_barplot_animation.gif", ani.width=480, ani.height=480)

我想知道是否有人知道如何让它工作,因为 gganimate 在 R 3.5.1 上不起作用并且我已经安装了devtools::install_github("thomasp85/gganimate")所以代码不同。

4

1 回答 1

3

该代码适用于 @drob 的 gganimate 的原始版本,而不是 @thomasp85 的当前转世版本。新样式通过向 ggplot 调用添加步骤来处理补间,因此tween_states被替换为transition_states. 要指定缓动,请添加ease_aes

library(ggplot2)
library(gganimate)

df <- rbind(
    data.frame(group = c("A","B","C"), values = c(3,2,4), frame = rep('a',3)),
    data.frame(group = c("A","B","C"), values = c(5,3,7), frame = rep('b',3))
)

ggplot(df, aes(group, values, fill = group)) + 
    geom_col(position = "identity") + 
    transition_states(frame, .02, .001) + 
    ease_aes('cubic-in-out')

如果要调整fps或绘图大小,请将绘图分配给一个对象并animate使用所需的设置调用它。要保存,请使用anim_save.

于 2018-12-01T23:59:11.583 回答