Visualizing Location Data: From PDF Table to Google Map with R

Datetime:2016-08-23 01:45:39         Topic: R Program          Share        Original >>
Here to See The Original Article!!!

In 2014, Illinois passed into law the creation of a medical cannabis pilot program.  As my son has cancer and marijuana could greatly help with some of his symptoms, we eagerly applied for a card when registration was available early in 2015.  The first dispensaries were not available until November 2015 and there were only 9 dispensaries at the time, so the PDF file with a table of dispensary names and locations provided by the Illinois Department of Health was not all that difficult to use.

In the time that dispensaries have been available, my son has been in various hospitals and facilities in and around the city of Chicago.  First we were in Park Ridge, then the University of Chicago in Hyde Park, then Hinsdale, then downtown Chicago and he will soon be home in Oak Park.  As we moved around the city, I would use that same PDF file to locate the dispensary closest to me.  The list has grown from 9 names and addresses to 39 today, and I hope that number will continue to grow.  The PDF table format is not particularly useful for showing where the dispensaries are located.  The entries are listed in the order of the license issue date, making it all the more difficult to see which dispensaries might be easiest for me to visit.

So a few weekends ago I decided to create a map of all the current locations.  Keeping in mind that more dispensaries will be available in the future, I wanted to create code that would read the official list of registered dispensaries , so that updates would be easy as more entries were added. 

I knew I could read the text of the file in R using pdftools , and could put the locations onto a google map using googlevis .  The hardest part of the code was trying to filter out the noise included in the text and reliably get the name, address, and phone number of each dispensary into a data.frame.  A few handy gsub statements worked their magic and I was left with data ready for mapping.  

I added in some geocoding to get the longitude and latitude, thanks to this tip . This step is not really necessary as gvisMap can use addresses for location, but the map loads faster when provide with the lat:long format for the location. 

Finally, after the data manipulation, the code to produce the map is rather straightforward:

# create id  and LatLong for googleVis map
all$id <- paste(all$name, all$address, all$phone, sep='; ')
all$LatLong = paste(result$lat, result$long, sep=":")

# Now plot with googleVis

g1 <- gvisMap(all, "LatLong" , "id",
width=400, height=400

# this opens a browser with the plot

# write the code that will be used on my website
cat(g1$html$chart, file="dispensariesIL.html")

I thought the map might be useful for others in Illinois also interested in finding a dispensary, so I created a small website for the map, and included a few other tips and tricks I have learned along the way by being the procurer of medicinal cannabis in my household.     

Here is the resulting map:


Put your ads here, just $200 per month.