【发布时间】:2021-02-16 12:26:46
【问题描述】:
我有一组质心,其 XY 坐标与不同的组相关联。我想使用sf 在每组多边形周围绘制一个有序多边形。
这是一个可重现的例子:
library(ggplot2)
library(cowplot)
library(dplyr)
library(sf)
ex <- data.frame(rect = c(rep("A", 8), rep("B", 6)),
x = c(1.5, 2.5, 1.5, 2.5, 3.5, 1.5, 2.5, 3.5,
5.5, 5.5, 6.5, 6.5, 7.5, 7.5),
y = c(5.5, 5.5, 4.5, 4.5, 4.5, 3.5, 3.5, 3.5,
1.5, 2.5, 2.5, 3.5, 2.5, 3.5))
# Plot
plot_grid(
ggplot(ex, aes(x, y)) +
geom_tile(color = "black", aes(fill = as.factor(rect)), show.legend = FALSE) +
geom_point() +
labs(title = "ggplot2 only") +
coord_equal() +
theme_classic() +
theme(panel.border = element_rect(fill = NA)),
ex %>%
group_by(rect) %>%
arrange(x, y) %>%
st_as_sf(coords = c("x", "y")) %>%
summarise() %>%
st_cast(to = "POLYGON") %>%
# st_geometry() %>%
ggplot() +
geom_sf(aes(fill = as.factor(rect)), color = "black", show.legend = FALSE) +
labs(title = "with sf") +
theme_classic() +
theme(panel.border = element_rect(fill = NA)),
align = "h"
)
产生:
以及我想要的:
【问题讨论】: