绘制世界地图时,存在一个问题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
一个特殊的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] 删除。
我来说两句