使用 ggplot2 更改 Robinson 投影的中央子午线时的视觉错误 [英] Visual bug when changing Robinson projection's central meridian with ggplot2

查看:76
本文介绍了使用 ggplot2 更改 Robinson 投影的中央子午线时的视觉错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在中央子午线不同于 0 的罗宾逊投影中投影世界地图.根据 和效果差不多.

我尝试在 Mac OS X 和 Ubuntu 18.04 上的两个独立 R 安装上运行我的代码段.

解决方案

跨越子午线的多边形在转换后会一直拉伸到整个地图.解决此问题的一种方法是将这些多边形从中间拆分,以便所有多边形都完全位于线的西侧或东侧.

# 定义一个长 &与子午线重叠的细长多边形将其 CRS 设置为匹配#那个世界多边形 <- st_polygon(x = list(rbind(c(-0.0001, 90),c(0, 90),c(0, -90),c(-0.0001, -90),c(-0.0001, 90)))) %>%st_sfc() %>%st_set_crs(4326)# 修改世界数据集以移除与世界多边形重叠的部分world2 <- world %>% st_difference(polygon)# 对世界数据集的修改版本进行转换world_robinson <- st_transform(world2,crs = '+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')# 阴谋ggplot() +geom_sf(data = world_robinson)

I am attempting to project a world map in a Robinson projection where the central meridian is different from 0. According to this StackOverFlow thread, it should be an easy thing (albeit the example uses sp).

Here is my reproducible code:

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

world <- ne_countries(scale = 'small', returnclass = 'sf')

# Notice +lon_0=180 instead of 0
world_robinson <- st_transform(world, crs = '+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')

ggplot() +
geom_sf(data = world_robinson)

This is the result. Polygons are closing themselves from one side to the other of the projection.

Trying with sp gives the same effect. I also tried with a shapefile including only polygons from coastlines (no political borders) from http://www.naturalearthdata.com/ and the effect is similar.

I tried to run my snippet on two independent R installations on Mac OS X and Ubuntu 18.04.

解决方案

Polygons that straddle the meridian line get stretched all the way across the map, after the transformation. One way to get around this is to split these polygons down the middle, so that all polygons are either completely to the west or east of the line.

# define a long & slim polygon that overlaps the meridian line & set its CRS to match
# that of world
polygon <- st_polygon(x = list(rbind(c(-0.0001, 90),
                                     c(0, 90),
                                     c(0, -90),
                                     c(-0.0001, -90),
                                     c(-0.0001, 90)))) %>%
  st_sfc() %>%
  st_set_crs(4326)

# modify world dataset to remove overlapping portions with world's polygons
world2 <- world %>% st_difference(polygon)

# perform transformation on modified version of world dataset
world_robinson <- st_transform(world2, 
                               crs = '+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')

# plot
ggplot() +
  geom_sf(data = world_robinson)

这篇关于使用 ggplot2 更改 Robinson 投影的中央子午线时的视觉错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆