如何使用 gganimate 制作不同年份的动画 ggplot

How to Make animated ggplot for different years using gganimate

所以我有一个简单的数据框,其中第一列包含道路 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)

大部分工作在于在将数据发送到 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之前将数据从wide更改为long。

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

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

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)

_________________________ 更新后编辑 COMMENTS_______
在评论中请求:

"I just want it to go through the timeline (from 2001 to 2003) just once and then stop at 2003."

如果您想在 2003 年停止,则需要在将数据发送到 ggplot 之前过滤数据 - 这是通过过滤器命令完成的。
截至 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)