如何在 ggplot2 中使用 geom_sf 获得多边形边界

Mia*_*Cai 9 r ggplot2 r-sf

之前在旧线程中已经问过这个问题,但是接受的答案目前在 ggplot2 的当前版本中不再有效。这是一个最小的例子:

library(ggplot2)
library(rnaturalearth)
world = ne_countries(scale = "medium", returnclass = "sf")
ggplot(world) + 
  geom_sf(aes(fill = pop_est)) + 
  scale_fill_viridis_c(option = "plasma", trans = "sqrt")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我的问题是:我怎样才能摆脱每个国家的边界​​?

dc3*_*c37 12

主要解决方案: color = NA

在 中ggplot2,绘图对象的边框由color参数控制,您可以设置NAcolor参数geom_sf以指示ggplot2不绘制边框(实际上,边框会被绘图但不会归属颜色)。

library(ggplot2)
library(sf)
library(rnaturalearth)
library(rgeos)
world = ne_countries(scale = "medium", returnclass = "sf")
ggplot(world) + 
  geom_sf(aes(fill = pop_est), color = NA) + 
  scale_fill_viridis_c(option = "plasma", trans = "sqrt")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

选择: lwd = 0

或者,您可以使用 获得相同的结果lwd = 0,但是根据下面@caldwelist 的回答,不推荐使用此解决方案,它的成功与否取决于系统。

因此,在我的系统上,我能够通过应用删除边框 lwd = 0

library(ggplot2)
library(sf)
library(rnaturalearth)
library(rgeos)
world = ne_countries(scale = "medium", returnclass = "sf")
ggplot(world) + 
  geom_sf(aes(fill = pop_est), lwd = 0) + 
  scale_fill_viridis_c(option = "plasma", trans = "sqrt")
Run Code Online (Sandbox Code Playgroud)

R 会话信息

顺便提一下,我使用的是 ggplot2 (3.2.1) 的最新版本

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19.2

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rgeos_0.5-2          sp_1.3-2             sf_0.8-0             rnaturalearth_0.1.0 
 [5] lubridate_1.7.4      forcats_0.4.0        stringr_1.4.0        dplyr_0.8.3         
 [9] purrr_0.3.3          readr_1.3.1          tidyr_1.0.0          tibble_2.1.3        
[13] tidyverse_1.3.0      data.table_1.12.8    circlize_0.4.8       ComplexHeatmap_2.2.0
[17] lattice_0.20-38      ggplot2_3.2.1       

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3              class_7.3-15            png_0.1-7              
 [4] assertthat_0.2.1        zeallot_0.1.0           digest_0.6.23          
 [7] utf8_1.1.4              R6_2.4.1                cellranger_1.1.0       
[10] plyr_1.8.5              backports_1.1.5         reprex_0.3.0           
[13] rnaturalearthdata_0.1.0 e1071_1.7-3             httr_1.4.1             
[16] pillar_1.4.3            GlobalOptions_0.1.1     rlang_0.4.2            
[19] lazyeval_0.2.2          readxl_1.3.1            rstudioapi_0.10        
[22] GetoptLong_0.1.8        labeling_0.3            munsell_0.5.0          
[25] broom_0.5.3             compiler_3.6.2          modelr_0.1.5           
[28] pkgconfig_2.0.3         shape_1.4.4             tidyselect_0.2.5       
[31] viridisLite_0.3.0       fansi_0.4.1             crayon_1.3.4           
[34] dbplyr_1.4.2            withr_2.1.2             nlme_3.1-143           
[37] jsonlite_1.6            gtable_0.3.0            lifecycle_0.1.0        
[40] DBI_1.1.0               magrittr_1.5            units_0.6-5            
[43] scales_1.1.0            KernSmooth_2.23-16      cli_2.0.1              
[46] stringi_1.4.5           farver_2.0.3            reshape2_1.4.3         
[49] fs_1.3.1                xml2_1.2.2              vctrs_0.2.1            
[52] generics_0.0.2          rjson_0.2.20            RColorBrewer_1.1-2     
[55] tools_3.6.2             glue_1.3.1              hms_0.5.3              
[58] parallel_3.6.2          clue_0.3-57             colorspace_1.4-1       
[61] cluster_2.1.0           classInt_0.4-2          rvest_0.3.5            
[64] haven_2.2.0   
Run Code Online (Sandbox Code Playgroud)