3

I am trying to do an animation to show the top 10 country in terms of GDP per captia. I am able to make the gif as below. I would like to make the label of country on the left side of y-axis. How can I do that?

Here is my code:

install.packages("ggplot2")
install.packages("data.table")
install.packages("gapminder")

library(gapminder)
library(ggplot2)
library(data.table)

gapminder10 = gapminder[,.SD[order(gdpPercap,decreasing = TRUE),][1:10],by = year]
gapminder10[, rank := 1:.N, by = year]

p <- ggplot(gapminder10) +
  geom_bar(aes(x=gdpPercap, y=rank, fill = country), stat = "identity")+   # I need to use geom_bar to make the bar
  geom_text(aes(x=0, y=rank, label = country), vjust = 0.2, hjust = "inward", size = 4) +
  labs(x = "GDP per capita", y = "country")+
  scale_y_reverse() +  # reverse the y-axis
  theme(legend.position="none", axis.text.y=element_blank(), axis.title.y=element_blank(),
        axis.ticks=element_blank(),panel.background=element_blank())

g<-p + transition_time(year) +
  labs(title = "Year: {frame_time}")

animate(g, nframes = 350,fps = 20, renderer = gifski_renderer())

enter image description here

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Liang Xu
  • 51
  • 2

1 Answers1

3

Putting the labels via geom_text on the left of the y-axis can be achieved like so:

  1. Put the labels on the left of the datapoints by changing the horizontal alignment via hjust = 1.

  2. I also added some space between the label and the axis by setting x = -1000

  3. We have to add some space for the labels so that they are not cut when reaching the borders of the plot. This can be achieved by increasing the expansion of the scale via the expand argument of scale_x_continuous. I inreased the expansion at the lower end to 20 percent, while keeping the default 5 percent at the upper end.

  4. Finally, to prevent breaks with negative values when setting x = -1000 in geom_text I force the breaks to start at 0 via breaks = seq(0, 90000, 10000).

Note 1: Instead of using scale_y_reverse I converted rank to a factor and used forcats::fct_rev to reverse the order.

Note 2: When running your code I got an error in the first data manipulation step. For this reason and as I'm unfortunately not familiar with data.table I switched to dplyr to prepare the dataset gapminder10. Also, I reduced the number of frames to reduce the rendering time for the reprex.

library(gapminder)
library(ggplot2)
library(gganimate)
library(dplyr)
library(forcats)

gapminder10 <- gapminder %>% 
  group_by(year) %>% 
  top_n(10, gdpPercap) %>% 
  arrange(year, desc(gdpPercap)) %>% 
  mutate(rank = row_number(),
         rank = fct_rev(factor(rank))) %>% 
  ungroup()

p <- gapminder10 %>% 
  ggplot() +
  geom_bar(aes(x=gdpPercap, y = rank, fill = country), stat = "identity")+   # I need to use geom_bar to make the bar
  geom_text(aes(x = -1000, y = rank, label = country), vjust = 0.2, hjust = 1, size = 4) +
  labs(x = "GDP per capita", y = NULL) +
  scale_x_continuous(breaks = seq(0, 90000, 10000), expand = expansion(mult = c(.2, 0.05))) +
  theme(legend.position="none", 
        axis.text.y = element_blank(), 
        axis.title.y=element_blank(),
        axis.ticks=element_blank(),panel.background=element_blank())

g<-p + transition_time(year) +
  labs(title = "Year: {frame_time}")

animate(g, nframes = 50, fps = 20, renderer = gifski_renderer())

stefan
  • 90,330
  • 6
  • 25
  • 51