0

The code below animates a simple bar plot between two years.

library(tweenr)
library(gganimate)
library(tidyverse)

df <- tibble(
  decile = c("lowest", "second", "third", "lowest", "second", "third"),
  change = c(1, 2, -0.5, -2, -3, 4),
  year = c(2001L, 2001L, 2001L, 2002L, 2002L, 2002L)
)

df2001 <- filter(df, year == 2001)
df2002 <- filter(df, year == 2002)

ggplot(df2001, aes(x = decile, y = change)) +
  geom_col() +
  scale_y_continuous(limits = c(-5, 5)) +
  theme_minimal()

ggplot(df2002, aes(x = decile, y = change)) +
  geom_col() +
  scale_y_continuous(limits = c(-5, 5)) +
  theme_minimal()

a <- ggplot(df, aes(x = decile, y = change/2, 
                    height = change, width = 0.9, group = decile)) +
  geom_tile() +
  scale_y_continuous(limits = c(-5, 5), name = "change") +
  theme_minimal(base_size = 26) +
  transition_states(year, wrap = FALSE, transition_length = 10, state_length = 1) +
  ease_aes("linear") +
  labs(title = "Year: {as.integer(frame_time)}")

animate(a, fps = 10, duration = 5, width = 800, height = 600, start_pause = 15)

I would like to have text indicating which year is being shown. I thought the addition of:

+ labs(title = "Year: {as.integer(frame_time)}")

would work. However, it results in the error:

Error in png::readPNG(frames[1], native = TRUE) : unable to open /var/folders/46/jqbc50s140sgtrn9n3k8wppw0000gn/T//RtmpzhebyS/5da71995796d/gganim_plot0001.png In addition: There were 36 warnings (use warnings() to see them)

warnings() yields:

Warning messages: 1: Cannot get dimensions of plot table. Plot region might not be fixed 
2: object 'frame_time' not found 
3: object 'frame_time' not found 
4: object 'frame_time' not found 
5: object 'frame_time' not found 

How do I dynamically display the year that corresponds with the current plot?

ixodid
  • 2,180
  • 1
  • 19
  • 46

0 Answers0