带有 sf 对象的 ggplot 标签轮廓
ggplot label contours with sf object
我有一个使用 st_read()
从 shapefile 导入的轮廓数据集。我使用 ggplot()
和 geom_sf()
绘制了此图。它呈现得很好。现在我想标记轮廓。使用 geom_st_label()
不会产生很好的输出。轮廓密集的地方和标签重叠。
我查看了 metR
包。它有一个 geom_contour_label()
函数,可以很好地控制 ggplot()
中的轮廓放置。但是,geom_contour()
和相关函数无法识别 sf_objects 中包含的几何图形。我收到此错误:stat_contour requires the following missing aesthetics: x, y and z
.
如何让 geom_contour_label()
与 sf 对象一起工作?这是 geom_contour_label()
可以产生的结果:
我的轮廓数据可从 https://cloudstor.aarnet.edu.au/sender/?s=download&token=241de91b-2015-4a19-a18f-c2125a12f2a7 获得。
library(sf)
library(tidyverse)
isobath <- read_sf("1misobath.shp")
ggplot() +
geom_sf(data = isobath, color = "blue", lwd = 0.25) +
geom_sf_label(data = isobath, aes(label = DEPTH), size = 2) +
coord_sf(xlim = c(18.42, 18.5), ylim = c(-34.20, -34.16), expand = T) +
theme_bw()
在深入研究 geom_contour()
函数之后。这是我对问题的理解。
函数 geom_contour()
不期望类型为 sf
的对象,而是类型为 dataframe
或 data.table
的对象,其中包含点行(不是您的情况下的线串) 包含三列:经度(即 X)、纬度(即 Y)和 Z 列(即,在您的情况下,这将是变量 DEPTH
)。 geom_contour()
函数将计算等值线并将它们绘制在地图上。
根据您的 sf
对象,我能够按照函数的预期创建具有 X、Y 和 Z 坐标的点数据框,但随后我遇到了一个无法解决的问题。您的数据网格是 rectilinear
,但函数需要 regular
类型的网格。因此,为了将网格从 rectilinear
转换为 regular
,我尝试使用 akima
包的 interp()
函数对这些点进行插值,但由于重复值,这是不可能的在 Z 中(即变量 DEPTH
)。
所以我改变主意了!最后,我想我通过使用非常好的 tmap
包的 tm_iso()
函数找到了一个非常简单的解决映射问题的方法。
请在下面找到显示代码和生成的地图结果的 reprex。
Reprex
library(sf)
library(s2)
library(tmap)
# 1. Your data
isobath <- st_read("D:/test/Projet_essai4/Isobath/1misobath.shp")
#> Reading layer `1misobath' from data source
#> `D:\test\Projet_essai4\Isobathmisobath.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 162 features and 9 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: 18.43944 ymin: -34.19143 xmax: 18.49996 ymax: -34.16805
#> Geodetic CRS: Cape
# 2. Visualization of your data with tm_iso() of the tmap package
tm_shape(isobath) +
tm_grid(col ="lightgray", line = TRUE) +
tm_iso(col = "blue", text = "DEPTH", fontface = "bold") +
tm_layout(frame = TRUE, inner.margins = 0)
由 reprex package (v0.3.0)
于 2021-10-31 创建
- 另一个版本,可能更接近您要查找的版本:
tm_shape(isobath) +
tm_grid(col ="white", line = TRUE) +
tm_iso(col = "blue", text = "DEPTH", fontface = "bold", bg.color = "lightgray", shadow = TRUE, size = 0.6) +
tm_layout(bg.color = "lightgray", frame = TRUE, inner.margins = 0)
由 reprex package (v0.3.0)
于 2021-10-31 创建
我有一个使用 st_read()
从 shapefile 导入的轮廓数据集。我使用 ggplot()
和 geom_sf()
绘制了此图。它呈现得很好。现在我想标记轮廓。使用 geom_st_label()
不会产生很好的输出。轮廓密集的地方和标签重叠。
我查看了 metR
包。它有一个 geom_contour_label()
函数,可以很好地控制 ggplot()
中的轮廓放置。但是,geom_contour()
和相关函数无法识别 sf_objects 中包含的几何图形。我收到此错误:stat_contour requires the following missing aesthetics: x, y and z
.
如何让 geom_contour_label()
与 sf 对象一起工作?这是 geom_contour_label()
可以产生的结果:
我的轮廓数据可从 https://cloudstor.aarnet.edu.au/sender/?s=download&token=241de91b-2015-4a19-a18f-c2125a12f2a7 获得。
library(sf)
library(tidyverse)
isobath <- read_sf("1misobath.shp")
ggplot() +
geom_sf(data = isobath, color = "blue", lwd = 0.25) +
geom_sf_label(data = isobath, aes(label = DEPTH), size = 2) +
coord_sf(xlim = c(18.42, 18.5), ylim = c(-34.20, -34.16), expand = T) +
theme_bw()
在深入研究 geom_contour()
函数之后。这是我对问题的理解。
函数 geom_contour()
不期望类型为 sf
的对象,而是类型为 dataframe
或 data.table
的对象,其中包含点行(不是您的情况下的线串) 包含三列:经度(即 X)、纬度(即 Y)和 Z 列(即,在您的情况下,这将是变量 DEPTH
)。 geom_contour()
函数将计算等值线并将它们绘制在地图上。
根据您的 sf
对象,我能够按照函数的预期创建具有 X、Y 和 Z 坐标的点数据框,但随后我遇到了一个无法解决的问题。您的数据网格是 rectilinear
,但函数需要 regular
类型的网格。因此,为了将网格从 rectilinear
转换为 regular
,我尝试使用 akima
包的 interp()
函数对这些点进行插值,但由于重复值,这是不可能的在 Z 中(即变量 DEPTH
)。
所以我改变主意了!最后,我想我通过使用非常好的 tmap
包的 tm_iso()
函数找到了一个非常简单的解决映射问题的方法。
请在下面找到显示代码和生成的地图结果的 reprex。
Reprex
library(sf)
library(s2)
library(tmap)
# 1. Your data
isobath <- st_read("D:/test/Projet_essai4/Isobath/1misobath.shp")
#> Reading layer `1misobath' from data source
#> `D:\test\Projet_essai4\Isobathmisobath.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 162 features and 9 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: 18.43944 ymin: -34.19143 xmax: 18.49996 ymax: -34.16805
#> Geodetic CRS: Cape
# 2. Visualization of your data with tm_iso() of the tmap package
tm_shape(isobath) +
tm_grid(col ="lightgray", line = TRUE) +
tm_iso(col = "blue", text = "DEPTH", fontface = "bold") +
tm_layout(frame = TRUE, inner.margins = 0)
由 reprex package (v0.3.0)
于 2021-10-31 创建- 另一个版本,可能更接近您要查找的版本:
tm_shape(isobath) +
tm_grid(col ="white", line = TRUE) +
tm_iso(col = "blue", text = "DEPTH", fontface = "bold", bg.color = "lightgray", shadow = TRUE, size = 0.6) +
tm_layout(bg.color = "lightgray", frame = TRUE, inner.margins = 0)
由 reprex package (v0.3.0)
于 2021-10-31 创建