ggplot2(和SF)中世界地图的整个地球多边形

埃斯皮涅利

绘制世界地图时,存在一个问题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一个特殊的GeoJSON的type{type: "Sphere"}已添加,看到这个线程,可以在行动中可以看出这里:它是在以下快照外部整个地球黑色边框:

在此处输入图片说明

The only trick I found in R/ggplot2 is the one published by Matt Strimas-Mackey, in his blog entry Mapping the Longest Commericial Flights in R, see the Bounding box and graticules section and the make_bbox and project_recenter functions.

These are quite a lot of code and I was wondering whether some sf or geom_sf code would make to a cleaner/simpler code, so I tried:

    # 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()

What I get is just an extra "anti-meridian" (note the line from North pole...) and no oceans filled with #D8F4FF... And the polygon is quite irregular at the bottom (the D3.js gurus did some smart adaptive resampling to increase the accuracy of projected lines...)

在此处输入图片说明

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

埃斯皮涅利

按照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()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2以限制为中心的多边形世界地图提供有趣的边缘

世界地图上的边框和颜色ggplot2

R,ggplot2和点为六边形单元格的世界地图

ggplot2 绘制点和多边形的问题

在R中按世界地图裁剪空间多边形

使用ggplot2绘制颜色编码的世界地图

在ggplot2中绘制时缺少一些sf多边形

ggplot 和 geom_sf 未在世界地图上显示刻度和刻度线

如何在ggplot2中使用geom_sf获得多边形边框

R ggplot2与shapefile和csv数据合并以填充多边形-特定

使用ggplot2绘制具有多个子多边形和孔的SpatialPolygons

将世界地图划分为北温带ggplot2

使用ggplot2在世界地图上绘制英国的次区域

R:使用ggplot,geom_sf和openstreetmap绘制多边形的异常错误

ggplot和sf,用于覆盖两层多边形(.shp)

SF 和星星:多边形化分类栅格

ggplot2通过坐标填充shapefile中的多边形

ggplot2 Choropleth贴图中未显示区域多边形

r:在ggplot2中绘制带孔的多边形时撕裂

如何在ggplot2中测量多边形的面积?

使用 ggplot2 在点周围创建阴影多边形

使用geom_sf()绘制世界地图时缺少坐标轴刻度和标签

Google地图:多边形和标记Z-Index

在给定纬度和经度的情况下计算地球凸包多边形面积

从数据框创建SF多边形

关于点和多边形的chloropleth辅助图例,ggplot

带有 ggplot 的 R 误差条和多边形

您可以从ggplot2 :: geom_density_2d_filled图获得多边形坐标吗?

小世界地图?