【问题标题】:R: ggfortify: "Objects of type prcomp not supported by autoplot"R:ggfortify:“自动绘图不支持 prcomp 类型的对象”
【发布时间】:2015-07-15 08:59:02
【问题描述】:

我正在尝试使用 ggfortify 来可视化我使用 prcomp 所做的 PCA 的结果。

示例代码:

iris.pca <- iris[c(1, 2, 3, 4)] 
autoplot(prcomp(iris.pca))  

错误:自动绘图不支持 prcomp 类型的对象。请改用 qplot() 或 ggplot()。

奇怪的是,autoplot 是专门为处理 prcomp 的结果而设计的——ggplot 和 qplot 不能处理这样的对象。我正在运行 R 版本 3.2,刚刚从 github 下载了 ggfortify。

谁能解释这个消息?

【问题讨论】:

  • 好吧,在没有任何其他想法的情况下,我只是第三次重新安装了所有东西,并且由于某种原因它起作用了。

标签: r pca ggfortify


【解决方案1】:

我猜你没有加载所需的库,代码如下:

library(devtools)
install_github('sinhrks/ggfortify')
library(ggfortify); library(ggplot2)
data(iris)
iris.pca <- iris[c(1, 2, 3, 4)] 
autoplot(prcomp(iris.pca))

会起作用

【讨论】:

    【解决方案2】:

    即使 ggfortify 的简单性很迷人,我也不鼓励将其用于与标准 ggplot2 函数的某些重叠(例如警告 replacing previous import ‘dplyr::vars’ by ‘ggplot2::vars’ when loading ‘ggfortify’)。一个聪明的解决方法是直接使用ggplot2

    在这里我提出两个版本及其结果。

    # creating the PCA obj using iris data set
    iris.pca <- iris[c(1, 2, 3, 4)] 
    pca.obj <- prcomp(iris.pca)
    
    # ggfortify way - w coloring
    library(ggfortify)
    autoplot(pca.obj) + theme_minimal()  
    
    
    # ggplot2 way - w coloring
    library(ggplot2)
    dtp <- data.frame('Species' = iris$Species, pca.obj$x[,1:2]) # the first two componets are selected (NB: you can also select 3 for 3D plottings or 3+)
    ggplot(data = dtp) + 
           geom_point(aes(x = PC1, y = PC2, col = Species)) + 
           theme_minimal() 
    

    注意:直接 ggplot2 数据框结构的着色要容易得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2013-10-08
      • 2020-05-08
      相关资源
      最近更新 更多