2

I am trying to plot the top 3 NHL scorers over time using gganimate. Currently, I have a column chart where the x axis shows player names and the y axis shows number of goals for each player. Here's a static version of what I have:

library(ggplot2)

data <- data.frame(name=c("John","Paul","George","Ringo","Pete","John","Paul","George","Ringo","Pete"),
     year = c("1997", "1997", "1997", "1997", "1997", "1998", "1998","1998","1998", "1998"),
     goals = c(50L, 35L, 29L, 5L, 3L, 3L, 5L, 29L, 36L, 51L))

data <- data %>%
  arrange(goals) %>%
  group_by(year) %>%
  top_n(3, goals)

ggplot(data, 
  aes(x = reorder(name, goals), y=goals)) +
  geom_col() +
  facet_wrap(data$year) +
  coord_flip()

enter image description here

What I want is to ONLY display the top 3 players. In other words, players who are in the top 3 one year but drop out of the top three the next year should not be shown on the second frame. The final product should look something like this:

https://www.youtube.com/watch?v=nYjpZcPhoqU

James Martherus
  • 1,033
  • 1
  • 9
  • 20

1 Answers1

5

I adapted the solution from this post to your example. I also changed the data a little bit so we could see the 3rd player dropping out and another one entering in its place. The gganimate website is also a great place to look at some examples.

The trick is to use the rankings as your x-axis (or the y-axis in your flipped plot). This way, when the ranks change from one year to the other, the position of the columns will change too. Then you can hide the x-axis' labels and create a text label with geom_text at the desired position (the x-axis in this case).

One observation: you must use the group aesthetic inside geom_col. I think this tells gganimate that some shapes are the same thing between the frames (and thus they move accordingly).

Here is my code:

library(ggplot2)
library(gganimate)
library(plyr)
library(dplyr)
library(glue)

# I changed your data set a little
data <- data.frame(name=c("John","Paul","George","Ringo","Pete",
                          "John","Paul","George","Ringo","Pete"),
                   year = c("1997", "1997", "1997", "1997", "1997", 
                            "1998", "1998","1998","1998", "1998"),
                   goals = c(50L, 35L, 29L, 5L, 3L, 
                             45L, 50L, 10L, 36L, 3L))

# create variable with rankings (this will be used as the x-axis) and filter top 3
data2 <- data %>% group_by(year) %>%
  mutate(rank = rank(goals)) %>% filter(rank >= 3)

stat.plot <- ggplot(data2) +
  # **group=name** is very important
  geom_col(aes(x=rank, y=goals, group=name), width=0.4) +
  # create text annotations with names of each player (this will be our y axis values)
  geom_text(aes(x=rank, y=0, label=name, group=name), hjust=1.25) +
  theme_minimal() + ylab('Goals') +
  # erase rank values from y axis
  # also, add space to the left to fit geom_text with names
  theme(axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.margin = unit(c(1,1,1,2), 'lines')) +
  coord_flip(clip='off')

# take a look at the facet before animating
stat.plot + facet_grid(cols=vars(year))

# create animation
anim.plot <- stat.plot + ggtitle('{closest_state}') + 
  transition_states(year, transition_length = 1, state_length = 1) +
  exit_fly(x_loc = 0, y_loc = 0) + enter_fly(x_loc = 0, y_loc = 0)

anim.plot

And this is the result:

enter image description here

kikoralston
  • 1,176
  • 5
  • 6