如何在条形图上使用 gganimate,以便出现的每个条形在动画结束之前不会消失?

How do I use gganimate on a bar plot so each bar that appears doesn't disappear until the animation ends?

每个条出现然后在下一个出现之前消失。我正在复制我在这里使用的代码:

library(ggplot2)
library(gganimate)
med_age <- c(18,31,31,33,35,42)
continent <- c("Africa","South America","Asia","North & Central America","Oceania","Europe")
cont_colors <- c("tan2","yellowgreen","tomato1","lightpink2","seagreen2","steelblue2")
#create data frame
age_continent <- data.frame(continent,med_age)

剧情:

age_animate <- ggplot(data=age_continent,aes(x=continent,y=med_age))+
geom_bar(stat = "identity",fill=cont_colors)+
geom_text(aes(label=med_age), vjust=1.6, color="black", size=5)+
theme_minimal()+
theme(axis.text.y=element_blank(),panel.grid=element_blank(),axis.text=element_text(size=10))+
xlab("")+
ylab("")+
ggtitle("Median Age by Continent", subtitle = "Source: www.visualcapitalist.com")+
transition_states(continent)

animate(age_animate)

这可以通过 shadow_mark() 实现,

... lets you show the raw data behind the current frame. Both past and/or future raw data can be shown and styled as you want.

参见here

library(ggplot2)
library(gganimate)

med_age <- c(18,31,31,33,35,42)
continent <- c("Africa","South America","Asia","North & Central America","Oceania","Europe")
cont_colors <- c("tan2","yellowgreen","tomato1","lightpink2","seagreen2","steelblue2")

age_continent <- data.frame(continent,med_age)
  
age_animate <- ggplot(data=age_continent,aes(x=continent,y=med_age))+
  geom_bar(stat = "identity",fill=cont_colors)+
  geom_text(aes(label=med_age), vjust=1.6, color="black", size=5)+
  theme_minimal()+
  theme(axis.text.y=element_blank(),panel.grid=element_blank(),axis.text=element_text(size=10))+
  xlab("")+
  ylab("")+
  ggtitle("Median Age by Continent", subtitle = "Source: www.visualcapitalist.com")+
  transition_states(continent) +
  shadow_mark()

animate(age_animate)

对 stefan 的主要回答稍作修改!!

library(ggplot2)
library(gganimate)

med_age <- c(18,31,31,33,35,42)
continent <- c("Africa","South America","Asia","North & Central America","Oceania","Europe")
cont_colors <- c("tan2","yellowgreen","tomato1","lightpink2","seagreen2","steelblue2")
#create data frame
age_continent <- data.frame(continent,med_age)

  
  age_animate <- ggplot(data=age_continent,aes(x=continent,y=med_age))+
  geom_bar(stat = "identity",fill=cont_colors)+
  geom_text(aes(label=med_age), vjust=1.6, color="black", size=5)+
  theme_minimal()+
  theme(axis.text.y=element_blank(),panel.grid=element_blank(),axis.text=element_text(size=10))+
  xlab("")+
  ylab("")+
  ggtitle("Median Age by Continent", subtitle = "Source: www.visualcapitalist.com")+
  transition_states(continent, wrap = FALSE) + 
  shadow_mark() +
  enter_grow() +
  enter_fade()

anim <- animate(age_animate)
anim_save("age_animate.gif", anim)