首页 > 解决方案 > R gganimate transition_time() 函数

问题描述

我有以下数据框,其中“库存”列中有两个不同的条目:

一瞥(df_final_time_series)

Rows: 396
Columns: 3
$ date  <date> 2004-01-01, 2004-02-01, 2004-03-01, 2004-04-01, 200...
$ stock <chr> "vtv", "vtv", "vtv", "vtv", "vtv", "vtv", "vtv", "vt...
$ price <dbl> 31.70592, 32.52995, 31.80250, 31.43529, 31.66834, 32...

我用以下代码创建了一个时间序列图:

    df_final_time_series %>% 
  ggplot(aes(x=stock, y=price)) +
  geom_line(aes(color = stock), size = 2) +
  geom_segment(aes(xend = stock, yend = price)) +
  geom_label(aes(label = price))+
  labs(title = "",
       subtitle = "Time-Series Performances of VTV and VUG",
       caption = "Source: Yahoo Finance") +
  gganimate::transition_reveal(as.Date(date)) + 
  ggthemes::theme_fivethirtyeight()

这段代码工作正常。我现在正在尝试复制带有 Year: YYYY 的图表,标题随着数据点的变化而变化。

我尝试实现 transition_time(year) 和其他几个变体,但遇到错误有 50 个或更多警告(使用 warnings() 查看前 50 个)警告消息:

1:无法获取绘图表的尺寸。绘图区域可能未固定 2:未找到对象“年份”3:未找到对象“年份”... 50:未找到对象“年份”

    df_final_time_series %>% 
  ggplot(aes(x=stock, y=price)) +
  geom_line(aes(color = stock), size = 2) +
  geom_segment(aes(xend = stock, yend = price)) +
  geom_label(aes(label = price))+
  transition_time(year)+
  labs(title = "Year: {year}",
       subtitle = "Time-Series Performances of VTV and VUG",
       caption = "Source: Yahoo Finance") +
  gganimate::transition_reveal(as.Date(date)) + 
  ggthemes::theme_fivethirtyeight()

标签: rggplot2time-seriesgganimate

解决方案


你试过 {frame_time} 而不是 {year}?这就是 gganimate 帮助页面显示的内容。请参阅 gganimate.com。


推荐阅读