Renthu
Renthu

Reputation: 47

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

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)

Upvotes: 3

Views: 796

Answers (2)

TarJae
TarJae

Reputation: 79122

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

Upvotes: 3

stefan
stefan

Reputation: 125028

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)

Upvotes: 1

Related Questions