purpleskater
purpleskater

Reputation: 57

R Animating line plot using gganimate and geom_text

I made animated plot using gganimate, so I can see the change in y axis as the year goes by.

So far, I have the setting below and it does most of the stuff I'm trying to achieve. However, I definitely want to clean up the geom_text portion a little more so it's easier for others to see.

My goal

  1. I want to get rid of fixed starting amount that is in all of the frames as it doesn't really serve any purpose.
  2. The text displays the amount during that specific frame. Instead of plotting raw numbers like "81788.2", I want the amount to be displayed in likes of "81k", similar to how I set up to Y axis. I want this consistent pattern during the animation.
  3. As you can see, number text are spilling out of the graph. I want to contain the numbers within the graph as much as possible.

Any suggestion on how to achieve these would be much appreciated

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())

enter image description here

data frame used

> 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

Upvotes: 3

Views: 1600

Answers (1)

eipi10
eipi10

Reputation: 93851

The code below takes care of your three items. I've also made some other changes that I hope will make the code cleaner and improve the look of the plot.

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")

enter image description here

Upvotes: 3

Related Questions