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

How to get ride of polygon borders using geom_sf in ggplot2

此问题之前已在 中提出过,但已接受的答案目前在当前版本的 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")

我的问题是:如何摆脱每个国家的边界​​?

主要解法:color = NA

ggplot2中,绘图对象的边框由color参数控制,可以在geom_sf中的color参数中设置NA顺序指示 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")

替代: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")

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   

请注意,显然不建议使用 lwd = 0,根据 @dc37,color = NA 是正确的选项。 This 对我为此打开的问题的回应解释了为什么以及为什么不同的用户有不同的结果:

Setting size to 0 is not recommended as it does not necessarily remove the border. This is outside the control of ggplot2 but is up to the graphic device (hence the system dependency). The recommendation is that a lad of 0 should result in the thinnest possible line but not all devices honours that.

Set colour to NA instead