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

标签 r ggplot2 gganimate

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

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)

最佳答案

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)
enter image description here

关于r - 如何在条形图上使用 gganimate 以便在动画结束之前出现的每个条形都不会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66187331/

相关文章:

r - 过滤每个主题有多个事件之间的行

R:两个数据集之间的模糊连接

r - 关于如何根据 R 中的条件删除行的任何想法?

r - 如何在ggplot2中获得水平颜色渐变?

r - 将方形或三角形标记(不是圆形)添加到 R 绘图 map

r - 向 GGPLOT2 中的条形图添加唯一趋势线

r - 如何在R中设置动画的速度?

r - transition_time 带有阴影标记,仅用于一个几何体/层

r - R中data.frame中每一列的第N个最小值

r - gganimate 根据时间对多条路径进行动画处理