在绘制世界地图时,有一个问题ggplot2
:它用相同的颜色为整个背景着色,包括实际上不属于地球的图的角落,请参见下面由以下代码生成的快照(它使用前沿sf
abdggplot2
版本,但问题是通用的,请参阅下面提到的博客文章):
#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函数。R
ggplot2
make_bbox
project_recenter
这些是相当多的代码,我想知道一些
sf
或geom_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 世界地图获取整个世界多边形的问题有什么想法吗?(感谢您阅读本文!)