【问题标题】:How to Make animated ggplot for different years using gganimate如何使用 gganimate 制作不同年份的动画 ggplot
【发布时间】:2020-02-14 03:36:20
【问题描述】:

所以我有一个简单的数据框,其中第一列包含道路 ID,接下来的 10 列包含每个道路 ID 10 年内的交通量。
我一直在尝试想出一个代码来显示 X 轴上的道路 ID 和 Y 轴上的交通量。然后在多年内为图表制作动画(Y 轴上的流量发生变化)。这是我的数据框示例:

谁能推荐一段代码来做这件事?这是我编写的代码,但并没有真正起作用。我知道这可能是非常错误的,但我对 gganimate 很陌生,不知道如何让不同的功能发挥作用。任何帮助表示赞赏。

year <- c(2001,2002,2003,2004,2005,2006,2007,2008,2009,2010)

p1 <- ggplot(data = Data) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2001Traffic)) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2002Traffic)) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2003Traffic)) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2004Traffic)) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2005Traffic)) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2006Traffic)) +

    geom_point(aes(x = Data$LinkIDs, y=Data$Year2007Traffic)) +  
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2008Traffic)) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2009Traffic)) +
    geom_point(aes(x = Data$LinkIDs, y=Data$Year2010Traffic)) +
  labs(title = 'Year: {frame_time}', x = 'Link ID', y = 'Traffic Volume') +  
  transition_time(year)

animate(p1)

【问题讨论】:

  • 请使用dput(Data)复制粘贴您的数据,它很难用作图像。
  • 我建议查找如何使您的数据为“长”格式,这将使 ggplot 更易于使用。见这里:tidyr.tidyverse.org

标签: r ggplot2 gganimate


【解决方案1】:

大部分工作在于在将数据发送到 ggplot 和 gganimate 之前更改数据。为了帮助您完成这项工作,我根据您的图片创建了一些示例数据(将来请您自己提供示例数据)。

library(tidyverse)
library(gganimate)

df <- tribble(
  ~LinkIDs, ~Year2001Traffic, ~Year2002Traffic, ~Year2003Traffic,
  "A", 1, 10, 15,
  "B", 3, 1, 10,
  "C", 10, 5, 1)
df
# A tibble: 3 x 4
  LinkIDs Year2001Traffic Year2002Traffic Year2003Traffic
  <chr>             <dbl>           <dbl>           <dbl>
1 A                     1              10              15
2 B                     3               1              10
3 C                    10               5               1

gganimate 和 ggplot 最适合长格式的数据。所以第一步是在发送到ggplot之前将数据从宽改为长。

df <- df %>% gather(Year, Traffic, -LinkIDs)
df
# A tibble: 9 x 3
  LinkIDs Year            Traffic
  <chr>   <chr>             <dbl>
1 A       Year2001Traffic       1
2 B       Year2001Traffic       3
3 C       Year2001Traffic      10
4 A       Year2002Traffic      10
5 B       Year2002Traffic       1
6 C       Year2002Traffic       5
7 A       Year2003Traffic      15
8 B       Year2003Traffic      10
9 C       Year2003Traffic       1

gganimate 需要 Year 列是一个数字才能将其用于动画。所以我们需要提取值中包含的数字。

df <- df %>% mutate(
  Year = parse_number(Year))
df
# A tibble: 9 x 3
  LinkIDs  Year Traffic
  <chr>   <dbl>   <dbl>
1 A        2001       1
2 B        2001       3
3 C        2001      10
4 A        2002      10
5 B        2002       1
6 C        2002       5
7 A        2003      15
8 B        2003      10
9 C        2003       1

现在剩下的就很简单了。只需绘制数据,并使用 year 变量作为动画参数。

p1 <- ggplot(df, aes(x = LinkIDs, y = Traffic))+
  geom_point()+
  labs(title = 'Year: {frame_time}', x = 'Link ID', y = 'Traffic Volume')+
  transition_time(Year)

animate(p1)

_______________ 更新评论后编辑_______
cmets 中的请求:

“我只是希望它通过时间线(从 2001 年到 2003 年) 一次,然后在 2003 年停止。”

如果您想在 2003 年停止,则需要在将数据发送到 ggplot 之前对其进行过滤 - 这是通过 filter 命令完成的。
截至 2019 年 3 月 23 日,据我所知,没有办法只看一遍动画。您可以更改 end_pause 参数,以便在动画的每次迭代后插入暂停(根据您的描述,我将 geom_point() 更改为 geom_col())。

p2 <- df %>% 
  #keep only observations from the year 2003 and earlier
  filter(Year <= 2003) %>% 
  #Send the data to plot
  ggplot(aes(x = LinkIDs, y = Traffic, fill = LinkIDs))+
  geom_col()+
  labs(title = 'Year: {frame_time}', x = 'Link ID', y = 'Traffic Volume')+
  transition_time(Year)  

animate(p2, fps = 20, duration = 25, end_pause = 95)

【讨论】:

  • 您好!非常感谢你的帮助。对于没有发布实际数据,我深表歉意。下次我一定会的。我还想问一件事。所以,我使用的是闪亮的,所以我必须使用 renderImage 才能显示动画图。我还将 ggplot 从点更改为条。有没有办法让我在图表底部添加时间线播放栏以使动画停止然后重新开始,类似于here底部的gapminder可视化?
  • 嗨阿克巴。不客气!我没有和 Shiny 合作过,所以我不能帮你。我的猜测是,由于 gganimate 仍然是一个非常新的包,Shiny 不会支持这样一个特定的功能。
  • 我明白了。无论如何,谢谢斯蒂恩。最后一个问题。你能告诉我如何停止动画吗?似乎它会永远持续下去。我只希望它通过时间线(从 2001 年到 2003 年)一次,然后在 2003 年停止。非常感谢您的帮助。
  • 嗨,Akbar,我更新了答案以帮助您处理最后一个请求。据我所知,目前没有选项可以在一次迭代后停止动画。您可以改为插入长时间的停顿。我希望这会有所帮助。
  • 非常感谢斯蒂恩。这很有帮助。
猜你喜欢
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 2020-07-13
  • 2020-07-23
  • 1970-01-01
相关资源
最近更新 更多