Search code examples
rggplot2ggmapgganimate

gganimate creating duplicate images


I've got a data frame that looks like the below:

head(newnolarank)

    lon      lat week b
1 -90.06445 29.97121    1 9
2 -90.06704 29.96944    1 9
3 -90.07495 29.96567    1 9
4 -90.07448 29.96621    1 9
5 -90.16480 29.91240    1 9
6 -90.04797 29.94557    1 9

my map was generated from the get_map function in ggmap

map <- get_map("New Orleans, LA", zoom=10, color="bw")

I used geom_hex to make a hex map

   p <- ggmap(map)+
coord_cartesian()+
stat_binhex(data=newnolarank,aes(x=lon, y=lat, alpha=0.5, frame = as.factor(b), cumulative = FALSE))+
scale_fill_continuous(low="#ACD9F4",high="#EC008C")+
theme(text=element_text(family="Avenir"), 
axis.line=element_blank(), 
axis.ticks = element_blank(), 
axis.text = element_blank(), 
plot.title=element_text(hjust=0.5),
 axis.title=element_blank())+
 ggtitle("Number of Sign Ups")

Then used gganimate to make a gif. Here in is the problem; the resulting gif seems to have the old images even though cumulative was set to false, which creates an undesired effect of overlaying hexs, or hexes showing up in odd areas.

gganimate(p, "gif1.gif", title_frame = TRUE)

Here's the GIF:

you can also see the overlay in the legends.

Bonus Question: If anyone could help me get rid of the legend for alpha that shows up, that'd be great as well.


Solution

  • forgot to post the solution here: turns out there were NAs in the data set for time, so gganimate added those to the first image and all others. basically it's a feature not a bug, and removing the NAs from the week column solved the issue.