In generating a plot using transition_layer in gganimate, I noticed that an additional layer is added of NA.
As a minimum example. Say I have 10 unique observations (labeled "obs") of x,y pairs for each of 5 groups (labeled "id").
library(tidyverse)
library(gganimate)
dat1 <- data.frame(expand.grid(id = seq(1,5,1),obs=seq(1,10,1))) %>%
mutate(x = runif(50,10,30),
y = runif(50,100,300))
I want to create a gganimate object that transitions over each of the 5 groups. Essentially, this would be the equivilant of creating a unique x,y scatter plot for each group and then combining those 5 plots into a GIF. I can do this with the transition_layer function in gganimate as:
p <- dat1 %>%
ggplot(aes(x,y))+
geom_point(data=filter(dat1,id==1))+
geom_point(data=filter(dat1,id==2))+
geom_point(data=filter(dat1,id==3))+
geom_point(data=filter(dat1,id==4))+
geom_point(data=filter(dat1,id==5))
layername <- c("one","two","three","four","five")
anim <- p + transition_layers(
layer_length = 5,
transition_length = 1,
keep_layers = FALSE,
from_blank = FALSE,
layer_order = NULL,
layer_names = layername
)+
ggtitle('Layers',
subtitle = '{closest_layer} of 5')
anim
The animation works as expected, transitioning through each of layers as can be seen in the title (e.g "one of 5", "two of 5",...,"five of 5"). However, at the end of the animation i get a break in the sequence. The layers go ..."five of 5", "one of 5", "NA of 5", "one of 5".
I'm not sure why the extra transitions in the animation are occurring.
Particularly since there are no NAs in the layers. Any idea why this may be occurring and how it can be fixed? Or maybe a better way to generate a similar output?
Edit: the layer object does not contain a layer for NA
anim$layers
[[1]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
[[2]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
[[3]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
[[4]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
[[5]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity