I help out with a project called Foodborne Chicago, where we search twitter for people in Chicago mentioning “food poisoning” and @reply to them with a link where they can file a report with the city of Chicago’s Department of Public Health. The application has been live since March 2013. Since then, we have passed on about 125 reports to the city. For the majority of these reports, an inspection work order is generated and an inspection occurs within about a week of the incident submission.
So… I decided to take a look and see where these submissions were by plotting them on a map. To start, I had a file from our app with three columns, id which is a unique identifier of the submission, restaurant.address which is the address for the submission, and created.at which represents the date that the complaint was submitted. The first step to plotting these submissions on a map was to get the latitude and longitude for the restaurant addresses. I used the ggmap package for this. Originally, I had a custom function that used the free Google geocoding API and parsed the results, then I found the ggmap package and changed over. The results are the same, but it makes the code cleaner and easier to maintain.
Then, I Since I’ve used the OpenStreetMap package in R in another project, I started with that to make a map. The only gotcha here was that the OpenStreetMap package needs the points in mercator, so there’s a conversion step.
The ggmap package makes doing this sorta thing, in conjunction with ggplot2, very simple. Here’s the same map as above, using ggmap and Google maps.