FR_Data
FR_Data

Reputation: 23

How can I use gganimate with geom_tile?

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!

Upvotes: 2

Views: 477

Answers (2)

Jon Spring
Jon Spring

Reputation: 66765

One possibility here is transiton_manual:

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

enter image description here

Upvotes: 3

TarJae
TarJae

Reputation: 79164

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

Upvotes: 2

Related Questions