在 FDA 图中绘制凸包 - R

Plot the convex hull in FDA plot - R

我正在尝试使用 ggpubr 包为该图中的每个组添加一个凸包?为什么它不起作用?

代码:

library(dplyr)
library(MASS)
library(ggplot2)
library(scales)
library(ggpubr)
library(data.table)

irisfda <- fda(Species ~ ., data = iris, method = mars)
  
df1 <- cbind(data.frame(irisfda$fit$fitted.values), species = iris[,"Species"])

ggplot(df1) +
  geom_point(aes(X1, X2, color = species, shape = species), size = 2.5) + 
  labs(x = "FDA1",y = "FDA1") +  
  stat_chull(aes(color =  species, fill =  species), geom = "polygon", alpha = 0.1)  

你还没有告诉 stat_chull x 和 y 点在哪里。你告诉 geom_point 它们在哪里,但是当你将它们添加到绘图中时,geoms 和 stats 不会相互继承。您可以只将 x 和 y co-ordinates 添加到 stat_chull,或者更好的是,将它们添加到 ggplot 调用。 然后 stat_chull可以继承它们,你可以节省一些输入。

顺便说一下,您使用了 library 调用 dplyr、MASS、scales 和 data.table,这些在本示例中不需要,但您忘记调用库 mda 需要:

library(ggplot2)
library(ggpubr)
library(mda)

irisfda <- fda(Species ~ ., data = iris, method = mars)
df1 <- cbind(data.frame(irisfda$fit$fitted.values), species = iris[,"Species"])

ggplot(df1, aes(x = X1, y = X2, color = species, shape = species)) +
  geom_point(size = 2.5) + 
  labs(x = "FDA1",y = "FDA1") +  
  stat_chull(geom = "polygon", alpha = 0.1)