全新是R的新鲜事物,很抱歉,如果问题的效果不佳或超级基础,但我并不是真正了解我可以自己找到的任何指南。
我正在尝试在我拥有的数据集上运行原理坐标分析(PCO)。该数据集包括非数字信息,例如分类单元和年龄以及数值数据。
我想运行数值数据的PCO,但请保留非数字数据,以便我可以通过结果图中的这些类别进行颜色代码/标签样本。
当前,我正在使用read.table()
加载数据,将其放入使用dist()
的矩阵中,使用pco()
在矩阵上运行PCO(cmdscale()
的同义词),然后用plot()
。
如果您提供一些数据播放的数据,获得答案的机会更高。这是一些:
data(iris)
pca = prcomp(iris[,-5])
与GG图:将PCA坐标与类别列组合在一起
pca_ploting=data.frame(pca$x, Species = iris[,5])
library(ggplot2)
ggplot(data=pca_ploting)+
geom_point(aes(x = PC1, y=PC2, color= Species))+
stat_ellipse(aes(x = PC1, y=PC2, color= Species))
# an approach I like is to scale the coords by the variance explained
coord_fixed(ratio = pca$sdev[2]^2/pca$sdev[1]^2)
# here it does not look so nice because PC1 explains over 90%, while PC2 only about 5%
与ggord:
library(ggord)
ggord(pca, axes = c("1", "2"), iris[,5], xlims = NULL,
ylims = NULL, ext = 1.1, vec_ext = 2, size = 1)