I'd like to insert another column value of my data into a gganimate animation title.
Example, here the states level variable is x and I'd like to add to title variable y:
df <- tibble(x = 1:10, y = c('a', 'a', 'b', 'd', 'c', letters[1:5])) df A tibble: 10 x 2 x y <int> <chr> 1 1 a 2 2 a 3 3 b 4 4 d 5 5 c 6 6 a 7 7 b 8 8 c 9 9 d 10 10 e This works as expected:
ggplot(df, aes(x, x)) + geom_point() + labs(title = '{closest_state}') + transition_states(x, transition_length = 0.1, state_length = 0.1) This fails:
ggplot(df, aes(x, x)) + geom_point() + labs(title = '{closest_state}, another_var: {y}') + transition_states(x, transition_length = 0.1, state_length = 0.1) Error in eval(parse(text = text, keep.source = FALSE), envir) :
object 'y' not found
Also tried this, but y will not change:
ggplot(df, aes(x, x)) + geom_point() + labs(title = str_c('{closest_state}, another_var: ', df$y)) + transition_states(x, transition_length = 0.1, state_length = 0.1) Another option is to map y as the states level variable and use the frame variable instead of x, but in my application y is either a not-necessarily-unique character variable like above, or it is a numeric variable but again not-necessarily-unique and not-necessarily-ordered. In which case gganimate (or ggplot?) will order it as it sees fit, making the final result weird not ordered by x:
ggplot(df, aes(x, x)) + geom_point() + labs(title = '{frame}, another_var: {closest_state}') + transition_states(y, transition_length = 0.1, state_length = 0.1) So how to simply add the changing value of the un-ordered, not numeric, y variable?
Finally: This question was asked here but without a reproducible example so it was not answered, hoping this one is better.



