使用R中的ggtern绘制三元图的凸包

3

多边形凸包面积

考虑以下数据框

DGChi <- structure(list(Sucrose = c(42, 40, 15, 19, 33, 49, 35, 31, 22, 
25, 37, 28, 31, 41, 27, 28, 33, 43, 21, 37, 14, 41, 30, 34, 38, 
40, 40, 33, 33), Fructose = c(27, 29, 41, 35, 29, 23, 27, 33, 
38, 38, 28, 31, 29, 26, 32, 34, 31, 28, 40, 30, 39, 27, 32, 31, 
29, 28, 28, 32, 29), Glucose = c(31, 31, 44, 46, 38, 28, 38, 
36, 40, 37, 35, 41, 40, 33, 41, 38, 36, 30, 39, 33, 47, 32, 38, 
35, 33, 32, 32, 35, 38), Sindrome = c("Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily")), .Names = c("Sucrose", 
"Fructose", "Glucose", "Sindrome"), row.names = c(NA, -29L), class = c("tbl_df", 
"tbl", "data.frame"))

第一次尝试

我正在尝试制作一个三元图,并在点周围添加凸包,我的首次尝试是使用ggalt软件包的geom_encircle

library(ggtern)
library(ggalt)

ggtern(data = DGChi, aes(x = Fructose, y = Sucrose, z = Glucose, fill = Sindrome)) +
theme_bw() +
geom_encircle(alpha=0.2,size=1, spread = 0.5) +
geom_point() +
theme(legend.position="bottom") 

通过这个结果:

enter image description here

这个图形围绕着点并覆盖了它们,但不是凸包

第二次尝试使用geometry包

尝试遵循我的rgl答案,我尝试了这个:

library(geometry)
DGChiMin <- as.data.frame(convhulln(matrix(c(DGChi$Fructose, DGChi$Sucrose, DGChi$Glucose), ncol = 3)))
colnames(DGChiMin) <- c("Fructose", "Sucrose", "Glucose")

然后是关于情节的内容:

ggtern(data = DGChi, aes(x = Fructose, y = Sucrose, z = Glucose)) +
  theme_bw() +
  geom_polygon(data = DGChiMin,aes(x = Fructose, y = Sucrose, z = Glucose)) +
  geom_point() +
  theme(legend.position="bottom")

但是得到了这个非常奇怪的多边形:

在此输入图片描述

有人能帮我制作凸包图吗?

2个回答

4

您会发现geom_encircle在内部使用chull。将expand参数设置为0。

library(ggalt)
library(ggtern)
ggtern(data = DGChi, aes(x = Fructose, y = Sucrose, z = Glucose, fill = Sindrome)) +
  theme_bw() +
  geom_encircle(alpha=0.2,size=1, expand=0) + ##<<<<<< expand = 0
  geom_point() +
  theme(legend.position="bottom") 

Output


2
这里有一个基于chull函数的简单解决方案。
DGChi <- structure(list(Sucrose = c(42, 40, 15, 19, 33, 49, 35, 31, 22, 
25, 37, 28, 31, 41, 27, 28, 33, 43, 21, 37, 14, 41, 30, 34, 38, 
40, 40, 33, 33), Fructose = c(27, 29, 41, 35, 29, 23, 27, 33, 
38, 38, 28, 31, 29, 26, 32, 34, 31, 28, 40, 30, 39, 27, 32, 31, 
29, 28, 28, 32, 29), Glucose = c(31, 31, 44, 46, 38, 28, 38, 
36, 40, 37, 35, 41, 40, 33, 41, 38, 36, 30, 39, 33, 47, 32, 38, 
35, 33, 32, 32, 35, 38), Sindrome = c("Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily", "Chiropterophily", 
"Chiropterophily", "Chiropterophily", "Chiropterophily")), .Names = c("Sucrose", 
"Fructose", "Glucose", "Sindrome"), row.names = c(NA, -29L), class = c("tbl_df", 
"tbl", "data.frame"))

# Convex hull
ch <- chull(DGChi[,1:2])
DGChiMin <- DGChi[ch, 1:3]

library(ggtern)
ggtern(data = DGChi, aes(x = Fructose, y = Sucrose, z = Glucose)) + geom_point() +
  theme_bw() +
  geom_polygon(data = DGChiMin, aes(x = Fructose, y = Sucrose, z = Glucose), fill="#FF000044") +
  theme(legend.position="bottom")

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接