我正在尝试为此处ggplot
描述的创建一个新几何,同时对其进行调整以处理简单特征对象。
作为一个例子,让我们进行绘制一组点的凸包的相同练习。因此,我编写了一个新geom_envelope()
函数借用元素geom_sf()
和一个相应的GeomEnvelope
ggproto
对象,该对象执行覆盖该draw_group()
方法的计算(因为我想要一个多边形来代表完整的点集)。
但是,我必须遗漏一些东西,因为我无法绘制多边形。我已经尝试了一段时间,但我要么得到错误,要么没有绘制任何内容。
library(sf); library(ggplot2); library(dplyr)
Npts <- 10
pts <- matrix(runif(2*Npts), ncol = 2) %>%
st_multipoint() %>%
st_sfc() %>%
st_cast("POINT") %>%
st_sf()
GeomEnvelope <- ggproto(
"GeomEnvelope", GeomSf,
required_aes = "geometry",
default_aes = aes(
shape = NULL,
colour = "grey20",
fill = "white",
size = NULL,
linetype = 1,
alpha = 0.5,
stroke = 0.5
),
draw_key = draw_key_polygon,
draw_group = function(data, panel_params, coord) {
n <- nrow(data)
if (n <= 2) return(grid::nullGrob())
gp <- gpar(
colour = data$colour,
size = data$size,
linetype = data$linetype,
fill = alpha(data$fill, data$alpha),
group = data$group,
stringsAsFactors = FALSE
)
geometry <- sf::st_convex_hull(st_combine(sf::st_as_sf(data)))
sf::st_as_grob(geometry, pch = data$shape, gp = gp)
}
)
geom_envelope <- function(
mapping = aes(),
data = NULL,
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...) {
if (!is.null(data) && ggplot2:::is_sf(data)) {
geometry_col <- attr(data, "sf_column")
}
else {
geometry_col <- "geometry"
}
if (is.null(mapping$geometry)) {
mapping$geometry <- as.name(geometry_col)
}
c(
layer(
geom = GeomEnvelope,
data = data,
mapping = mapping,
stat = "identity",
position = position,
show.legend = if (is.character(show.legend))
TRUE
else
show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = na.rm,
legend = if (is.character(show.legend))
show.legend
else
"polygon",
...
)
),
coord_sf(default = TRUE)
)
}
ggplot(pts) + geom_sf() + geom_envelope() + theme_bw()
由reprex 包(v0.2.1)于 2019 年 4 月 23 日创建