Adding list outside of ggplot2 which changes as time progresses - gganimate

Hi,

I have created an animated plot using ggplot2 and then gganimate. The plot shows points changing in time and I have highlighted in red, a point of interest, and in yellow, points that stick around with the red point over time.

I am trying to have outside of the plot on the right hand side, what the red and yellow points are (as in their identity in the dataframe) and change according to the time point. as you see some yellow points disappear etc - however I cannot make this work. I have tried geom_text(),`` labs() with no luck.

The code for the plot is:
labels = 'word1'
highlight_red<- words[words$X %in% labels,]#dataframe with word of interest

labels2<-c("word2", "word3", "word3",
"word4", "word5", "word6", "word7")
highlight_orange<-words[words$X %in% labels2,]#dataframe with word of interest

p <- ggplot(words, aes(x_coord, y=y_coord, col = time)) +
  theme_void()+
  labs(title ="{words$time[words$time == round(frame_time)][1]}") +
  theme(plot.title = element_text(size = 50, face = "bold", colour = "grey", margin = margin(t = 10, b = -20)))
  geom_point(alpha = 1)+
  geom_point(data=highlight_red, 
             aes(x_coord, y=y_coord), 
             color='red',
             size=3)+
  geom_point(data=highlight_orange, 
             aes(x_coord, y=y_coord), 
             color='orange',
             size=3, alpha=0.8)+
  guides(size=FALSE) +
  theme(legend.title = element_blank()) +
  coord_fixed(ratio = 1)

and animate:


p<-p+
  transition_time(age)+
  shadow_wake(wake_length = 0, alpha = FALSE)+
  enter_fade() + 
  exit_shrink()

animate(p, duration = 5, fps = 20, renderer = gifski_renderer())

attached is the gif. I would like the words of interest to cycle on the right hand side as they do on the scatter plot to reflect what is actually changing. I have tried adding code such as (to the main ggplot code)


# geom_text(aes(label = "{highlight_orange$X[highlight_orange$time == round(frame_time)][1]}"),
    #        hjust = -0.35,
     #       size = 3,
      #      check_overlap = TRUE)

with absolutely no luck. any help is much appreciated. thank you!
output

edit: the gif shows as embedded to post, however i don't see it! let me know if any issues

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.