r - gganimate 不平滑动画

标签 r ggplot2 gif gganimate

几天来我一直在努力解决这个问题,但我不明白我错过了什么。 gganimate 似乎没有在帧之间添加任何过渡。

我正在使用 Tour de France dataset from the tidytuesday .由此,我制作了一个多年来与获胜者所在国家/地区的数据框。我有每年的累计胜利次数,并添加了当年每个国家的排名。有 106 个版本,从 1 到 106,以及 106 年(从 1903 年到 2019 年,由于 WWs 有一些差距)。

编辑:可用 CSV 格式的数据集:https://github.com/AScalco/AS_TidyTuesday

> head(race_chart_ranked, 20)
# A tibble: 20 x 5
# Groups:   year [2]
   nationality   n.victories edition  year  rank
   <chr>               <int>   <dbl> <dbl> <int>
 1 France                  1       1  1903     1
 2 Luxembourg              0       1  1903     2
 3 Belgium                 0       1  1903     3
 4 Italy                   0       1  1903     4
 5 Switzerland             0       1  1903     5
 6 Spain                   0       1  1903     6
 7 Netherlands             0       1  1903     7
 8 United States           0       1  1903     8
 9 Ireland                 0       1  1903     9
10 Denmark                 0       1  1903    10
11 Germany                 0       1  1903    11
12 Australia               0       1  1903    12
13 Great Britain           0       1  1903    13
14 Colombia                0       1  1903    14
15 France                  2       2  1904     1
16 Luxembourg              0       2  1904     2
17 Belgium                 0       2  1904     3
18 Italy                   0       2  1904     4
19 Switzerland             0       2  1904     5
20 Spain                   0       2  1904     6
> tail(race_chart_ranked, 20)
# A tibble: 20 x 5
# Groups:   year [2]
   nationality   n.victories edition  year  rank
   <chr>               <int>   <dbl> <dbl> <int>
 1 Switzerland             2     105  2018     9
 2 Australia               1     105  2018    10
 3 Denmark                 1     105  2018    11
 4 Germany                 1     105  2018    12
 5 Ireland                 1     105  2018    13
 6 Colombia                0     105  2018    14
 7 France                 36     106  2019     1
 8 Belgium                18     106  2019     2
 9 Spain                  12     106  2019     3
10 Italy                  10     106  2019     4
11 United States          10     106  2019     5
12 Great Britain           6     106  2019     6
13 Luxembourg              5     106  2019     7
14 Netherlands             2     106  2019     8
15 Switzerland             2     106  2019     9
16 Australia               1     106  2019    10
17 Colombia                1     106  2019    11
18 Denmark                 1     106  2019    12
19 Germany                 1     106  2019    13
20 Ireland                 1     106  2019    14

我制作了这个图,它运行良好,只是运行不流畅。我已经尝试更改持续时间、帧数和 fps 的数量,但变化确实很大。我尝试添加“ease_aes()”,但没有产生任何变化。我错过了什么?

animated plot - not smooth


animation_ranked <- race_chart_ranked %>%
  ggplot(aes(xmin = 0, xmax = n.victories, ymin=rank-.5, ymax=rank+0.5, y=rank)) +
  geom_rect(fill = "black", color="black", size=1.05) +
  scale_x_continuous(limits=c(-15, 40), breaks=c(seq(0, 40, by=5))) +
  # Add labels to each country 
  geom_text(aes(label=nationality, x=(-2.5), hjust="right"), size = 6, fontface="bold") +
  # Add a label with the year
  geom_text(aes(label=year, x=35, y=13.5), size=15) +
  # Add number of victories (if more than 0) for each country
  geom_text(data=race_chart_ranked %>% filter(n.victories > 0),
            aes(label=n.victories, x=n.victories-0.75, y=rank), color="white", fontface="bold") +
  # Reverse the scale to show the highest victories on top
  scale_y_reverse() +
  # Add labels
  labs(title = "Number of victories by country",
       x = "Number of victories", y = "") +
  # Choose theme
  theme_void() +
  # ANIMATION CODE
  # Group by year
  aes(group = year) +
  # Apply transition by time
  transition_time(year) +
  # Apply ease_eas()
  ease_aes()

# Set animation general options
options(gganimate.nframes = 100, gganimate.fps = 20)
# Animate and other options
animate(animation_ranked, end_pause=10, duration = 15)

最佳答案

您是否尝试过将 enter_fade() 添加到绘图中以查看它是否为您提供了您想要的外观?抱歉,我会把它留作评论,但我的声誉不够高。

animation_ranked <- race_chart_ranked %>%
  ggplot(aes(xmin = 0, xmax = n.victories, ymin=rank-.5, ymax=rank+0.5, y=rank)) +
  geom_rect(fill = "black", color="black", size=1.05) +
  scale_x_continuous(limits=c(-15, 40), breaks=c(seq(0, 40, by=5))) +
  # Add labels to each country 
  geom_text(aes(label=nationality, x=(-2.5), hjust="right"), size = 6, fontface="bold") +
  # Add a label with the year
  geom_text(aes(label=year, x=35, y=13.5), size=15) +
  # Add number of victories (if more than 0) for each country
  geom_text(data=race_chart_ranked %>% filter(n.victories > 0),
            aes(label=n.victories, x=n.victories-0.75, y=rank), color="white", fontface="bold") +
  # Reverse the scale to show the highest victories on top
  scale_y_reverse() +
  # Add labels
  labs(title = "Number of victories by country",
       x = "Number of victories", y = "") +
  # Choose theme
  theme_void() +
  # ANIMATION CODE
  # Group by year
  aes(group = year) +
  # Apply transition by time
  transition_time(year) +
  # Apply ease_eas()
  ease_aes() +
  enter_fade(alpha = 0)

关于r - gganimate 不平滑动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61140786/

相关文章:

regex - R:提取包含在两个子字符串之间的所有子字符串的最快方法

java - Android:显示来自互联网的 .gif

c++ - 构建 R 包 : "Found ' rand', 可能来自 'rand' (C)"检查包时注意

将 NA 的值替换为不同列中的先前值和当前值的总和

r - 如何在 ggplot2 中使用填充美学绘制两组的相对比例?

r - 在不使用外部工具的情况下使用大型数据集时,我可以减少 knitR/ggplot2 中的 pdf 文件大小吗?

objective-c - 在 iOS10 上生成的 GIF 图像不再在浏览器上永远循环

ffmpeg:将单独的帧保存为静止的 gif

r - `With` 函数内部使用(包装器)

r - ggplot饼图标签