PCA prcomp:如何获得PC1到PC3的图形

时间:2016-01-16 01:08:57

标签: r pca ggbiplot

在我的PCA脚本(下图)中,我总是得到一张PC1与PC2的图表。

 mydata.pca <- prcomp(mydata.fixed, center = TRUE, scale. = TRUE)

 g <- ggbiplot(mydata.pca, obs.scale = 1, var.scale = 1, 
               groups = mysamples, ellipse = FALSE, 
               circle = FALSE, var.axes=FALSE) 
g <- g + scale_color_discrete(name = '') 
g <- g + theme(legend.direction ='horizontal', 
                legend.position = 'top') 
g <- g + theme_bw()

然而,当我运行类似的东西时:

summary(mydata.pca)

我看到了PC1到PC4的所有信息。如何更改我的ggbioplot脚本以获取PC1与PC3的图表?

1 个答案:

答案 0 :(得分:3)

ggbiplot的选择参数正是您所寻找的:

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

# PC1 vs PC 2

ggbiplot(iris_pca, choices = c(1,2), obs.scale = 1, var.scale = 1, 
         groups = iris$Species, ellipse = TRUE, 
         circle = TRUE) + scale_color_discrete(name = '') + theme(legend.direction = 'horizontal', 
               legend.position = 'top')

enter image description here

# PC1 vs PC 3

ggbiplot(iris_pca, choices = c(1,3), obs.scale = 1, var.scale = 1, 
         groups = iris$Species, ellipse = TRUE, 
         circle = TRUE) + scale_color_discrete(name = '') + theme(legend.direction = 'horizontal', 
               legend.position = 'top')

enter image description here