Добавить динамический текст в анимацию gganimate - PullRequest
0 голосов
/ 08 июня 2019

Код ниже анимирует простой гистограммы между двумя годами.

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)

Я бы хотел, чтобы текст указывал, какой год показывается. Я думал, что добавление:

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

будет работать. Однако это приводит к ошибке:

Ошибка в png :: readPNG (frames [1], native = TRUE): невозможно открыть /var/folders/46/jqbc50s140sgtrn9n3k8wppw0000gn/T//RtmpzhebyS/5da71995796d/gganim_plot0001.png Кроме того: было 36 предупреждений (используйте warnings () чтобы увидеть их)

warnings () дает:

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 

Как динамически отобразить год, соответствующий текущему графику?

...