【发布时间】:2020-11-04 22:27:51
【问题描述】:
假设我想为 geom_sf() 绘图添加标签,以获得以下信息:
截至 2020 年 11 月,该方法不存在,因此我收到警告,但尝试时未绘制任何内容:
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ggplot() +
geom_sf(data = nc, aes(label = CNTY_ID))
Warning message:
Ignoring unknown aesthetics: label
当尝试通过 geom_text() 手动添加标签时,我不知道如何从 nc 转到可以在ggplot:
如果我尝试以下操作,则输出不符合预期:
nc2 <- nc %>% st_centroid() %>% # ok, transform multypoligon to centroid
as.data.frame() # does not return something useful, WHY ?
但如果我这样做:
nc2 <- nc %>% st_centroid() %>%
as_Spatial() %>% # this is nonsense, why ?
as.data.frame()
现在,使用以下内容,我可以得到最初想要的带有适当标签的图。
ggplot() +
geom_sf(data = nc) +
geom_text(data=nc2, aes(coords.x1, coords.x2, label=CNTY_ID))
从 sf 到 tibble/data.frame 到的推荐方式是什么 格图?在我看来这是一项普通的任务,必须经过两个步骤 (as_Spatial() + as.data.frame()) 是错误的。
【问题讨论】:
-
你试过geom_sf_label吗? yutannihilation.github.io/ggsflabel/reference/…
-
@mrhellmann 当然试过了。我的机器上无法安装 0.0.1 版; (问题在这里打开:github.com/yutannihilation/ggsflabel/issues/3)但问题实际上是关于从 sf 到可以绘制的 tidy data
-
如果向 sf 对象(在非列表、非几何)列中添加 x 和 y 坐标是您正在寻找的内容,这可能会有所帮助:stackoverflow.com/a/61745776/7547327