gpt4 book ai didi

r - 在 R 中使用 ggtern 的三元图的凸包

转载 作者:行者123 更新时间:2023-12-01 15:00:59 33 4
gpt4 key购买 nike

Polynators 凸包面积

考虑以下数据框

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

包围点,但不是凸包

第二次尝试使用几何包

正在尝试关注 my own answer for 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")

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

enter image description here

谁能帮我得到凸包图?

最佳答案

认为您会发现 geom_encircle 内部使用了 chull。将扩展参数设置为 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

关于r - 在 R 中使用 ggtern 的三元图的凸包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45923511/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com