ggplot2 / ggfortify绘制PCA中的错误

时间:2018-08-22 15:06:39

标签: r ggplot2 pca ggbiplot

我收到以下错误消息,尝试使用ggplot2绘制PCA的(基本)图:

  

plot_label中的错误(p = p,数据= plot.data,标签=标签,标签.label =标签。标签,:     不支持的类:princomp

该图在R自己的双图函数biplot(prin_comp)中起作用,所以我不确定在要求ggplot执行此操作时是否出错,或者PCA输出是否有问题使其不适用于ggplot。我不能发布数据(机密性问题),但是它是一个很大的数据集,如果有帮助,我可以发布PCA输出吗?

编辑: 这是使用mtcars数据集的示例,其结果相同:

library("ggbiplot")
data(mtcars)
View(mtcars)
pca <- princomp(mtcars, scale=1)
princomp(x = mtcars, scale = 1)

Standard deviations:
     Comp.1      Comp.2      Comp.3      Comp.4      Comp.5      Comp.6 
134.3827868  37.5472829   3.0226511   1.2860724   0.8922099   0.6530910 
     Comp.7      Comp.8      Comp.9     Comp.10     Comp.11 
  0.3037193   0.2814568   0.2467490   0.2073344   0.1952988 

 11  variables and  32 observations.


ggbiplot(pca)
  

plot_label中的错误(p = p,数据= plot.data,标签=标签,label.label =标签.label ,:不受支持的类:princomp

同样,在base R的双图函数中起作用。 我希望得到以下类型的输出(而不是下面答案中所示的图),因此如果不明显,请使用biplot而不是autoplot道歉: biplot mtcars

无法在谷歌搜索或在此处搜索时找到类似的错误,但很高兴与他人联系找到该错误。

1 个答案:

答案 0 :(得分:1)

ggfortifyhttps://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html)的文档显示-

  

{ggfortify}让{ggplot2}知道如何解释PCA对象。后   加载{ggfortify}时,您可以使用ggplot2 :: autoplot函数   stats :: prcomp和stats :: princomp对象。

因此,您要做的只是以下几点:

library(ggfortify)
#> Loading required package: ggplot2

ggplot2::autoplot(princomp(mtcars, scale = 1))
#> Warning: In princomp.default(mtcars, scale = 1) :
#>  extra argument 'scale' will be disregarded

另一种实现方法是使用ggbiplot库(https://github.com/vqv/ggbiplot):

ggbiplot::ggbiplot(princomp(mtcars))

reprex package(v0.2.0.9000)创建于2018-08-23。