向 Fviz_PCA_biplot 添加聚类

问题描述 投票:0回答:1

我使用此代码构建了 fviz_pca_biplot

ph <- "Physiological"
ag <- "Morphological"
trait <- factor(c(ph,ph,ph,ph,ph,ph,ph,ph,ph,ph,ph,ag,ag,ag,ag))

fviz_pca_biplot(pc1, geom.ind = c("text","point"), labelsize = 5,
                pointshape = 22,pointsize = 3,font.family = "serif",
                fill.ind = ct$Species,
                col.ind = "black",
                col.var = trait, 
                repel = T, parse = T,
                legend.title = list(fill = "Species", color = "Traits"))+
  coord_cartesian(xlim = c(-4.5, 4.25), ylim = c(-3,3.5))+
  scale_y_continuous(breaks=c(-3,-2,-1,0,1.25,2.5,3.5),
                     labels = number_format(accuracy = 0.01))+
  scale_x_continuous(breaks=c(-4.75,-3,-1.5,0,1.5,3,4.5))+
  fill_palette(c("#E5871A","#1AE522", "#1A78E5","#E51ADD"))+ 
  color_palette(c("darkgreen", "#1A0DB6"))+
  theme_minimal() + 
  theme(panel.background = element_rect(colour = "black"),
        axis.text.x =element_text(colour="black", size = 9, family = "serif", face = "bold"),
        axis.text.y =element_text(colour="black", size = 9, family = "serif", face = "bold"),
        axis.title = element_text(face = "bold",family = "serif",colour = "black",size = 9),
        plot.title =element_text(vjust = -9,hjust=0.01,face = "bold",family = "sans", size = 10),
        axis.ticks = element_line(colour = "black"),
        legend.position = "right", #legend.direction = "horizontal", 
        #legend.title = element_blank(),
        legend.text = element_text(size = 9,face="bold.italic",family = "serif"),
        panel.grid.minor = element_blank()) +
  labs(title = "a.", x= "PC1 (32.49%)", y= "PC2 (21.51%)")

然后我使用这段代码计算了聚类

HCPC(pc1, method = "ward", metric= "euclidean", graph=T, order = T)

现在我想在我之前通过 fviz_pca_biplot 制作的同一图中显示获得的簇 最终的图表应该是这样的 我手动绘制集群,但我不知道如何使用 ggplot 来完成它。 我的想法是,如果我可以通过像凸型一样画一条线来加入相同簇的个体。

r ggplot2 pca
1个回答
0
投票

library(ggforce)
geom_mark_ellipse(aes(fill = df$Clust,),alpha = 0.3)
此代码行帮助我在
fviz_pca_biplot
上制作簇椭圆 您也可以将
ellipse
替换为
hull
,
circle
例如

© www.soinside.com 2019 - 2024. All rights reserved.