使用 ggplot 在 R 中绘制地形底图没有 Google API 密钥

问题描述

我希望创建北半球的基本地形底图,以便稍后添加经纬度点。我不想使用 Google Cloud 服务通过 API 执行此操作,但我找不到一种无需 API 密钥即可绘制底图的可靠方法。我在下面尝试了“basemapR”包,但收效甚微:

#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))

我收到以下错误

curl::curl_download(url,destfile = tmp) 中的错误: 已达到超时:[] 连接在 10001 毫秒后超时

我对获取地形底图的其他方法持开放态度,我正在伸出援手,因为我觉得这应该有一个我找不到的简单解决方案。感谢您的帮助!

解决方法

不确定您是否需要任何特定的投影,但您应该能够从 map_data() 包中的 maps 获取地图数据。关于如何做到这一点,网上有一些方便的参考资料。类似地,包 rnaturalearthrnaturalearthdata 具有一些可用于创建地图的基本世界数据。

这是一个基于 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

有关其他一些不错的参考资料,您可以查看 this page on DataNoviathis one over at datavizpyr

,

经过@chemdork123 的一些研究和指导,我能够获得以下使用 ggmap 映射地形底图的解决方案。我还进一步在地形图上绘制了点。

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,pch= 21,size = 2,color = "black")+
              theme_bw() + 
              labs(x = "Longitude",y = "Latitude")

enter image description here