thomasp85 / gganimate

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

Using gganimate's view_follow function with static bits #353

Closed sobradob closed 3 years ago

sobradob commented 5 years ago

Following the example set in the wiki I would like to create an animation with the view_follow function and static points/lines/labels.

The issue is that view_follow only works as expected once the static lines/labels are in view.

library(gganimate)

airq <- airquality
airq$Month <- format(ISOdate(2004,1:12,1),"%B")[airq$Month]

# modified code 
ggplot(airq, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  geom_segment(aes(xend = Day+1, yend = Temp), linetype = 2, colour = 'grey') + 
  geom_point(size = 2) + 
  geom_text(aes(x = Day+1.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))+
  view_follow(aspect_ratio = (16/9))+ # add view_follow()
  geom_vline(xintercept = 15)+ #added static point
  geom_label(aes(x = 15, y = 45, label = "text"), hjust = 1) #add a label

The desired behaviour would be for the line and label to "appear in view". How do I accomplish this?

PS: I asked this here but this is probably a better place.

thomasp85 commented 3 years ago

With the dev version you can set exclude_layer = c(5, 6) in the view (this is ignored in the cran version due to a bug)

You'll need to move the label into position or set a fixed ylim in the coord for the label to be visible