Home > database >  Why does gganimate fail to order lines correctly by date with transition_reveal?
Why does gganimate fail to order lines correctly by date with transition_reveal?

Time:03-25

I'm aiming to reproduce an animated figure by Ed Hawkins on climate change in R with gganimate. The figure is called enter image description here

Animated GIF: enter image description here

At first glance, the results look equal, however, the detail shows differences (for instance, the marked blue line).

enter image description here

In this example code with synthetic data, the differences are minor. But with real data, the figures look pretty different as many red lines (recent data points with high temperatures) disappear in the background. So, how can you retain the order in transition_reveal() by date? Any help appreciated, thanks a lot!

CodePudding user response:

This isn't the answer per se. This is the why. You'll have to tell me what you prefer given this information for me to give you a solution.

I tried a few things—each of which I was just sure would work but did not. So, I wanted to see what was happening in ggplot. My hunch proved correct. Your data is in order of value_yr in the png, not year.

I repeat this question at the end:

Either you can put the animation in order of value_yr or you can put the color in ggplot in order by year. Which would you prefer?

How do I know? I extracted the assigned colors in the object.

tellMe <- ggplot_build(temp)$data[[1]]
head(tellMe)
#    colour x           y group PANEL flipped_aes size linetype alpha
# 1 #1E60A4 1 -1.75990067     1     1       FALSE  0.6        1    NA
# 2 #1E60A4 2 -0.08968196     1     1       FALSE  0.6        1    NA
# 3 #1E60A4 3 -0.69657130     1     1       FALSE  0.6        1    NA
# 4 #1E60A4 4 -0.10777727     1     1       FALSE  0.6        1    NA
# 5 #1E60A4 5  1.57710505     1     1       FALSE  0.6        1    NA
# 6 #1E60A4 6  1.63277369     1     1       FALSE  0.6        1    NA 

gimme <- tellMe %>% group_by(group) %>% 
  summarise(color = unique(colour)) %>% 
  print(n = 100) # there are less than 100, I just want them all

head(gimme)
# # A tibble: 6 × 2
#   group color  
#   <int> <chr>  
# 1     1 #1E60A4
# 2     2 #114781
# 3     3 #175290
# 4     4 #053061
# 5     5 #1C5C9E
# 6     6 #3E8BBF 

To me, this indicated that the colors weren't in group order, so I wanted to see the colors to visualize the order.

I used this function. I know it came from a demo, but I don't remember which one. I looked just so I could include that here, but I didn't find it.

# this is from a demo (not sure which one anymore!
showCols <- function(cl=colors(), bg = "lightgrey",
                     cex = .75, rot = 20) {
  m <- ceiling(sqrt(n <-length(cl)))
  length(cl) <- m*m; cm <- matrix(cl, m)
  require("grid")
  grid.newpage(); vp <- viewport(w = .92, h = .92)
  grid.rect(gp=gpar(fill=bg))
  grid.text(cm, x = col(cm)/m, y = rev(row(cm))/m, rot = rot,
            vp=vp, gp=gpar(cex = cex, col = cm))
}

showCols(gimme$color)

The top left color is the oldest year, the value below it is the following year, and so on. The most recent year is the bottom value in the right-most column.

enter image description here

df %>% group_by(yr) %>% summarise(value_yr = unique(value_yr))
# they are in 'value_yr' order in ggplot, not year
# # A tibble: 71 × 2
#       yr value_yr
#    <int>    <dbl>
#  1  1950  0.0380 
#  2  1951 -0.215  
#  3  1952 -0.101  
#  4  1953 -0.459  
#  5  1954 -0.00130
#  6  1955  0.559  
#  7  1956 -0.457  
#  8  1957 -0.251  
#  9  1958  1.10   
# 10  1959  0.282  
# # … with 61 more rows 

Either you can put the animation in order of value_yr or you can put the color in ggplot in order by year. Which would you prefer?



Update

You won't use transition_reveal to group and transition by the same element. Unfortunately, I can't tell you why, but it seems to get stuck at 1958!

To make this gif on the left match that ggplot png on the right:

enter image description hereenter image description here

First, I modified the calls to ggplot and geom_line

  ggplot(aes(x = month(date, label = T), y = value, 
             group = yr, color = yr))  
  geom_line(size = .6)

Then I tried to use transition_reveal but noticed that subsequent years were layered underneath other years. I can't account for that odd behavior. When I ran showCol after changing temp, the colors were in order. That ruled out what I had thought the problem was initially.

I modified the object anim, using transition_manual to force the order of the plot layers.

anim <- temp  
  transition_manual(yr, cumulative = T)  
  ease_aes('linear')

That's it. Now the layers match. As to whether this would have worked before you changed the color assignment: original plot with manual transitions of the year on the left, ggplot png on the right:

enter image description hereenter image description here

It looks like that would've have worked, as well. So, my original drawn-out explanation wasn't nearly as useful as I thought, but at least you have a working solution now. (Sigh.)

  • Related