使用 R 脚本创建新西兰静态地图

问题描述 投票:0回答:2

我正在尝试使用此脚本和 R 中的 ggplot2 创建新西兰地图

library(sf) #'simple features' package
library(leaflet) # web-embeddable interactive maps
library(ggplot2) # general purpose plotting
library(rnaturalearth) # map data
library(rnaturalearthdata)# map data
library(ggspatial) # scale bars and north arrows

#the package below was installed from source through github: 
# devtools::install_github("ropensci/rnaturalearthhires"))
library(rnaturalearthhires)# map data 

theme_set(theme_bw())

newzealand <- ne_countries(country="New Zealand", type="countries", scale='large', returnclass = "sf")

ggplot(data = newzealand) +
    geom_sf()

不幸的是,它不起作用,我得到的只是情节右下角的一个小版本(看起来它是在世界平面的比例尺上,见附图)。如果我与其他国家/地区一起调整脚本,它会完美运行。有人能帮我吗?我觉得我正在寻找的地图不存在于脚本使用的数据库中。

感谢所有能提供帮助的人。

我尝试运行脚本,但没有成功

r ggplot2 maps rnaturalearth
2个回答
0
投票

您可以设置

xlim
ylim
有适当的地图

library(sf) #'simple features' package
library(leaflet) # web-embeddable interactive maps
library(ggplot2) # general purpose plotting
library(rnaturalearth) # map data
library(rnaturalearthdata)# map data
library(ggspatial) # scale bars and north arrows

#the package below was installed from source through github: 
# devtools::install_github("ropensci/rnaturalearthhires"))
library(rnaturalearthhires)# map data 

theme_set(theme_bw())

newzealand <- ne_countries(country="New Zealand", type="countries", scale = 'large',  
                           returnclass = "sf")

ggplot(data = newzealand) +
  geom_sf() +
  xlim(166, 179) +
  ylim(-48, -34)


0
投票

我认为问题在于 NZ 多边形包含东经和西经 -180 的区域;因此,当您尝试在其 CRS(WGS 84,其经度从 -180 到 180)中绘制它们时,它们会分裂。解决方法可能是将多边形转换为自定义 CRS,将 180 设置为中央子午线。

# Bounding box
st_bbox(newzealand)
# X values extending from -177 to 178 
# Using proj string to center map on lon = 180
crs_cent180 <- st_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +lon_0=180")
nz2 <- st_transform(newzealand, crs_cent180)
ggplot(data = nz2) +
  geom_sf() 

© www.soinside.com 2019 - 2024. All rights reserved.