esp*_*lli 7 gis r ggplot2 r-sf
在绘制世界地图时,存在以下问题ggplot2:它使用相同的颜色为整个背景着色,包括实际上不是地球的一部分的绘图角落,请参阅下面的代码生成的快照(它使用了蒙太边缘)sfabd ggplot2版本但问题是通用的,请参阅下面提到的博客文章:
#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()
Run Code Online (Sandbox Code Playgroud)
为了能够很好地绘制地球的轮廓,在D3.js一个特殊的GeoJSON的type,{type: "Sphere"}已添加,看到这个线程,可以在行动中可以看出这里:它是在以下快照外部整个地球黑色边框:
唯一的把戏我发现R/ ggplot2是一个由Matt Strimas -麦基发表,在他的博客条目映射最长的各种商用航班R中,看到外框和经纬网部分和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()
Run Code Online (Sandbox Code Playgroud)
我得到的只是一个额外的"反子午线"(注意来自北极的线......)并且没有海洋填充#D8F4FF......并且底部的多边形是非常不规则的(D3.js大师做了一些聪明的自适应重新采样以提高投影线的准确性......)
关于我试图获取ggplot2世界地图的整个世界多边形有什么不对的想法?(感谢您阅读此内容!)
根据戴夫的建议,我创建了一个足够小的新格线,以便凸壳操作将导致足够接近地球边界.以下代码给出了非常好的结果(见图后):
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1041 次 |
| 最近记录: |