如何在ggplot2中使用geom_sf获得多边形边框 [英] How to get ride of polygon borders using geom_sf in ggplot2

查看:245
本文介绍了如何在ggplot2中使用geom_sf获得多边形边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前在线程,但是当前接受的答案在ggplot2的当前版本中不再起作用.这是一个最小的示例:

This question has been asked before in an old thread, but the accepted answer does not currently work anymore in the current version of ggplot2. Here is a minimal example:

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

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

My question is: how can I get rid of the borders in each country?

推荐答案

主要解决方案:color = NA

Main Solution: color = NA

ggplot2中,打印对象的边框由color参数控制,您可以在geom_sf中将NA设置为color参数,以指示ggplot2不打印边框(实际上,则将绘制边框,但不会归因于颜色).

In ggplot2, borders of plotting objects are controlled by color parameter, you can set NA to the color parameter in geom_sf in order to indicate to ggplot2 to not plot borders (actually, the border will be plot but no color will be attributed).

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

Alternative: lwd = 0

或者,您可以使用lwd = 0来获得相同的结果,但是根据下面@caldwelist的回答,不建议使用此解决方案,其成功取决于系统.

Alternatively, you can obtain the same results by using lwd = 0, however according to @caldwelist's answer below, this solution is not recommended and it success will be system-dependent.

因此,在我的系统上,我可以通过应用lwd = 0

Thus, on my system, I was able to remove borders by applying 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)

Just to mention that I'm using the last version of 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   

这篇关于如何在ggplot2中使用geom_sf获得多边形边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆