3

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
ike
  • 342
  • 3
  • 17
  • 2
    `+ guides(alpha = FALSE)` for the bonus. – Jonathan Carroll Feb 13 '17 at 05:07
  • that worked a charm, thanks – ike Feb 13 '17 at 14:40
  • diving in more here I *think* it might have something to do with the fact that i missed having +scale_x_log10() but adding that in resulted in the error "Error in seq.default(min, max, by = by) : 'from' cannot be NA, NaN or infinite", I've also tried giving that a range, +scale_x_log10(limits=c(1e-307,10000000000)) which resulted in a crazy wacky frame. I've also tried +scale_x_continuous() and a massive (incorrect) jump in counts – ike Feb 13 '17 at 15:52
  • my current working theory is that this is caused because the hex bins resize based on the furthest coordinates and since these are outside the frame the bins move but the map doesn't move to accommodate them. I'm testing by doing this without the map, if it works I might try enlarging the map... – ike Feb 13 '17 at 16:56
  • update: I've tried: 1) removing the na rows, 2) removing anything that fell outside the range of the map, 3) removing the map entirely 4) reversing the coordinates so that scale_x_log10() would work without protest. No dice. all of this seems to lock on having frame 9 (of 13) show up in all other frames. that seems to be the one that's creating the shadow effect. – ike Feb 13 '17 at 20:41
  • current code: p <- ggplot(data=newnolarank3, aes(frame = as.factor(b)))+coord_cartesian()+stat_bin_hex(data=newnolarank3,bins=50,aes(x=as.numeric(lon*-1), y=as.numeric(lat), alpha=as.numeric(0.75)))+theme(text=element_text(family="Avenir"), axis.line=element_blank(), axis.ticks = element_blank(), plot.title=element_text(hjust=0.5), axis.title=element_blank())+ggtitle("Number of Sign Ups")+ guides(alpha = FALSE)+scale_x_log10() – ike Feb 13 '17 at 20:42
  • 1
    Another note on the `alpha` legend - `aes()` is used for mapping aesthetics to *data*. Any aesthetics included in `aes()` will get a legend (or axis). Constants, like `alpha = 0.25`, don't depend on your data so you should put them *outside* of `aes()`, and then they *won't* be included in the legend by default - letting you skip the `+ guides(alpha = FALSE)`. – Gregor Thomas Feb 13 '17 at 23:44
  • Oh interesting. thank you for explaining the logic behind the functions. helpful insight. – ike Feb 14 '17 at 15:00
  • it appears part of the problem was caused by NAs in the frame column. that said with those removed the 'jittered' legend still occurs – ike Feb 14 '17 at 17:34

1 Answers1

0

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.

ike
  • 342
  • 3
  • 17