我从以下网站获得以下代码: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")
,因此代码是不同的。
答案 0 :(得分: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
。