r - ggplot 和 geom_sf 未在世界地图上显示刻度线和刻度线
问题描述
我正在尝试使用 naturalearth 数据和 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))
解决方案
尝试停用s2
on sf
package 的使用:
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)
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
推荐阅读
- python - 使用诗歌从 conda-forge(例如 cartopy)安装预构建包,而不依赖 conda(仅使用通道)
- r - 如何计算每列的值> x的时间比例
- xamarin.ios - Xamarin IOS CoreML 模型更改多路器 (posenet)
- ruby - ruby rspec 单元测试中的 OpenSSL 版本号错误,连接到 docker hashcorp vault
- javascript - 加载视频时如何添加微调器?使用 Javascript
- r - 如何合并多个变量并使其中一个变量处于模糊匹配中
- javascript - 如何在jquery中的ajax加载数据中传递全局变量数据?
- python - 如何使散点图中的点尺寸逐渐减小(matplotlib)
- android - Kotlin 协程崩溃,没有有用的堆栈跟踪
- string - awk 混淆了两个包含数字的不同字符串