【发布时间】:2021-10-18 17:43:57
【问题描述】:
下面的 R 代码可以在早期版本的 R 和 sf 上运行,但不能在我最近下载 R、RStudio 和必要软件包的另一台笔记本电脑上运行。对世界地图(sf 对象)使用“st_crop”函数会导致地图中只有水平线朝向北极。我只能通过首先将对象转换为“SpatialPolygonsDataFrame”并使用 sp 包中的“crop”函数对其进行裁剪来裁剪它。是否有错误,或者我在这里做错了什么?
版本
R 4.1.0 科幻小说 1.0.3 rnaturalearth 0.1.0 dplyr 1.0.7
这是可重现的代码
library(sp)
library(sf)
library(dplyr)
library(rnaturalearth)
library(raster)
library(ggplot2)
# World map (sf object)
world_sf <- ne_countries(scale = 10, type = "countries", returnclass = "sf") %>%
filter(!(admin == "Antarctica"))
#This code does NOT work
ext_world1 <- c(xmin = -165, xmax = 175, ymin= -59.47275, ymax = 83.6341)
world_sf1 <- st_crop(world_sf, ext_world1)
ggplot() + geom_sf(data = world_sf1, color="gray20", fill = "gray85", lwd = 0.3)
# This code produces the expected result
ext_world2 <- as(raster::extent(-165, 175, -59.47275, 83.6341), "SpatialPolygons")
world_sp <- crop(as_Spatial(world_sf), ext_world2)
world_sf2 <- st_as_sf(world_sp)
ggplot() + geom_sf(data = world_sf2, color="gray20", fill = "gray85", lwd = 0.3)
【问题讨论】: