Baseball SeRies: Using maps to plot Baseball Data in R.

Days back I was wondering how I could plot some data on a map using R. Luckily I was able to find some good information by querying Google and Stack Overflow. I mixed the info I got from those sites with some baseball data and now I’m showing you the results!

First of all, make sure you have the required packages to plot the map:

After that go to the Global Administrative Areas webpage and download a map from the GADM spatial database.  Please note that since I’m doing some MLB stuff in R for  I downloaded the level 1 USA map in an R Spatial Polygons Data Frame format.  If you need a more (or less) detailed map  please check the different map levels offered in the website.

Once you downloaded the file, push it into the R environment using readRDS and transform it into a data.frame using ggplot2::fortify. Note that fortify will transform the spatial object into a data.frame that ggplot2 can understand.  Also, please take a close look at line 8. There I’m a creating a new column in the map dataset that contains the state names without any punctuation character. This is helpful since eventually you will want to join data by country( cities, provinces, etc. ) but  R joins might not work properly with punctuation characters  because of encoding stuff. Line 13 just removes Alaska and Hawaii from my map.

Once your map data is ready, it’s time to load some other data you would like to show in your plot. In this case I got the MLB players’ place of birth data from Baseball-Reference and I loaded into R. Kindly note that line 9 does the same string conversion I did before for the map data.

Now that all of our data is ready, we are ready to join it:

And plot it:

MapaYou can access the full code from here.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s