NB: http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html has a complete tutorial on this, if you’d like more of a walkthrough. But here are a couple ways to make maps of the U.S. to get you started.
The packages you are going to need to install to run this tutorial are: tidyverse, maps, and ggmap (remember, that’s by running install.packages("tidyverse") once and then library(tidyverse) every time you run the code). Otherwise, you should be able to just copy and paste this code to get everything to run! Let me know if you have issues.
usa <- map_data("usa") # pull map data
# Here is a little table of example location data you want to plot.
Locations <- tribble(
~Location_code, ~Latitude, ~Longitude,
#-------------|-----------|-----------
"ABBI", 49.869621, -111.377099,
"COFC", 40.65167, -104.999168,
"COFR", 39.17892, -108.700073,
"IDKI", 42.55103, -114.340004,
"IDPA", 43.66495, -116.520451,
"MISA", 43.39521, -83.686963,
"MOCO", 38.943, -92.319914,
"MTSI", 47.72635, -104.150097,
"NESB", 41.94255, -103.814399,
"NMFA", 36.68915, -108.307665,
"SKOU", 51.47894, -107.050903,
"WAOT", 46.79111, -119.033087,
"WYPO", 44.77592, -108.758948,
"AZBO", 32.451195, -109.958609,
"NYFR", 42.52198, -76.330525,
"TXLU", 33.6915, -101.826086,
"ONEX", 43.31734, -81.506848,
"CACH", 39.6903, -121.823782,
"CADV", 38.53782, -121.756973,
"KSTR", 38.47032, -101.77941,
"NDFA", 46.8994, -96.811921,
"CAIR", 33.6884, -117.722118
)
ggplot() + geom_polygon(data = usa, aes(x=long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_point(data = Locations, mapping = aes(x = Longitude, y = Latitude), color = "red")
That’s all there is to it! coord_fixed() here is setting a nice aspect ratio for your map.
Ok, it’s nice that we can plot a polygon (ok, a really sophisticated polygon) in ggplot, but what if we want to add other features to the map? Then you can use the ggmap package, which pulls data from google and/or other mapping sources.
bat_location <- geocode("Congress Avenue Bridge, Congress Avenue, Austin, TX")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Congress%20Avenue%20Bridge,%20Congress%20Avenue,%20Austin,%20TX&sensor=false
pat_location <- geocode("2415 Speedway, Austin, TX 78712", source = "google")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=2415%20Speedway,%20Austin,%20TX%2078712&sensor=false
ggmap(get_map("austin", zoom = 13), ylab = "Latitude", xlab = "Longitude") +
geom_point(data = bat_location, size = 7, shape = 13)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=austin&zoom=13&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=austin&sensor=false
ggmap(get_map("University of Texas at Austin", zoom = 15), ylab = "Latitude", xlab = "Longitude") +
geom_point(data = pat_location, size = 7, shape = 13, color = "darkorange3")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=University+of+Texas+at+Austin&zoom=15&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=University%20of%20Texas%20at%20Austin&sensor=false
ggmap(get_map(location = c(lon = -100, lat = 45), zoom = 4), ylab = "Latitude", xlab = "Longitude") + geom_point(data = Locations, mapping = aes(x = Longitude, y = Latitude), color = "blue")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=45,-100&zoom=4&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
Unfortunately ggmap doesn’t seem to support non-square maps yet, so that’s a reason you may prefer to stay with the maps package.