我有一個st_read()從 shapefile匯入的輪廓資料集。我已經使用ggplot()and繪制了這個圖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()
uj5u.com熱心網友回復:
geom_contour()深入研究功能后。以上是我對問題的理解。
該函式geom_contour()不期望型別為sfadataframe或 a的物件,data.table其中包含具有三列的點行(不是您的情況中的線串):經度(即 X)、緯度(即 Y)和 Z 列(即在您的情況下)情況下,這將是變數DEPTH)。geom_contour()然后,該函式將計算等值線并將它們繪制在地圖上。
從您的sf物件中,我能夠按照函式的預期創建具有 X、Y 和 Z 坐標的點的資料框,但隨后我遇到了一個無法解決的問題。您的資料網格是rectilinear,但該函式需要一個網格regular型別。因此,為了將網格從 轉換rectilinear為regular,我嘗試使用包的interp()函式對點進行插值,akima但由于 Z 中的重復值(即變數DEPTH),這是不可能的。
所以我改變了主意!最后,我想我通過使用tm_iso()非常好的tmap包的功能找到了一個非常簡單的解決您的映射問題的方法。
請在下面找到顯示代碼和生成的地圖結果的 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\Isobath\1misobath.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 包(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 包(v0.3.0)于 2021 年 10 月 31 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343313.html
