How to make y axis limits dynamic when using gganimate

Viewed 425

I have an animated plot I want to make where the final value is much higher than the rest. I want the y axis to change with the values so that when the final value is shown it changes the y axis and shrinks the rest of the data. I tried view_follow but that only changes the x axis, not the y axis.

Here's some sample code that recreates my problem

library(tidyverse)
library(gganimate)

#simple test data
exampledata <- data.frame(year = 1961:2020,data = rnorm(60,100,10))
exampledata$data[60] <- 2000
#plotting simple test
exampledata %>% ggplot(aes(x=year,
                           y = data)) + 
  geom_bar(stat = "identity") + 
  transition_reveal(year) + 
  shadow_trail(past = TRUE)

Does anyone know how to change the y axis so it changes with the largest value over time?

Thanks!

1 Answers

Try using view_follow with fixed_x to have dynamic y-axis bounds but fixed x-axis bounds, e.g.

exampledata %>% ggplot(aes(x=year,
                           y = data)) +
  geom_bar(stat = "identity") +
  transition_reveal(year) +
  view_follow(fixed_x = TRUE) +
  shadow_trail(past = TRUE)

enter image description here

Related