3

Each bar appears and then disappears before the next one does. I'm copying the code I'm using 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")
#create data frame
age_continent <- data.frame(continent,med_age)

Plot:

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)
Renthu
  • 47
  • 3

2 Answers2

2

A little modified primary answer from 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

TarJae
  • 72,363
  • 6
  • 19
  • 66
1

This could be achieved via shadow_mark() which

... 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.

See 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
  • 90,330
  • 6
  • 25
  • 51