KC Ray
KC Ray

Reputation: 87

Plotting a terrain basemap in R with ggplot (without a Google API key)

I am looking to create a basic terrain base map of the Northern Hemisphere to later add lat, long points to. I don't want to use Google Cloud services to do this with an API, but I can't find a reliable way to plot a base map without requiring an API key. I have tried the 'basemapR' package with little success below:

#devtools::install_github('Chrisjb/basemapR')
library(sf)
library(spData)
library(ggplot2)
library(basemapR)

world_data = spData::world

ggplot() +
  base_map(st_bbox(world_data),basemap = 'hydda', increase_zoom = 2) +
  geom_sf(data = world_data, col = 1, fill = NA)+
  coord_sf(xlim = c(-150,128), ylim = c(17,70))

I get the following error:

Error in curl::curl_download(url, destfile = tmp) : Timeout was reached: [] Connection timed out after 10001 milliseconds

I am open to other methods of getting a terrain base map and I am reaching out because I feel like this should have a simple solution that I cannot find. Thanks for your help!

Upvotes: 2

Views: 4252

Answers (2)

KC Ray
KC Ray

Reputation: 87

After some research and direction from @chemdork123 I was able to get the following solution for mapping a terrain basemap using ggmap. I also went further to plot points over the terrain map.

bbox <- c(left = -160, bottom = 15, right = 135, top = 70)

latitude = c(49.38639, 50.68870, 50.77530, 49.86880, 39.31181, 37.05229)          
longitude = c(-121.45063, -120.36646, 50, -97.40836, 76.71748, -119.19536)

site_df = as.data.frame(cbind(latitude,longitude))

site_map = ggmap(get_stamenmap(bbox, maptype = "terrain-background", zoom = 2))+
              geom_point(data = site_df, aes(x = longitude, y = latitude), 
               size = 1.5, color = "orange")+
              geom_point(data = site_df, aes(x = longitude, y = latitude), 
               pch= 21, size = 2, color = "black")+
              theme_bw() + 
              labs(x = "Longitude", y = "Latitude")

enter image description here

Upvotes: 1

chemdork123
chemdork123

Reputation: 13843

Not sure if you need any specific projection, but you should be able to grab the map data from map_data() from the maps package. There's some handy references online for how to do that. Similarly the package rnaturalearth and rnaturalearthdata have some basic world data you can use to create a map.

Here's an example based on this nice tutorial over at r-spatial.org.

library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)

world <- ne_countries(scale='medium', returnclass = 'sf')

ggplot(world) + geom_sf() + coord_sf(ylim=c(0,90)) + labs(title="The Northern Hemisphere")

enter image description here

For some other nice references, you can check out this page on DataNovia and this one over at datavizpyr.

Upvotes: 1

Related Questions