2017-04-04 6 views
4

세계지도를 그릴 때 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() 

enter image description here

: 세계는, 아래의 다음 코드에 의해 생성 된 스냅 샷 참조 (이 가장자리 sf ABD ggplot2 버전을 bleading 사용하지만 문제는 제네릭을, 아래에 언급 된 블로그 게시물을 참조) 멋지게 지구의 등고선을 그릴 수있다.,923,210 GeoJSON type, {type: "Sphere"} 특별한 행동 here에서 볼 수 this thread 참조 추가되었습니다

enter image description here

유일한 트릭 내가 찾은 : 그것은 다음과 같은 스냅 샷의 외부 전체 지구 검은 색 테두리가 R/ggplot2은 Matt Strimas-Mackey가 블로그 항목 Mapping the Longest Commericial Flights in R에 게시했으며 Bounding box and graticules 섹션과 make_bboxproject_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() 

" 반대로 자오선 "(북극에서 선을주의하십시오 ...) 그리고 no 대양#D8F4FF ... 로 채웠다 그리고 다각형은 바닥에 확실히 불규칙하다 (D3.js 지도자는 약간 똑똑한 adaptive resampling 정확도를 증가시켰다 투영 된 선의 ...)

ggplot2 세계지도에서 전체 폴리곤을 얻으려는 시도에있어 잘못된 점에 대한 아이디어가 있습니까? (지금까지 읽어 주셔서 감사합니다)

답변

6

, 나는 볼록 선체 작업이 세계의 국경의 충분히 좋은 근사 될 수 있도록 충분히 작은 새로운 계수 선을 만들었습니다.

library(ggplot2) 
library(sf) 
library(rnaturalearth) 
library(dplyr) 

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) 

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

enter image description here

3

나는 여전히 sf의 마법을 발견하는 중이므로 더 쉽게 문제를 해결할 수 있습니다. 그럼에도 불구하고이 방법이 유용 할 수 있습니다.

지구의 구체에 대한 다각형을 추출하려면 원하는 투영 계수 선을 사용하여 볼록한 선체를 만들고 결과 폴리곤을 결합하면됩니다.

library(ggplot2) 
library(sf) 
library(rnaturalearth) 
library(dplyr) 

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) 

sphere <- st_graticule(st_transform(ctrys50m, 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") + 
    #coord_sf(crs = crs) + # not necessary because the crs from the first layer is being used. 
    theme_bw() 

이 당신에게 여전히 눈에 보이는 모서리를 가지고 있지만 목적에 따라 충분할 수 있습니다 다음과 같은 플롯을 제공합니다. 데이브의 제안에 따라

enter image description here

+0

이 환상적인 제안이다 : 다음 코드는 매우 좋은 결과 (후 이미지 참조) 제공합니다. 더 좋은 다각형을 가지려면'ne_countries (scale = 10, ...) '을 사용 하겠지만, 그렇지 않으면 실용적인 해결책입니다. 감사! – espinielli

+0

다음과 같이 '구형'이라는 정의를 사용하여 눈에 잘 띄는 모서리가없는 매우 좋은 결과를 얻었습니다. 'sphere'- st_graticule (ndiscr = 10000, margin = 10e-6) %> % st_transform (crs = crs) %> % st_convex_hull() %> 요약 (geometry = st_union (geometry))' – espinielli