r - 使用 geom_sf() 绘制世界地图时缺少轴刻度和标签
问题描述
在绘制基本世界地图时,我没有得到纬度和经度以显示在轴上。我已经做了一个由geom_sf()
on cran 提供的例子,它确实显示了纬度。
这是我下面的代码。
library("ggplot2")
library("sf")
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
#> [1] "sf" "data.frame"
ggplot(data = world) +
geom_sf() +
coord_sf()
这是在轴上生成 lat long 的示例代码。
nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.6/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
ggplot(nc) + geom_sf()
解决方案
coord_sf()
仅当刻度线实际到达轴时才绘制轴刻度。在您的世界地图中,绘图面板扩展超出了地球的大小(您可以看到刻度线在绘图面板边缘之前结束),因此没有绘制轴刻度。
解决此问题的一种方法是关闭扩展。
library("ggplot2")
library("sf")
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
#> [1] "sf" "data.frame"
ggplot(data = world) +
geom_sf() +
coord_sf(expand = FALSE)
由reprex 包(v0.3.0)于 2019-11-02 创建
推荐阅读
- mysql - 如何在列初始记录和最终记录中创建数字序列?
- javascript - 来自 Lambda 的 AWS Cognito adminCreateUser,使用 Amplify CLI 创建
- sparql - Wikidata mwapi 同时搜索多个词条
- angular - 将数据发送到 Angular 工具提示指令
- json - 如何在 PL/SQL 中查询非标量 JSON 值
- kubernetes - 将 kubectl 上下文重置为 docker 桌面
- compiler-errors - 使用 ninja 编译时找不到 Python.h 文件
- javascript - 如何在最后链接一个 observable 、一个常规同步函数和另一个 observable
- sql - SQL 中迄今为止的时间戳
- spring - 如何获取和存储从下拉列表中选择的值到数据库?