简体   繁体   English

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

[英]Convex Hull for Ternary plot using ggtern in R

Polynators convex hull Area Polynators凸包面积

Consider the following data.frame 考虑以下数据

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"))

First try 第一次尝试

I am trying to make a ternary plot and add a convex hull around the points, my first try was using geom_encircle of the ggalt package: 我试图做一个三元相图,并添加点周围的凸包,使用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") 

With this result 有了这个结果

在此处输入图片说明

Which encircles the points, but is not a convex hull 包围点,但不是凸包

Second try using the geometry package 第二次尝试使用几何图形包

Trying to follow my own answer for rgl , I tried this: 为了遵循我对rgl的回答 ,我尝试了以下方法:

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

and then this for the plot: 然后是情节:

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")

But got this super weird polygon: 但是得到了这个超级怪异的多边形:

在此处输入图片说明

Can someone help me get the convex hull plot? 有人可以帮我得到凸包图吗?

Think you will find that geom_encircle uses chull internally. 想想您会发现geom_encircle在内部使用chull。 Set the expand parameter to 0. 将扩展参数设置为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") 

产量

Here is a simple solution based on the chull function. 这是基于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")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM