在沃罗诺伊图中,点和图块之间存在一一对应关系,因此可以轻松地将填充美学映射到图块上。您数据框中的每一行都将有自己的图块,因此可以指定填充、透明度等。
但是,在德劳内三角剖分中情况并非如此,您数据框中的每一行描述一个单独的顶点,因此每个图块需要3行数据来描述。这3行具有不同的花瓣长度值,并且行与图块之间没有一一对应关系。
显然的选择是对三个顶点的花瓣长度取平均值,但似乎ggforce没有以这种方式插值顶点的选项。但是,我们可以使用deldir包以与ggforce相同的方式来实现这一点:
library(ggplot2)
library(deldir)
tri <- triang.list(deldir(iris$Sepal.Length, iris$Sepal.Width))
do.call(rbind, lapply(seq_along(tri),
function(x) {
data.frame(Sepal.Length = tri[[x]]$x, Sepal.Width = tri[[x]]$y,
Petal.Length = mean(iris$Petal.Length[tri[[x]]$ptNum]),
tri = x)
})) |>
ggplot(aes(Sepal.Length, Sepal.Width)) +
geom_polygon(aes(fill = Petal.Length, group = tri))
![enter image description here](https://istack.dev59.com/ack0H.webp)
额外的
为了进行渐变填充三角形,最好在2D插值上绘制空白三角形:
library(interp)
library(ggforce)
interp(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length,
duplicate = "mean", nx = 1000, ny = 1000) |>
interp2xyz() |>
as.data.frame() |>
setNames(names(iris)[1:3]) |>
ggplot(aes(Sepal.Length, Sepal.Width)) +
geom_raster(aes(fill = Petal.Length)) +
scale_fill_continuous(na.value = NA) +
geom_delaunay_tile(data = iris, fill = NA, color = "#00000050")
![enter image description here](https://istack.dev59.com/jqr4S.webp)