【问题标题】:Map with geom_sf in ggplot2 missing axis labels在 ggplot2 中使用 geom_sf 映射缺少轴标签
【发布时间】:2022-08-19 04:21:30
【问题描述】:

在使用sfgeom_sfggplot2 中创建地图时,我没有看到任何轴标签或刻度。

library(\"ggplot2\")
theme_set(theme_bw())
library(\"sf\")
library(\"rnaturalearth\")
library(\"rnaturalearthdata\")
world <- ne_countries(scale = \"medium\", returnclass = \"sf\")
ggplot(data = world) +
  geom_sf()

为了比较,this tutorial 显示了相同的图(即基于代码),带有标线和纬度、经度标签。为什么我什么都看不到?

sessionInfo() 以下:

R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sf_1.0-7      ggplot2_3.3.5

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8         latex2exp_0.9.3    compiler_3.6.3     pillar_1.7.0       RColorBrewer_1.1-2
 [6] hdf5r_1.3.5        class_7.3-15       tools_3.6.3        digest_0.6.29      bit_4.0.4         
[11] evaluate_0.14      lifecycle_1.0.1    tibble_3.1.6       gtable_0.3.0       pkgconfig_2.0.3   
[16] rlang_1.0.1        DBI_1.1.2          cli_3.2.0          yaml_2.2.2         xfun_0.29         
[21] fastmap_1.1.0      e1071_1.7-9        withr_2.4.3        stringr_1.4.0      dplyr_1.0.8       
[26] knitr_1.37         generics_0.1.2     vctrs_0.3.8        classInt_0.4-3     tidyselect_1.1.2  
[31] bit64_4.0.5        grid_3.6.3         glue_1.6.1         R6_2.5.1           fansi_1.0.2       
[36] rmarkdown_2.11     purrr_0.3.4        magrittr_2.0.2     units_0.8-0        scales_1.1.1      
[41] htmltools_0.5.2    ellipsis_0.3.2     colorspace_2.0-3   KernSmooth_2.23-16 utf8_1.2.2        
[46] proxy_0.4-26       stringi_1.7.6      munsell_0.5.0      crayon_1.5.0

    标签: r ggplot2 geospatial geom


    【解决方案1】:

    您可以使用以下代码:

    library("ggplot2")
    theme_set(theme_bw())
    library("sf")
    library("rnaturalearth")
    library("rnaturalearthdata")
    world <- ne_countries(scale = "medium", returnclass = "sf")
    ggplot(data = world) +
      geom_sf() +
      coord_sf(expand = FALSE) +
      scale_y_continuous(limits = c(-79, 79))
    

    输出:

    【讨论】:

    • 谢谢,但是,我发现使用更严格的纬度限制仍然会破坏 x 轴标签;例如,从您的示例中更改为:scale_y_continuous(limits = c(-60, 85))
    【解决方案2】:

    Disabling spherical geometry seems to be the solution.

    # Deactivate s2
    sf::sf_use_s2(FALSE)
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2021-07-23
      • 2020-02-28
      相关资源
      最近更新 更多