gganimate 不平滑动画
Posted
技术标签:
【中文标题】gganimate 不平滑动画【英文标题】:gganimate does not smooth animation 【发布时间】:2020-07-23 05:33:36 【问题描述】:我几天来一直试图弄清楚这一点,但我无法理解我错过了什么。 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)
【问题讨论】:
我也有同样的问题。会对您的解决方案感兴趣。 【参考方案1】:您是否尝试将 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)
【讨论】:
以上是关于gganimate 不平滑动画的主要内容,如果未能解决你的问题,请参考以下文章
R语言ggplot2和gganimate包可视化动态动画气泡图(Animated Bubble chart):使用gganimate包创建可视化gif动图
R语言gganimate包创建可视化gif动图并使用anim_save函数保存可视化gif动图(gganimate export to gif)
ggsave 和 gganimate 的“动画”中符号的大小一致