Basemaps

Author

K. W. Bridges

Published

October 22, 2022

Getting an appropriate basemap is an essential first step in the map making process. The examples shown in this gallery show some of the variety you can get by using fairly simple R code.

Later, you’ll see that the specification of a basemap is made easier by using some functions. Here, we’re only focused on the general appearance of the basemaps. These examples will help you decide what type of basemap will best suit your project.

Setup & Defaults

Setup Information

Each section has its own setup and default information. This allows each section to be run independently. The duplication of code is a small price to be paid for the independence and clarity achieved by this repetition.

All use of Google maps requires the use of an API key. Getting this key is a one-time task. Store the key somewhere same and hidden from the outside.

Registering the Google Key needs to be done once each session.

Show the code chunk
## Libraries
library(readr)
library(ggmap)
library(ggplot2)
library(sitemaps)

## Initialize Google Map key; the is stored in a Project directory.
  My_Key <- read_file("P://Hot/Workflow/Workflow/keys/Google_Maps_API_Key.txt")

## Test if Google Key is registered
if (!has_google_key()){

  ## Register the Google Maps API Key.
  register_google(key = My_Key, account_type = "standard")
  } ## end Google Key test

Some Required and Useful Code

It is essential to include the code to set the parameters (site_styles()) and the Google Map hide codes (site_google_hides()).

This code block also defines a simple theme that removes the geographic coordinates from the map borders, places a black line around the map, and suppresses a default legend.

Show the code chunk
## Read in all the parameters
column <- site_styles()
hide   <- site_google_hides()

#################################################################

## Define a theme that removes the axis labels and 
## puts a border around the map. No legend.
simple_black_box <- theme_void() +
              theme(panel.border = element_rect(color = "black", 
                                   fill=NA, 
                                   size=2),
                    legend.position = "none")

Google Basemaps

The Google basemaps have annotations on top. There are ways to selectively limit the annotation. This capability is discussed later along with a few other features that make these maps more useful.

Map Type Appearance
roadmap Street map
terrain Shaded relief map
satellite Satellite image
hybrid Satellite image and street map

Here are a few examples that show some of the types of base maps. Note that the theme (“simple_black_box”), defined earlier, is used to simplify these maps.

Show the code chunk
map2 <- get_map(location = "Island of Oahu, Hawaii, USA", 
                zoom=10, 
                source="google",
                maptype="roadmap")

ggmap(map2) + simple_black_box

Figure 1: Oahu, Hawaii as a roadmap.

Show the code chunk
map3 <- get_map(location = "Island of Oahu, Hawaii, USA", 
                zoom=10, 
                source="google",
                maptype="terrain")

ggmap(map3) + simple_black_box

Figure 2: Oahu, Hawaii as a terrain map.

Show the code chunk
map5 <- get_map(location = "Island of Oahu, Hawaii, USA", 
                zoom=10, 
                source="google",
                maptype="satellite")

ggmap(map5) + simple_black_box

Figure 3: Oahu, Hawaii as a satellite map.

Show the code chunk
map1 <- get_map(location = "Island of Oahu, Hawaii, USA", 
                zoom=10, 
                source="google",
                maptype="hybrid")

ggmap(map1) + simple_black_box

Figure 4: Oahu, Hawaii as a hybrid map.

Google Map Simplifications

It is possible to “hide” different layers of annotations on Google Maps. This example (Figure 5), which shows the hide$all parameter is just one possibility. See the Google Maps Simplified section for examples of alternative layer hiding.

Show the code chunk
map <- get_googlemap(center = "Island of Oahu, Hawaii, USA", 
                zoom=10, 
                maptype="terrain",
                style=hide$all)

ggmap(map) + simple_black_box

Figure 5: Oahu, Hawaii as a terrain map with layers hidden.

Scale Differences with Google Maps

The large property shown in this example (Figure 6) houses the Huntington Library, Art Gallery and Botanical Garden. Zooming out, you see the surrounding neighborhood (Figure 7) and, with even more zoom, the city is shown (Figure 8) next to the adjacent mountains. Finally (Figure 9) you see much of the Los Angeles basin. The original site is now too small to be seen.

Show the code chunk
map10 <- get_map(location = c(lon=-118.11418, lat=34.12839),
                zoom=16,
                source="google",
                maptype="terrain")

ggmap(map10) + simple_black_box

Figure 6: Southern California at zoom = 16 on a terrain map.

Show the code chunk
map11 <- get_map(location = c(lon=-118.11418, lat=34.12839),
                zoom=14,
                source="google",
                maptype="terrain")

ggmap(map11) + simple_black_box

Figure 7: Southern California at zoom = 14 on a terrain map.

Show the code chunk
map12 <- get_map(location = c(lon=-118.11418, lat=34.12839),
                zoom=12,
                source="google",
                maptype="terrain")

ggmap(map12) + simple_black_box

Figure 8: Southern California at zoom = 12 on a terrain map.

Show the code chunk
map13 <- get_map(location = c(lon=-118.11418, lat=34.12839),
                zoom=10,
                source="google",
                maptype="terrain")

ggmap(map13) + simple_black_box

Figure 9: Southern California at zoom = 10 on a terrain map.

Stamen Basemaps

Similarly, the Stamen maps have very different appearances. These alternatives are shown in the next set of maps.

Some of the basemap alternatives, such as “watercolor,” (Figure 15) are very creative and might be interesting to use for non-technical applications.

In practice, Stamen maps of large areas may involve considerable downloading. It is suggested that they only be used for smaller areas.

Map Type Appearance
terrain-background Terrain without names
terrain Mountains with hill-shading, colored vegetation, and names
toner Black & white, minimalist without names
toner-background Black & white, minimalist without names
toner-lite Grey & white, minimalist with names
watercolor Colorful, abstract, without streets, place names, or borders
Show the code chunk
map8 <- get_map(location="Japan",
                zoom=5,
                source="stamen",
                maptype="terrain-background")

ggmap(map8) + simple_black_box

Figure 10: Japan with a Stamen terrain-background map.

Show the code chunk
map9 <- get_map(location="Japan",
                zoom=5,
                source="stamen",
                maptype="terrain")

ggmap(map9) + simple_black_box

Figure 11: Japan with a Stamen terrain map.

Show the code chunk
map12 <- get_map(location="Japan",
                zoom=5,
                source="stamen",
                maptype="toner")

ggmap(map12) + simple_black_box

Figure 12: Japan with a Stamen toner map.

Show the code chunk
map13 <- get_map(location="Japan",
                zoom=5,
                source="stamen",
                maptype="toner-background")

ggmap(map13) + simple_black_box

Figure 13: Japan with a Stamen toner-background map.

Show the code chunk
map10 <- get_map(location="Japan",
                zoom=5,
                source="stamen",
                maptype="toner-lite")

ggmap(map10) + simple_black_box

Figure 14: Japan with a Stamen terrain-lite map.

Show the code chunk
map14 <- get_map(location="Japan",
                zoom=5,
                source="stamen",
                maptype="watercolor")

ggmap(map14) + simple_black_box

Figure 15: Japan with a Stamen watercolor map.

These maps give a lot of options when you’re choosing a basemap.

Adding Place Names (preview)

Maps that don’t have names can be given the place names you want. This is an important part of the process in creating a custom map that best serves your visualization requirements.

There is a detailed treatment giving names in a later chapter (Chapter 6: Names). Here is a simple demonstration (Figure 16) that uses the last map (Figure 15).

You can see (in the code) that the names are provided in a table along with coordinate locations and a few style parameters. How these column values work will be explained later.

Show the code chunk
map14names <- read_csv(col_names = TRUE, file = 
    "text,          lat,      lon,       name_text_wrap, fontface
     Tokyo,         35.68668, 139.77796, NA,             plain
     Kyoto,         35.01866, 135.76851, NA,             plain
     Hiroshima,     34.45604, 132.47520, NA,             plain
     Sapporo,       43.14579, 141.34479, NA,             plain
     Sea of Japan,  39.94473, 134.04759, 9,              italic
     Pacific Ocean, 30.91643, 140.34591, 9,              italic")

ggmap(map14) +
  site_names(datatable = map14names) +
  simple_black_box

Figure 16: Japan with a Stamen watercolor map with place names.