我使用gganimate制作了动画图,因此我可以看到随着时间的推移y轴的变化。
到目前为止,我有下面的设置,它可以完成我要实现的大多数工作。但是,我绝对想多清理一下geom_text部分,以便其他人更容易看到。
我的目标
关于如何实现这些的任何建议将不胜感激
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")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句