Recently, I’ve been working on analysis of geospatial data. I found that there was a bit of a sharp learning curve just to be able to do some preliminary visualizations of spatial data, so this post will be a tutorial in some simple map visualizations with R. Note that this is really just useful for exploratory visual analysis.
I’ve been working with data over the entire US with data points for each county. You could do the same sort of thing as I am doing here if you have a list of data values with latitude and longitude coordinates or county (or state) FIPS codes.
For now, let’s just look at county populations. I’m going to map these data by the location of county population centroid (so by latitude and longitude coordinates, ie points). You could also look at the data by county polygons given by a shapefile (by FIPS code), but I’ll save choropleth mapping for another post.
First, I’m going to grab some public data on the county populations and locations of county population centroids in the US from the 2010 census.
Let’s look at the data.
Restrict to the continental United States.
Now that I have a dataframe of latitude and longitude points, I’ll drop some columns and convert to a spatial object in R.
Now I have a SpatialPointsDataFrame object. Great! I’d like to plot it over a map of the US. So I’ll grab shapefile data also from the following link and unzip the contents into my working directory: http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_5m.zip
I’ll manipulate this data a little bit to plot nicely.
Make plots of the US population. I like to use ggplot2, since I think those plots look the nicest.
And that’s how you overlay a map of data values with latitude and longitude coordinates on a continental US map using R’s ggplot2. Happy mapping!!