Lets say I have four groups of numbers (groups 1-4) that have some x (1-10), y (1-10) and time (1-10 coordinates. However, groups 1 and 2 stop existing after time-point 5.
pt <- data.frame( xx = rep(1:10, 10), yy = jitter(rep(1:10, 10)), tt = rep(1:10, each = 40), gg = rep(1:4, each = 10) ) %>% filter(!(gg %in% c(1,2) & tt > 5)) %>% mutate(yy = yy + gg)
I create an animated plot, color coding the points by the group gg and animating by value tt
pp <- pt %>% ggplot(aes(x = xx, y = yy, fill = as.factor(gg), frame = tt, group = gg)) + geom_point(shape = 21, colour = "black", size = 2) + scale_fill_viridis_d() ppp <- ggplotly(pp) ppp
This gives me a plot that behaves as expected for time points 1-5
However, when the plot advances to time-point 6 two related strange things happen.
(1) The points from groups 1 and 2 move into the position into the position of the points from groups 3 and 4.
(2) The points that were originally from groups 3 and 4 remain frozen in their final positions. I am left with something that looks like this. With two points per group, only one of which moves correctly and the other of which is frozen in the position from time point 5.
If, instead of treating gg as a factor
fill = as.factor(gg) when I set up the plot, I instead leave it as a number
fill = gg I get the afformetntioned behavior (1) but not behavior (2). That is the points shuffle positions in a way that I do not want, but at least the old groups don’t hang around confusing the viewer.
Any suggestions about how I can resolve these two plotting issues? Thanks.