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?
Main Solution: color = NA
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")
Alternative: lwd = 0
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.
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 Session Info
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
Just to note, apparently usage of lwd = 0
is not recommended, and color = NA
as per @dc37 is the correct option. This response to an issue I opened for this explains why and why different users had different results:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With