thomasp85 / gganimate

A Grammar of Animated Graphics
https://gganimate.com
Other
1.95k stars 310 forks source link

`transition_reveal` + `geom_text` assume ordered column to work #323

Closed osiris08 closed 3 years ago

osiris08 commented 5 years ago

In order to reveal lines with correct geom_text label, transition_reveal needs the table to be ordered to work. In the following example, I shuffled the data, and it cannot show labels correctly.

It's very often data comes in with shuffled order, and it's difficult to know this assumption.

airq <- airquality %>% dplyr::sample_n(153)
airq$Month <- format(ISOdate(2004,1:12,1),"%B")[airq$Month]
ggplot(airq, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  geom_segment(aes(xend = 31, yend = Temp), linetype = 2, colour = 'grey') + 
  geom_point(size = 2) + 
  geom_text(aes(x = 31.1, label = Month), hjust = 0) + 
  transition_reveal(Day) + 
  coord_cartesian(clip = 'off') + 
  labs(title = 'Temperature in New York', y = 'Temperature (°F)') + 
  theme_minimal() + 
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5))
awgymer commented 5 years ago

I just found this same problem using transition_reveal(x) with geom_step and geom_point. With an unordered dataframe my animation had multiple points plotted in every frame, and the step path had a weird tail. Ordering the dataframe by my x column solved everything. Did I just miss this in the documentation or is it not stated?