将动态文本添加到gganimate的动画中

时间:2019-06-08 01:20:45

标签: r gganimate

下面的代码对两年之间的简单条形图进行动画处理。

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 

如何动态显示与当前图对应的年份?

0 个答案:

没有答案