Voronoi图又叫泰森多边形、Dirichlet图、邻近图, 是一种按距离划分空间中样本点邻近区域的分割空间平面方法,每个多边形的边是由空间中两邻点间直线的垂直平分线组成, 可以一个有限样本空间完全细分为多面体。Voronoi diagram - Wikipedia
建立 Voronoi 图算法的关键是对离散数据点合理地连成三角网,即构建 Delaunay 三角网。
特点
1、每个泰森多边形内仅含有 一个离散点数据(称为 生成元);
2、泰森多边形内的点到相应离散点的距离最近;
3、位于泰森多边形边上的点到其两边的离散点的距离相等。
4、若某物从不同的点以均匀的增长率增长,Voronoi 图的生成就是一种自发模式
应用
泰森多边形图基于其非常直观的展示方式,可用于定性分析、统计分析、邻近分析等,通过创建泰森多边形创建的多边形要素可对可用空间进行划分并将其分配给最近的点要素。泰森多边形有时会用于替代插值操作,以便将一组样本的点测量值概化到最接近他们的区域,从而建立整个平面的区域划分 。例如使用泰森多边形可将取自一组气候测量仪的测量值概化到周围区域。
The fascinating world of Voronoi diagrams | by Francesco Bellelli | Towards Data Science
How Voronoi diagrams help us understand our world – The Irish Times
GGPLOT 实现
-
1、ggvoronoi--示例代码
Voronoi diagram in ggplot2 with ggvoronoi | R CHARTS (r-charts.com)
pacman::p_load(Seurat,ggplot2,ggvoronoi,ggforce,dior,dplyr)
data.use <- DimPlot(pbmc_small)$data
ggplot(data.use, aes(tSNE_1,tSNE_2)) +
geom_voronoi(aes(fill = factor(ident)),color = "white",alpha = 1) +
geom_path(stat="voronoi",alpha=.08,size= 2,color = "white") +
geom_path(stat="voronoi",alpha=.05,size= 3,color = "grey80") +
geom_point(size = 3) +
scale_fill_brewer(palette = "Accent") +
tidydr::theme_dr() + theme(panel.grid = element_blank())
-
2、ggforce--示例代码 ggforce添加了一些额外扩展效果
Voronoi tesselation and delaunay triangulation — geom_voronoi • ggforce (data-imaginist.com)
ggplot(data.use, aes(tSNE_1,tSNE_2, group = -1L)) +
geom_voronoi_tile(aes(fill = factor(ident)),color = "white") +
geom_voronoi_segment(alpha=.08,size= 2,color = "white") +
geom_voronoi_segment(alpha=.05,size= 2,color = "grey80") +
geom_point(aes(size = after_stat(vorarea)),stat = 'delvor_summary') +
scale_fill_brewer(palette = "Accent") +
tidydr::theme_dr() + theme(panel.grid = element_blank())
-
3、从质心生成泰森多边形
r - Extend geom_voronoi past its limits with scale_*_continuous - Stack Overflow
pacman::p_load(Seurat,ggplot2,ggvoronoi,ggforce,dior,dplyr)
data.use <- DimPlot(pbmc_small)$data
vnor_dat <- data.use %>% group_by(ident) %>% summarise(tSNE_1 = median(tSNE_1),tSNE_2 = median(tSNE_2),.groups = "drop")
outline.dt <- data.frame(x = c(-80, 90 , 90, -80),y = c(-30, -30, 30, 30))
ggplot(vnor_dat,aes(tSNE_1,tSNE_2)) +
geom_voronoi(aes(fill = factor(ident)),outline = outline.dt,alpha = 1) +
geom_path(stat="voronoi",alpha=0.8,size= 0.1,color = "black",outline = outline.dt) +
geom_point(pch = "+",alpha = 0.3,size = 20,aes(color = ident),show.legend = F) +
geom_point(data = data.use,aes(color = ident),size = 2,show.legend = F) +
scale_fill_manual(values = c("#2d333f","#f25a33","#f6f2e6")) +
scale_color_brewer(palette = "Set1") +
tidydr::theme_dr() + theme(panel.grid = element_blank())
Classic Voronoi diagram 配色参考
Chapter 74 Tutorial of three ggplot2 based packages | EDAV Fall 2021 Tues/Thurs Community Contributions (jtr13.github.io)
网友评论