在PCA ggplot-R中的组旁边添加分类器 [英] Add classifier next to its group in PCA ggplot - R

查看:49
本文介绍了在PCA ggplot-R中的组旁边添加分类器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在此PCA图中的质心旁边添加物种"分类器.

How can I added the Species classifier next to the centroid in this PCA plot.

代码:

library(ggpubr)
library(ggbiplot)

data(iris)
Species<- iris$Species

myPCA <- prcomp(iris[.1:4], scale. = TRUE)

dt<-data.table(PC1=myPCA$x[,1],PC2=myPCA$x[,2],  Species)
dt[order(dt$Species),]
centeriod<-summaryBy(PC1+PC2 ~ Species, data=dt, FUN=list(mean))

ggplot(dt,aes(x=PC1,y=PC2,color=Species, fill=Species)) +  geom_point(size = 2)    

推荐答案

在这种情况下,我不确定为什么需要ggpubr和ggbiplot软件包.您可以仅使用ggplot2(以及示例中已使用的data.table)来完成此操作.

I'm not sure why you would need the ggpubr and ggbiplot packages in this case. You can do this with just ggplot2 (and data.table which you already used in your example).

library(ggplot2)
library(data.table)

data(iris)
Species<- iris$Species

myPCA <- prcomp(iris[,1:4], scale. = TRUE)

dt <- data.table(PC1=myPCA$x[,1],PC2=myPCA$x[,2],  Species)
dt <- dt[order(dt$Species),]

centeroid <- dt[, list(PC1 = mean(PC1), PC2 = mean(PC2)), by = Species]

ggplot(dt,aes(x=PC1,y=PC2)) +  
  geom_point(size = 2, aes(color=Species)) +
  geom_text(data = centeroid, aes(label = Species))

reprex软件包(v0.3.0)创建于2020-06-23 sup>

Created on 2020-06-23 by the reprex package (v0.3.0)

这篇关于在PCA ggplot-R中的组旁边添加分类器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆