R 使用 gganimate 和 geom_text 制作动画线图
R Animating line plot using gganimate and geom_text
我使用 gganimate 制作了动画情节,因此我可以看到 y 轴随时间的变化。
到目前为止,我有以下设置,它完成了我想要实现的大部分工作。但是,我绝对想再清理一下 geom_text 部分,以便其他人更容易看到。
我的目标
- 我想去掉所有帧中的固定起始量,因为它实际上没有任何用处。
- 文本显示该特定帧内的数量。我不想绘制像“81788.2”这样的原始数字,而是希望以“81k”之类的方式显示数量,类似于我设置 Y 轴的方式。我想要动画期间的这种一致模式。
- 如您所见,数字文本从图表中溢出。我想尽可能地包含图表中的数字。
任何关于如何实现这些的建议将不胜感激
anim3 <-
ggplot(trade_balance,(aes(x=Year, y=Amount,group=Trade_Balance,color=Trade_Balance)))+
geom_line(size=2)+
geom_text(aes(x = max(Year), label = Amount)) +
transition_reveal(Year)+
coord_cartesian(clip = 'off') +
scale_y_continuous(breaks = c(100000,200000,300000,400000,500000,600000),
labels=c("100k","200k","300k","400k","500k","600k"))+
labs(x="Year", y="USD million",title="Forign trade statistic U.S. / China")
animate(anim3, renderer = gifski_renderer())
使用的数据框
> trade_balance
Year Amount Trade Trade_Balance
1 2019 106447.3 1 Exports to China
2 2018 120289.3 1 Exports to China
3 2017 129997.2 1 Exports to China
4 2016 115594.8 1 Exports to China
5 2015 115873.4 1 Exports to China
6 2014 123657.2 1 Exports to China
7 2013 121746.2 1 Exports to China
8 2012 110516.6 1 Exports to China
9 2011 104121.5 1 Exports to China
10 2010 91911.1 1 Exports to China
11 2009 69496.7 1 Exports to China
12 2008 69732.8 1 Exports to China
13 2007 62936.9 1 Exports to China
14 2006 53673.0 1 Exports to China
15 2005 41192.0 1 Exports to China
16 2004 34427.8 1 Exports to China
17 2003 28367.9 1 Exports to China
18 2002 22127.7 1 Exports to China
19 2001 19182.3 1 Exports to China
20 2000 16185.2 1 Exports to China
21 1999 13111.1 1 Exports to China
22 2019 451651.4 0 Imports from China
23 2018 539243.1 0 Imports from China
24 2017 505165.1 0 Imports from China
25 2016 462420.0 0 Imports from China
26 2015 483201.7 0 Imports from China
27 2014 468474.9 0 Imports from China
28 2013 440430.0 0 Imports from China
29 2012 425619.1 0 Imports from China
30 2011 399371.2 0 Imports from China
31 2010 364952.6 0 Imports from China
32 2009 296373.9 0 Imports from China
33 2008 337772.6 0 Imports from China
34 2007 321442.9 0 Imports from China
35 2006 287774.4 0 Imports from China
36 2005 243470.1 0 Imports from China
37 2004 196682.0 0 Imports from China
38 2003 152436.1 0 Imports from China
39 2002 125192.6 0 Imports from China
40 2001 102278.4 0 Imports from China
41 2000 100018.2 0 Imports from China
42 1999 81788.2 0 Imports from China
下面的代码处理您的三项。我还做了一些其他更改,我希望这些更改会使代码更清晰并改善情节的外观。
library(tidyverse)
library(gganimate)
anim3 <- ggplot(trade_balance,
aes(x=Year, y=Amount, group=Trade_Balance, color=Trade_Balance)) +
geom_line(size=2) +
geom_text(aes(x=max(Year), label=sprintf("%1.0fk", Amount/1000)),
nudge_x=3, hjust=1, show.legend=FALSE, size=4.5) +
transition_reveal(Year, keep_last=FALSE) +
#coord_cartesian(clip = 'off') +
scale_y_continuous(breaks = seq(0, 6e5, 1e5),
labels = function(x) paste0(x/1e3,"k"),
expand = expansion(c(0, 0.05))) +
scale_x_continuous(expand = expansion(c(0.02, 0.02))) +
expand_limits(y=0,
x=max(trade_balance$Year, na.rm=TRUE) + 3) +
labs(x="Year", y="USD million",title="Foreign trade statistic U.S. / China",
colour="Trade Balance") +
theme_bw(base_size=15)
animate(anim3, renderer = gifski_renderer(), height=400, width=550)
anim_save("anim_example.gif")
我使用 gganimate 制作了动画情节,因此我可以看到 y 轴随时间的变化。
到目前为止,我有以下设置,它完成了我想要实现的大部分工作。但是,我绝对想再清理一下 geom_text 部分,以便其他人更容易看到。
我的目标
- 我想去掉所有帧中的固定起始量,因为它实际上没有任何用处。
- 文本显示该特定帧内的数量。我不想绘制像“81788.2”这样的原始数字,而是希望以“81k”之类的方式显示数量,类似于我设置 Y 轴的方式。我想要动画期间的这种一致模式。
- 如您所见,数字文本从图表中溢出。我想尽可能地包含图表中的数字。
任何关于如何实现这些的建议将不胜感激
anim3 <-
ggplot(trade_balance,(aes(x=Year, y=Amount,group=Trade_Balance,color=Trade_Balance)))+
geom_line(size=2)+
geom_text(aes(x = max(Year), label = Amount)) +
transition_reveal(Year)+
coord_cartesian(clip = 'off') +
scale_y_continuous(breaks = c(100000,200000,300000,400000,500000,600000),
labels=c("100k","200k","300k","400k","500k","600k"))+
labs(x="Year", y="USD million",title="Forign trade statistic U.S. / China")
animate(anim3, renderer = gifski_renderer())
使用的数据框
> trade_balance
Year Amount Trade Trade_Balance
1 2019 106447.3 1 Exports to China
2 2018 120289.3 1 Exports to China
3 2017 129997.2 1 Exports to China
4 2016 115594.8 1 Exports to China
5 2015 115873.4 1 Exports to China
6 2014 123657.2 1 Exports to China
7 2013 121746.2 1 Exports to China
8 2012 110516.6 1 Exports to China
9 2011 104121.5 1 Exports to China
10 2010 91911.1 1 Exports to China
11 2009 69496.7 1 Exports to China
12 2008 69732.8 1 Exports to China
13 2007 62936.9 1 Exports to China
14 2006 53673.0 1 Exports to China
15 2005 41192.0 1 Exports to China
16 2004 34427.8 1 Exports to China
17 2003 28367.9 1 Exports to China
18 2002 22127.7 1 Exports to China
19 2001 19182.3 1 Exports to China
20 2000 16185.2 1 Exports to China
21 1999 13111.1 1 Exports to China
22 2019 451651.4 0 Imports from China
23 2018 539243.1 0 Imports from China
24 2017 505165.1 0 Imports from China
25 2016 462420.0 0 Imports from China
26 2015 483201.7 0 Imports from China
27 2014 468474.9 0 Imports from China
28 2013 440430.0 0 Imports from China
29 2012 425619.1 0 Imports from China
30 2011 399371.2 0 Imports from China
31 2010 364952.6 0 Imports from China
32 2009 296373.9 0 Imports from China
33 2008 337772.6 0 Imports from China
34 2007 321442.9 0 Imports from China
35 2006 287774.4 0 Imports from China
36 2005 243470.1 0 Imports from China
37 2004 196682.0 0 Imports from China
38 2003 152436.1 0 Imports from China
39 2002 125192.6 0 Imports from China
40 2001 102278.4 0 Imports from China
41 2000 100018.2 0 Imports from China
42 1999 81788.2 0 Imports from China
下面的代码处理您的三项。我还做了一些其他更改,我希望这些更改会使代码更清晰并改善情节的外观。
library(tidyverse)
library(gganimate)
anim3 <- ggplot(trade_balance,
aes(x=Year, y=Amount, group=Trade_Balance, color=Trade_Balance)) +
geom_line(size=2) +
geom_text(aes(x=max(Year), label=sprintf("%1.0fk", Amount/1000)),
nudge_x=3, hjust=1, show.legend=FALSE, size=4.5) +
transition_reveal(Year, keep_last=FALSE) +
#coord_cartesian(clip = 'off') +
scale_y_continuous(breaks = seq(0, 6e5, 1e5),
labels = function(x) paste0(x/1e3,"k"),
expand = expansion(c(0, 0.05))) +
scale_x_continuous(expand = expansion(c(0.02, 0.02))) +
expand_limits(y=0,
x=max(trade_balance$Year, na.rm=TRUE) + 3) +
labs(x="Year", y="USD million",title="Foreign trade statistic U.S. / China",
colour="Trade Balance") +
theme_bw(base_size=15)
animate(anim3, renderer = gifski_renderer(), height=400, width=550)
anim_save("anim_example.gif")