I’m a lifelong resident of NYC, and one of the (only) nice things about summer here is that every year as July 4th approaches, you’ll start seeing (and hearing) more and more fireworks in the sky. Some of these are from planned events, but the vast majority are…unauthorized.
Because they aren’t exactly looked kindly on by the city, there’s no registry of these - but because a lot of people don’t like them, there is a record of sorts – namely, calls to 311 complaining about them. And since NYC makes all 311 calls publically available, I thought it would be interesting to plot a selection of them - for the days leading up to and right after July 4th. And since I make almost everything a GIF, I thought I would make it a GIF. So here’s how I did it:
The code
I used quite a few libraries for this – aside from ggmap, readr and dplyr – I used one package that I knew about and already loved (gganimate, which makes creating GIFs easy), and one that I didn’t know about but now am obsessed with (ggimage, by Guangchuang Yu, which lets you plot icons or images directly from within ggplot2).
I then loaded the data from NYC Open Data, which is a pretty awesome data portal; among other things, it lets you do direct API calls to specific datasets. They have 311 calls from 2010 to the present - I downloaded only those where the complaint was “Illegal Fireworks.”
Note that the latitude and longitude here is the caller’s location - not the fireworks - but I’m assuming for these purposes that it’s closeish to where the fireworks are.
I then had to do a bit of data manipulation, mainly to do with dates. First, I limited the dataset to 2017 (though the dataset is actually updated daily, so you could recreate my plots for 2018 up to yesterday’s date).
More importantly though, I had to fix a “problem” with the 311 dataset, which is that the only dates represented are dates with complaints. That makes sense, of course – it’s a dataset of complaints – but I wanted to include days with no fireworks complaints as well, so my GIF wouldn’t skip over days. So I created a dataset of all the days in 2017, with the latitude and longitude of the fake complaints set to an arbitrary point within NYC (doesn’t matter because I’m not going to plot them), and appended them to the main dataset.
I decided points would be too boring, so I downloaded some firework icons (source at bottom of graph), and randomly assigned them to each complaint. I then assigned a blank image to the fake complaints so they wouldn’t show up.
I then loaded a map of NYC, and created my plot:
Finally, I used the gganimate package to make the GIF: