8

在绘制世界地图时,有一个问题ggplot2:它用相同的颜色为整个背景着色,包括实际上不属于地球的图的角落,请参见下面由以下代码生成的快照(它使用前沿sfabdggplot2版本,但问题是通用的,请参阅下面提到的博客文章):

    #install.packages("devtools")
    #devtools::install_github("tidyverse/ggplot2")
    #devtools::install_github("edzer/sfr")

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

    theme_map <- function(...) {
      theme_minimal() +
      theme(
        text = element_text(family = "Ubuntu Regular", color = "#22211d"),
        axis.line = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.grid.minor = element_line(color = "#ebebe5", size = 0.2),
        panel.grid.major = element_line(color = "#ebebe5", size = 0.2),
        plot.background = element_rect(fill = "#f5f5f2", color = NA),
        panel.background = element_rect(fill = "#f5f5f2", color = NA),
        legend.background = element_rect(fill = "#f5f5f2", color = NA),
        panel.border = element_blank(),
        ...
      )
    }

    crs <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +datum=WGS84 +units=m +no_defs"
    ctrys50m <- ne_countries(scale = 50, type = "countries", returnclass = "sf") %>%
       select(iso_a3, iso_n3, admin)

    ggplot() + 
      geom_sf(data = ctrys50m, alpha = 0.15, fill="grey") +
      coord_map() +
      coord_sf(crs = crs) +
      theme_map()

在此处输入图像描述

为了能够很好地绘制地球轮廓,在D3.js一个特殊的 GeoJSONtype{type: "Sphere"}添加了,请参阅此线程,它可以在此处看到:它是以下快照中的整个地球外部黑色边框:

在此处输入图像描述

我在/中发现的唯一技巧是 Matt Strimas-Mackey 在他的博客文章Mapping the Longest Commercial Flights in R中发布的技巧,请参阅边界框和标线部分以及and函数。Rggplot2make_bboxproject_recenter

这些是相当多的代码,我想知道一些 sfgeom_sf代码是否会使代码更干净/更简单,所以我尝试了:

    # whole world WSG84 bounding box
    sphere <- ne_download(category = "physical", type = "wgs84_bounding_box", returnclass = "sf")
    sphere_laea <- st_transform(sphere, crs)
    ggplot() + 
      geom_sf(data = sphere, fill = "#D8F4FF") +
      coord_sf(crs = crs) +
      geom_sf(data = ctrys50m, alpha = 0.15, fill="grey") +
      coord_map() +
      coord_sf(crs = crs) +
      theme_map()

我得到的只是一个额外的“反子午线”(注意来自北极的线......)并且没有充满海洋#D8F4FF......并且底部的多边形非常不规则(D3.js专家做了一些智能自适应重新采样以提高投影线的准确性...)

在此处输入图像描述

关于我尝试为 ggplot2 世界地图获取整个世界多边形的问题有什么想法吗?(感谢您阅读本文!)

4

2 回答 2

9

按照 Dave 的建议,我创建了一个足够小的新标线,以便凸包操作将产生足够好的地球边界近似值。以下代码给出了非常好的结果(见后图):

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

crs <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +datum=WGS84 +units=m +no_defs"

ctrys50m <- ne_countries(scale = 50, type = "countries", returnclass = "sf") %>%
  select(iso_a3, iso_n3, admin)

sphere <- st_graticule(ndiscr = 10000, margin = 10e-6) %>%
  st_transform(crs = crs) %>%
  st_convex_hull() %>%
  summarise(geometry = st_union(geometry))

ggplot()  +
  geom_sf(data = sphere, fill = "#D8F4FF", alpha = 0.7) +
  geom_sf(data = ctrys50m, fill="grey") +
  theme_bw()

在此处输入图像描述

于 2017-04-07T13:05:47.637 回答
3

我还在探索 的神奇之处sf,所以可能会有更直接的解决方案来解决您的问题。不过,您可能会发现这种方法很有用:

为了提取地球球体的多边形,只需使用所需投影的经纬网,创建一个凸包并合并生成的多边形。

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

crs <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000
        +datum=WGS84 +units=m +no_defs"

ctrys50m <- ne_countries(scale = 50, type = "countries", returnclass = "sf") %>%
  select(iso_a3, iso_n3, admin)

sphere <- st_graticule(st_transform(ctrys50m, crs = crs)) %>%
  st_convex_hull() %>%
  summarise(geometry = st_union(geometry))

ggplot()  +
  geom_sf(data = sphere, fill = "#D8F4FF", alpha = 0.7) +
  geom_sf(data = ctrys50m, fill="grey") +
  #coord_sf(crs = crs) + # not necessary because the crs from the first layer is being used.
  theme_bw()

这为您提供了以下情节,该情节仍然有一些可见的角落,但根据您的目的可能就足够了。

在此处输入图像描述

于 2017-04-06T09:20:05.323 回答