當繪製世界地圖時,存在ggplot2
的問題:它使用相同顏色對整個背景着色,包括情節的實際上並不是全球範圍內,請參閱下面的代碼下面產生的快照(它使用bleading邊緣sf
ABD ggplot2
版本,但問題是通用的,見下文提到的博客文章):用於ggplot2(和sf)中的世界地圖的整個地球多邊形
#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=00 +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"}
已添加,看到this thread可以在動作here可以看出:它是外部地球整體黑色邊框下面的快照:
唯一招我發現在R
/ggplot2
是由Matt Strimas-Mackey在他的博客文章Mapping the Longest Commericial Flights in R中發佈的,參見Bounding box and graticules部分和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大師做了一些智能adaptive resampling增加了準確性的投影線......)
不好的事,我試圖得到GGPLOT2世界地圖整個世界多邊形任何想法? (感謝遠閱讀本!)
這是一個夢幻般的建議。我將使用'ne_countries(scale = 10,...)'有一個更好的多邊形,否則它是一個可行的解決方案。謝謝! – espinielli
我得到了非常好的結果,即沒有可見的角落,下面定義了'sphere':'sphere < - st_graticule(ndiscr = 10000,margin = 10e-6)%>%st_transform(crs = crs)%>%st_convex_hull ()%>%彙總(幾何= st_union(幾何))' – espinielli