Basemaps in R - Earth analytics course module

Welcome to the first lesson in the Basemaps in R module. This module covers using ggmap to create basemaps in r / rmarkdown and how to overlay raster data on top of a hillshade.

Lesson 1. Add a basemap to an R Markdown report using ggmap

Learning objectives

After completing this tutorial, you will be able to:

  • Create a quick basemap using ggmap OR
  • Create a quick basemap using the maps package

What you need

You need R and RStudio to complete this tutorial. Also you should have an earth-analytics directory set up on your computer with a /data directory with it.

# install devtools
# install ggmap from dev space
# devtools::install_github("dkahle/ggmap")


Create basemap

First, create a basemap that shows the location of the stream gage in Boulder, Colorado.

myMap <- get_map(location = "Boulder, Colorado",
          source = "google",
          maptype = "terrain", crop = FALSE,
          zoom = 6)
# plot map

ggmap base plot

You can customize the base maps too - let’s change the GOOGLE basemap to satellite rather than terrain to see what happens.

myMap <- get_map(location = "Boulder, Colorado",
          maptype="satellite", crop=FALSE,
# plot map

ggmap base plot

There are many other basemap options. Check out the help for get_map by typing ??get_map into the R console.

Let’s check out the stamen watercolor maps. Notice that I zoomed out some here so you can see the full effect of the new basemap.

myMap <- get_map(location = "Boulder, Colorado",
          maptype="watercolor", crop=FALSE,
# plot map

ggmap base plot

Ok, enough of the map play time - let’s get back to business. Next, let’s add a point to our map representing the location of our actual stream gage data.

Latitude: 40.051667 Longitude: 105.178333

USGS gage 06730200 40°03’06” 105°10’42”

# add points to your map
# creating a sample data.frame with your lat/lon points
gage_location <- data.frame(lon = c(-105.178333), lat = c(40.051667))

# create a map with a point location for boulder.
ggmap(myMap) + labs(x = "", y = "") +
  geom_point(data = gage_location, aes(x = lon, y = lat, fill = "red", alpha = 0.2), size = 5, shape = 19) +
  guides(fill=FALSE, alpha=FALSE, size=FALSE)

ggmap with location point on it.

Alternative - maps package

If you can’t install ggmap, you can also create nice basemaps using the maps package. The maps package allows you to quickly create basemaps of study areas. It utilizes a set of vector based layers including layers that map:

  • countries across the globe
  • the United States and associated counties
  • and more

You can use the maps package, combined with the R base plot functions to add base layers to your map.


Create a basic map of the United States

# add a title to your map
title('Map of the United States')

vector map of the US

Plot using basemap - customize colors.

map('state', col = "darkgray",
# add a title to your map
title('Map of the United States')

vector map of the US with colors

Create a map of Colorado with county boundaries.

map('county', regions = "Colorado", col = "darkgray", fill = TRUE, border = "grey80")
map('state', regions = "Colorado", col = "black", add = T)
# add the x, y location of the stream guage using the points
# notice i used two colors adn sized to may the symbol look a little brighter
points(x = -105.178333, y = 40.051667, pch = 21, col = "violetred4", cex = 2)
points(x = -105.178333, y = 40.051667, pch = 8, col = "white", cex = 1.3)
# add a title to your map
title('County Map of Colorado\nStream gage location')

vector map of the CO with colors

You can stack several map layers using add=TRUE. Notice you can create multi-line titles using \n.

map('state', fill = TRUE, col = "darkgray", border = "white", lwd = 1)
map(database = "usa", lwd = 1, add = T)
# add the adjacent parts of the US; can't forget my homeland
map("state", "colorado", col = "springgreen",
    lwd = 1, fill = TRUE, add = TRUE)
# add gage location
title("Stream gage location\nBoulder, Colorado")
# add the x, y location of hte stream guage using the points
points(x = -105.178333, y = 40.051667, pch = 8, col = "red", cex = 1.3)

Create final map

Or you can plot with ggplot

usa <- map_data("usa")
##     long   lat group order region subregion
## 1 -101.4 29.74     1     1   main      <NA>
## 2 -101.4 29.74     1     2   main      <NA>
## 3 -101.4 29.65     1     3   main      <NA>
## 4 -101.4 29.64     1     4   main      <NA>
## 5 -101.3 29.63     1     5   main      <NA>
## 6 -101.3 29.64     1     6   main      <NA>

plot with ggplot.

ggplot() +
  geom_polygon(data = usa, aes(x = long, y = lat, group = group)) +

map of the United States

Leave a Comment