繁体   English   中英

如何用 sf 放置多边形周围的地理坐标?

[英]How to place the geographical coordinates around polygon with sf?

我有这个以下多边形。

library(ggplot2)
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE

poly <- st_polygon(list(rbind(
  c(-90, 70),
  c(-40, 70),
  c(-40, 74),
  c(-90, 74),
  c(-90, 70)
))) |>
  st_sfc() |>
  st_segmentize(5) |>
  st_set_crs(4326) |>
  st_as_sf() |>
  st_transform(3413) |>
  st_cast("POLYGON")

ggplot() +
  geom_sf(data = poly) +
  theme(
    panel.background = element_blank()
  )

是否可以按照多边形(而不是绘图区域)的“形状”放置坐标标签?

创建于 2023-01-11,使用reprex v2.0.2

这在 ggplot 本身是不可能的,但是使用geomtextpath绘制轴是可行的:

library(geomtextpath)

xvals <- seq(-90, -40, 10)

yvals <- c(70, 72, 74)

xaxis <- lapply(xvals, function(x) {
  st_linestring(cbind(c(x - 5, x + 5), c(69, 69)))})|>
  st_sfc() |> 
  st_set_crs(4326) |>
  st_transform(crs = 3413) |>
  st_as_sf() |>
  within(label <- as.character(xvals))

yaxis <- lapply(yvals, function(x) {
  st_linestring(cbind(c(-93, -91), c(x, x)))})|>
  st_sfc() |> 
  st_set_crs(4326) |>
  st_transform(crs = 3413) |>
  st_as_sf() |>
  within(label <- as.character(yvals))

ggplot() +
  geom_sf(data = poly) +
  geom_textsf(data = xaxis, aes(label = label), linewidth = NA) +
  geom_textsf(data = yaxis, aes(label = label), linewidth = NA) +
  coord_sf(crs = 3413) +
  theme_void()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM