Search code examples
rggplot2gganimate

How to make A racing Bar Chart in R


I have a dataset that has a column of years from 1965 to 2020 and Teams that have won the championship in the respective years.

I am trying to create a racing bar chart and so far I have been struggling to create the required dataset to create the animated GIF

  df1 <- df %>% 
     group_by(Team) %>% 
     mutate(cups = 1:n()) %>% 
     ungroup() %>% 
     group_by(Year) %>% spread(Year, cups) %>% 
     replace(is.na(.),0)

which brings a result of the following format. enter image description here

Kindly assist in how I should go about completing this racing bar chart as I have browsed through several resources but I still cant seem to crack it..


Solution

  • Check if this works, as Jon mentioned you need to pivot your data using pivot_longer from tidyr.

    df1 <- pivot_longer(df, -1, names_to = 'Year') %>%
      rename(Team= ï..Team) %>%
      mutate(Year = as.numeric(substr(Year, 2, 5)))
    

    Then this should create the racing barchart"

    df1 <- df1 %>%
      group_by(Year) %>%
      # The * 1 makes it possible to have non-integer ranks while sliding
      mutate(rank = min_rank(-value) * 1,
             Value_rel = value/value[rank==1],
             Value_lbl = paste0(" ",value)) %>%
      filter(rank <=10) %>%  # This would show the top 10 teams
      ungroup()
    
    p <- ggplot(df1, aes(rank, group = Team, 
                         fill = as.factor(Team), color = as.factor(Team))) +
      geom_tile(aes(y = value/2,
                    height = value,
                    width = 0.9), alpha = 0.8, color = NA) +
      geom_text(aes(y = 0, label = paste(Team, " ")), vjust = 0.2, hjust = 2) +
      geom_text(aes(y=value,label = Value_lbl, hjust=0)) +
      coord_flip(clip = "off", expand = FALSE) +
      scale_y_continuous(labels = scales::comma) +
      scale_x_reverse() +
      guides(color = FALSE, fill = FALSE) +
      
      labs(title='{closest_state}', x = "", y = "Your Title",
           caption = "Your Caption") +
      theme(plot.title = element_text(hjust = 0, size = 22),
            axis.ticks.y = element_blank(),  # These relate to the axes post-flip
            axis.text.y  = element_blank(),  # These relate to the axes post-flip
            plot.margin = margin(1,1,1,4, "cm")) +
      
      transition_states(Year, transition_length = 4, state_length = 1) +
      ease_aes('cubic-in-out')
    
    animate(p, 200, fps = 10, duration = 40, width = 800, height = 600, renderer = gifski_renderer("gganim.gif"))
    
    anim_save("YourPath//Name.gif")