ggplot和geom_sf在世界地图上不显示网格线和刻度标记。

3
我正在尝试使用naturalearth数据和ggplot2制作世界地图。 但是,纬线和轴刻度标记和标签在世界地图的x轴上没有显示出来。 据我所知,以前可以通过coord_sf(expand = FALSE)解决此问题(我有使用此选项的旧代码),但它似乎不再起作用了。 我已经找到了一种使用不同格式的解决方法,但更希望能够使用sf数据解决这个问题。 任何帮助都将不胜感激!
library(ggplot2) # version 3.3.4
library(rnaturalearth) # version 0.1.0
library(sf) # version 1.0-1

map <- rnaturalearth::ne_countries(scale = 110, returnclass = "sf")

ggplot() +
  geom_sf(data = map) +
  coord_sf(expand = FALSE) 

更改坐标范围时显示网格线

ggplot() +
  geom_sf(data = map) +
  coord_sf(expand = FALSE,
           xlim = c(0,50),
           ylim = c(0,50)) 
1个回答

6
尝试在sf包上停用s2的使用:
library(ggplot2)
library(rnaturalearth)
library(sf)

map <- rnaturalearth::ne_countries(scale = 110, returnclass = "sf")

# Deactivate s2
sf::sf_use_s2(FALSE)


ggplot(map) +
  geom_sf() +
  coord_sf(expand = FALSE)

enter image description here

sessionInfo()
#> R version 4.1.0 (2021-05-18)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19042)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=Spanish_Spain.1252  LC_CTYPE=Spanish_Spain.1252   
#> [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C                  
#> [5] LC_TIME=Spanish_Spain.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] sf_1.0-1            rnaturalearth_0.1.0 ggplot2_3.3.3      
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.6         pillar_1.6.1       compiler_4.1.0     highr_0.9         
#>  [5] class_7.3-19       tools_4.1.0        digest_0.6.27      lattice_0.20-44   
#>  [9] evaluate_0.14      lifecycle_1.0.0    tibble_3.1.2       gtable_0.3.0      
#> [13] pkgconfig_2.0.3    rlang_0.4.11       reprex_2.0.0       DBI_1.1.1         
#> [17] yaml_2.2.1         xfun_0.23          e1071_1.7-7        withr_2.4.2       
#> [21] styler_1.4.1       stringr_1.4.0      dplyr_1.0.7        knitr_1.33        
#> [25] rgeos_0.5-5        generics_0.1.0     fs_1.5.0           vctrs_0.3.8       
#> [29] classInt_0.4-3     grid_4.1.0         tidyselect_1.1.1   glue_1.4.2        
#> [33] R6_2.5.0           fansi_0.5.0        rmarkdown_2.8      sp_1.4-5          
#> [37] purrr_0.3.4        magrittr_2.0.1     units_0.7-1        backports_1.2.1   
#> [41] scales_1.1.1       ellipsis_0.3.2     htmltools_0.5.1.1  assertthat_0.2.1  
#> [45] colorspace_2.0-1   KernSmooth_2.23-20 utf8_1.2.1         proxy_0.4-25      
#> [49] stringi_1.6.2      munsell_0.5.0      crayon_1.4.1

1
此答案基于问题#4571。不再需要停用s2 - noriega

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接