r-newbie
r-newbie

Reputation: 159

gganimate does not work properly in combination with geom_area

I would like to animate a ggplot with gganimate using geom_area with different values for the years between 2000 and 2050. However, for some reason if I include view_zoom to keep the y-axis fixed and to zoom out along the x-axis for around the first 50 frames it zooms in between the values 1999.95 and 2000.05 and for the last 50 frames it shows the whole range of the x-axis (from year 2000 to 2050). How can I fix this, so that it gradually zooms out until it shows the whole range of the x-axis at the end?

library(gganimate)
library(tidyverse)

gif_data <-
  tibble(year = as.numeric(2000:2050),
         value = as.numeric(seq(0.5, 0.3, length.out = 51)))

gif <-
  ggplot(gif_data,
         aes(x = year,
             y = value)) +
  geom_area() +
  transition_reveal(year) +
  ggtitle('Frame {frame} of {nframes}') +
  view_zoom(fixed_y = TRUE)

animate(gif,
        fps = 10,
        duration = 10,
        height = 337.5,
        width = 600,
        units = "px",
        res = 50,
        renderer = gifski_renderer()) 

anim_save("~/Desktop/gif.gif",
          animation = last_animation())

example gif

Upvotes: 1

Views: 290

Answers (1)

hyman
hyman

Reputation: 325

Use view_follow instead of view_zoom like this:

gif <-
  ggplot(gif_data,
         aes(x = year,
             y = value)) +
  geom_area() +
  transition_reveal(year) +
  ggtitle('Frame {frame} of {nframes}') + 
  view_follow(fixed_y = TRUE)

Upvotes: 0

Related Questions