Map Time: COMPLETE Map of US Hometowns of 2017 MLS Players (Phase 2)

What a difference a few hours can make. Curious about how I was going to find the latitudes and longitudes of the cities missing from us.cities in a way that wouldn't take the rest of my life, I starting poking around on the web. In relatively (and embarrassingly) short order, I discovered a way to use R to build a data frame of latitudes and longitudes for any cities that Google has mapped and to do so rapidly. Perfect! After building this data frame, I did have to make some modifications to my R code for the partial map I posted earlier, and after I had made some different aesthetic choices, this was the resulting COMPLETE map of US hometowns of current MLS players (as of Week 5):

The key to today's rapid progress was learning that ggmap has a geocode function that allows R to send city and state information to Google and obtain the latitude and longitude. The code below allowed R to send each city and state pair to Google, return the latitude and longitude, and add them as columns to my USISSL database (here labeled as "MLS_2017").

> lonlat <- cbind(MLS_2017, geocode(paste(MLS_2017$Home.Town, MLS_2017$Home.State)))

The rest of the R code that I used for the partial map had to be modified in part because the data frames were named differently and because I no longer needed to merge city and state information into one column in order to subset the data frame with the latitude and longitude columns. Bonus! (AMENDMENT: It turns out that I still had to do this merger in order to make sure that cities with the same name but in different states were not lumped together. The map above has been updated to reflect this.)

I decided to change the map from color to what is effectively a gray scale by adding color="bw" to the get_googlemap command. After creating the map using the different sized dots that I used in the partial map, I didn't like the way the dots looked given the overlaps of the 311 dots now included on the complete map. I abandoned differential dot size in favor of differential dot color, leading me to spend a great deal of time looking at color palettes. I settled on Set1 from the RColorBrewer package, which was actually one of the packages that I blindly included in my code for the partial map. I'm still not thrilled with this set of colors, but it was the best of the ones I tried. Increasing the dot sizes also helped to see the different colors better.

I'm really pleased with this map and my overall progress in just these few days.


Popular Posts