0

I am working with R. Suppose I have the following data frame:

 my_data <- data.frame(
"col" = c("red","red","red","red","red","blue","blue","blue","blue","blue","green", "green", "green", "green","green"),
"x_cor" = c(1,2,5,6,7,4,9,1,0,1,4,4,7,8,2),
"y_cor" = c(2,3,4,5,9,5,8,1,3,9,11,5,7,9,1),
"frame_number" = c(1,2,3,4,5, 1,2,3,4,5, 1,2,3,4,5)
)

my_data$col = as.factor(my_data$col)
head(my_data)

       col x_cor y_cor frame_number
1  red     1     2            1
2  red     2     3            2
3  red     5     4            3
4  red     6     5            4
5  red     7     9            5
6 blue     4     5            1
  

In R, is it possible to create a (two-dimensional) graph that will "animate" each colored point to a new position based on the "frame number"?

For example: enter image description here

I started following the instructions from this website here: https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/

First, I made a static graph:

library(ggplot2)
library(gganimate)

p <- ggplot(
    my_data, 
    aes(x = x_cor, y=y_cor,  colour = col)

Then, I tried to animate it:

p + transition_time(frame_number) +
  labs(title = "frame_number: {frame_number}")

Unfortunately, this produced an empty plot and the following warnings:

There were 50 or more warnings (use warnings() to see the first 50)

1: Cannot get dimensions of plot table. Plot region might not be fixed
2: values must be length 1,
 but FUN(X[[1]]) result is length 15

Can someone please show me how to fix this problem?

Thanks

UseR10085
  • 7,120
  • 3
  • 24
  • 54
stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 2
    In `p`'s definition, isn't a `geom` layer missing? – Leonardo Hansa Jul 28 '21 at 06:26
  • 1
    Try `ggplot( my_data, aes(x = x_cor, y=y_cor, colour = col)) + geom_point() + transition_time(frame_number) + labs(title = "frame_number: {frame_time}")` – Jon Spring Jul 28 '21 at 07:50
  • @ Jon Spring: Thank you! This fixed the problem! Would you like to copy/paste this code into an answer so that future readers will know how to solve this problem? Or would you like me to do it? Please let me know. Thanks! – stats_noob Jul 28 '21 at 15:25
  • Can someone please take a look at this question? https://stackoverflow.com/questions/68564089/r-customizing-animation-text thanks! – stats_noob Jul 29 '21 at 16:48

0 Answers0