将 sf 几何与变换后的几何相结合

Jon*_*ing 5 r ggplot2 r-sf

我正在用 R 复制xkcd 的“错误地图投影:ABS(经度)”。我有一个不错的开始,但我想以“正确”的方式进行。

在这里,我拿了一张世界地图并制作了一个带有水平反射的副本。然后我绘制两者并将观察窗口裁剪为正经度。

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

world <- ne_countries(scale = "small", returnclass = "sf") |>
  sf::st_as_sf() %>% 
  sf::st_set_crs(value = 4326) 

# matrix multiplication to reflect longitude
world_rev <- st_geometry(world)  * matrix(c(-1,0,0,1), 2, 2) 
world2 <- world |>
  st_set_geometry(world_rev) |>
  sf::st_as_sf() |>
  sf::st_set_crs(value = 4326) 

ggplot() +
  geom_sf(data = world, fill = "#336666", alpha = 0.5) +
  geom_sf(data = world2, fill = "#663366", alpha = 0.5) +
  coord_sf(xlim = c(0, 180), expand = 0)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这样做的一个缺点是我似乎无法应用不同的预测。例如,如果我以

coord_sf(crs= "+proj=robin", xlim = c(0, 180), expand = 0)
Run Code Online (Sandbox Code Playgroud)

我得到一个空白的情节。有没有更强大的方法来创建反转对象?

mar*_*usl 2

有点接近,但仍然不理想:

  • 将罗宾逊投影中心的经度从默认 0 移动到 90
  • 要处理“超出边缘”的几何形状,请将它们裁剪为 0 ... 180 经度
  • 为此,请禁用 s2
library(ggplot2)
library(sf)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(rnaturalearth)
library(rnaturalearthdata)

world <- ne_countries(scale = "small", returnclass = "sf") |>
  sf::st_as_sf() %>% 
  sf::st_set_crs(value = 4326) 

# matrix multiplication to reflect longitude
world_rev <- st_geometry(world)  * matrix(c(-1,0,0,1), 2, 2) 

world2 <- world |>
  st_set_geometry(world_rev) |>
  sf::st_as_sf() |>
  sf::st_set_crs(value = 4326) 

sf_use_s2(use_s2 = FALSE)
#> Spherical geometry (s2) switched off
bbox_0_180 <- st_bbox(c(xmin = 0, ymin = -90, xmax = 180, ymax = 90), crs = 4326)
world_crp  <- st_crop(world,  bbox_0_180)
world2_crp <- st_crop(world2, bbox_0_180)

ggplot() +
  geom_sf(data = world_crp,  fill = "#336666", alpha = 0.5) +
  geom_sf(data = world2_crp, fill = "#663366", alpha = 0.5) +
  coord_sf(crs= "+proj=robin +lon_0=90", expand = FALSE) +
  scale_x_continuous(breaks = seq(0,180,30))
Run Code Online (Sandbox Code Playgroud)

创建于 2023-07-27,使用reprex v2.0.2


你有空白情节

coord_sf(crs= "+proj=robin", xlim = c(0, 180), expand = 0)
Run Code Online (Sandbox Code Playgroud)

因为罗宾逊是投影的,单位是米,xlim也适用,你可以尝试这个:

coord_sf(crs= "+proj=robin", xlim = c(-18e6, 18e6), expand = 0)
Run Code Online (Sandbox Code Playgroud)

、 和worldworld2产生如下结果: 在此输入图像描述