如何解决使用 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
获取地图数据。关于如何做到这一点,网上有一些方便的参考资料。类似地,包 rnaturalearth
和 rnaturalearthdata
具有一些可用于创建地图的基本世界数据。
这是一个基于 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")
有关其他一些不错的参考资料,您可以查看 this page on DataNovia 和 this 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")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。