How can I use gganimate with geom_tile?

Viewed 229

I was having trouble animating a geom_tile() plot where the tile remains visible after it appears.

Here's my code using the airquality data.

First, the static plot. Here, the x-axis is Day. The y-axis is Month and Temp is the fill.

library(gganimate) 

anim <- ggplot(airquality, aes(x = Day, y = Month, fill = Temp)) +
  geom_tile()

anim

Static Tile Plot enter image description here

Using transition_reveal() doesn't visually preserve the Temp tiles as it traverses along Day.

anim1 <- anim + transition_reveal(Day)
anim1

Animated Tile Plot

I also tried this with transition_time() with no luck.

Thanks for your help!

2 Answers

One possibility here is transiton_manual:

anim1 <- anim + transition_manual(Day, cumulative = TRUE)

enter image description here

You can achieve this with transition_time() and shadow_mark()

library(gganimate) 

anim <- ggplot(airquality, aes(x = Day, y = Month, fill = Temp)) +
  geom_tile()+ 
  transition_time(Day) +
  shadow_mark()
anim

enter image description here

Related