ggplot 和 geom_sf 未在世界地图上显示刻度和刻度线

时间:2021-07-12 06:48:15

标签: r ggplot2 sf

我正在尝试使用自然地球数据和 ggplot2 获取世界地图。但是,经纬网和轴刻度线和标签未显示在世界地图的 x 轴上。 AFAIK 这个问题曾经用 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 个答案:

答案 0 :(得分:2)

尝试在 s2 包上停用 sf

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