2

I can't use transitions from the gganimate package due to how I've set up my colors from group to group. My data is organized by Team, Season, Rank, Primary color, Secondary Color:

Team,Season,Rank,Primary,Secondary
Washington Wizards,1998,16,#e31837,#002B5C
Washington Wizards,1999,24,#e31837,#002B5C
Washington Wizards,2000,24,#e31837,#002B5C
Utah Jazz,1998,2,#F9A01B,#002B5C
Utah Jazz,1999,5,#F9A01B,#002B5C
Utah Jazz,2000,5,#F9A01B,#002B5C
Toronto Raptors,1998,28,#BA0C2F,#753BBD
Toronto Raptors,1999,19,#BA0C2F,#753BBD
Toronto Raptors,2000,9,#BA0C2F,#753BBD
San Antonio Spurs,1998,5,#000000,#c4ced4
San Antonio Spurs,1999,1,#000000,#c4ced4
San Antonio Spurs,2000,9,#000000,#c4ced4
Sacramento Kings,1998,23,#63727A,#5a2d81
Sacramento Kings,1999,9,#63727A,#5a2d81
Sacramento Kings,2000,9,#63727A,#5a2d81

And my current code:

library(ggplot2)
library(gganimate)
library(transformr)
library(dplyr)

df <- read.csv('NBARH_mini.csv')
df$Primary <- as.character.factor(df$Primary)
df$Secondary <- as.character.factor(df$Secondary)



df_sort <-arrange(df,Team)
PPalette <- c(df_sort$Primary[c(1,4,7,10,13)])
SPalette <- c(df_sort$Secondary[c(1,4,7,10,13)])
TPalette <- c(PPalette,SPalette)


p <- ggplot(df_sort, aes(x = Season, y = Rank)) +
  geom_point(aes(colour = Team), size = 3, shape = 21, fill = df_sort$Secondary)+
  geom_line(aes(group = Team, color = Team))+
  scale_y_reverse()
p <- p + scale_colour_manual(values= PPalette)+
         scale_fill_manual(values = SPalette, aesthetics = "fill")

p

anim <- p + 
  transition_states(Team,
                    transition_length = 5,
                    state_length = 5)+
  ease_aes('quadratic-in-out')+ # Slow start and end for a smoother look
  ggtitle('NBA Season ending rank since Jordan',
  subtitle='Ranking History of the {closest_state}')


animate(anim +
          enter_fade() +
          exit_shrink(),
        nframes = 50, 
        width = 500, 
        height =400)

My goal is to have a line plot and point plot for each team where the lines are the primary color and the points are filled with the secondary color. I spent a bunch of time trying to hack that together for just a plot trying lots of different methods. The only thing I could do to make it work is to pass df_sort$Secondary into geom_point() and I've been told that's a "NoNo", but I can't get it to plot otherwise.

And now that I've got that working I get

Error: identical(classes, col_classes(to)) is not TRUE

when I try to animate when using enter_fade()+exit_shrink(). It will animate with/without those, but with no transitions. Here's what it looks like currently.

https://gfycat.com/NervousComposedGrayfox

Anyone have any experience with this?

Adam Bunn
  • 63
  • 5
  • I think you can simplify the process of setting colours somewhat by just using `Primary` and `Secondary` directly and using `scale_color_identity()` and `scale_fill_identity()`. Unfortunately I just tried it and it doesn't seem to solve the issues with the animation. – Marius Mar 05 '19 at 22:45
  • I'm very sure there is a more efficient way to assign colors. I'm using this project to try and learn as best I can about ggplot2 so I'll go take a look at those two functions. Would they replace scale_color_manual/scale_fill_manual? – Adam Bunn Mar 05 '19 at 22:50
  • Yes, and you would set `colour = Primary` and `fill = Secondary` in the aesthetics. Like I said, I tried it as I thought it would solve the issues with animation, but no dice. – Marius Mar 05 '19 at 22:56
  • I'd first of all question *why* you want to animate this way. What's the purpose of cycling through the teams one by one? It doesn't help the reader to compare performance across teams, since you never see two teams at the same time. And mapping both colour & fill to Team with different palettes sounds unnecessarily confusing. – Z.Lin Mar 06 '19 at 02:18
  • It's just an experiment, and once I found I couldn't do it, I wanted to know if it was an error on my part of if it just can't be done with gganimate. And I could see implementing a shadow_wake with a small subset of teams to create a loop where a teams go from grey -> color -> grey as visually interesting. Thanks for chiming in. – Adam Bunn Mar 06 '19 at 05:32

0 Answers0