R使用gganimate和geom_text动画线图

Purpleskater

我使用gganimate制作了动画图,因此我可以看到随着时间的推移y轴的变化。

到目前为止,我有下面的设置,它可以完成我要实现的大多数工作。但是,我绝对想多清理一下geom_text部分,以便其他人更容易看到。

我的目标

  1. 我想摆脱所有框架中固定的起始金额,因为它实际上没有任何作用。
  2. 文本显示该特定框架内的金额。我不希望像“ 81788.2”那样绘制原始数字,而是希望以“ 81k”的形式显示金额,类似于我设置为Y轴的方式。我希望动画过程中保持一致的模式。
  3. 如您所见,数字文字从图表中溢出。我想在图中尽可能包含数字。

关于如何实现这些的任何建议将不胜感激

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
eipi10

下面的代码照顾您的三个项目。我还做了一些其他更改,希望它们可以使代码更整洁并改善绘图的外观。

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

gganimate中的Ghost geom_text

来自分类Dev

在ggplot / gganimate中控制geom_point和geom_text的颜色

来自分类Dev

ggplot和geom_text()

来自分类Dev

单独进行动画处理geom_text和geom_point

来自分类Dev

ggplot和geom_text()标签

来自分类Dev

Matplotlib FuncAnimation不动画线图

来自分类Dev

Matplotlib FuncAnimation不动画线图

来自分类Dev

matplotlib 动画线图保持为空

来自分类Dev

使用gganimate为geom_bar设置动画的问题

来自分类Dev

使用geom_text标记geom_error栏

来自分类Dev

使用geom_text标记geom_error栏

来自分类Dev

使用ggplot2和geom_text在比例条形图中插入标签

来自分类Dev

结合上标和包含<符号的变量标签时使用ggplot geom_text

来自分类Dev

使用分面 geom_col 和 position="fill" 时为 geom_text 创建比例数字

来自分类Dev

画线图

来自分类Dev

geom_text和构面无法正常工作

来自分类Dev

ggplot:geom_text和ggplot(fill)之间的冲突

来自分类Dev

用ggplot调整geom_text和条之间的距离

来自分类Dev

geom_text和构面无法正常工作

来自分类Dev

geom_text适用于R中的直方图?

来自分类Dev

在R / ggplot中无法获得geom_text

来自分类Dev

如何在R中均匀绘制geom_text?

来自分类Dev

R ggplot2 标签排序(geom_text)

来自分类Dev

使用facet_wrap评估geom_text

来自分类Dev

使用position_dodge时出现geom_text问题

来自分类Dev

无法使用facet_wrap获得geom_text标签

来自分类Dev

r - ggplot2 - geom_text - geom_text 上方的水平值没有相同的水平位移

来自分类Dev

每组的geom_text位置

来自分类Dev

如何在具有ggplot的R中的多面堆叠条形图中使用不同的geom_text()标签?