我有以下多边形。
\nlibrary(ggplot2)\nlibrary(sf)\n#> Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.1; sf_use_s2() is TRUE\n\npoly <- st_polygon(list(rbind(\n c(-90, 70),\n c(-40, 70),\n c(-40, 74),\n c(-90, 74),\n c(-90, 70)\n))) |>\n st_sfc() |>\n st_segmentize(5) |>\n st_set_crs(4326) |>\n st_as_sf() |>\n st_transform(3413) |>\n st_cast("POLYGON")\n\nggplot() +\n geom_sf(data = poly) +\n theme(\n panel.background = element_blank()\n )\nRun Code Online (Sandbox Code Playgroud)\n
是否可以按照多边形(而不是绘图区域)的 \xe2\x80\x9cshape\xe2\x80\x9d 的方式放置坐标标签?
\n创建于 2023-01-11,使用reprex v2.0.2
\n这对于 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()
Run Code Online (Sandbox Code Playgroud)