gganimate:一个接一个地制作动画条并保留之前显示的

gganimation: animate bars one by one and retatin previously shown

我想知道如何使用 gganimateggplot2 动画化 geom_bar,使之前显示的条形保持不变。

library(ggplot2)
library(gganimate)
 
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))
df1 <- rbind(a,b)  
 
Plot1 <-
  ggplot(data= df1, aes(x = group, y = values, fill = group)) + 
  geom_bar(stat='identity') +
  theme_bw() +
  transition_states(
    states            = group
  , transition_length = 2
  , state_length      = 1
  ) 
Plot1

上面给出的代码一条一条地显示,但淡出之前显示的。但是,我想保留已经显示的条形图。有什么想法。

gganimate 包中查看 ?shadow_mark()。我认为这实现了您想要实现的目标:

library(ggplot2)
library(gganimate)

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))
df1 <- rbind(a,b)  

Plot1 <-
  ggplot(data= df1, aes(x = group, y = values, fill = group)) + 
  geom_bar(stat='identity') +
  theme_bw() +
  transition_states(
    states            = group
    , transition_length = 2
    , state_length      = 1
  ) +
  shadow_mark() # keeps past data displayed
Plot1