【问题标题】:subset of prcomp object in RR中prcomp对象的子集
【发布时间】:2016-05-25 16:28:04
【问题描述】:

我基本上是在为一组变量计算 PCA,一切正常。假设我以虹膜数据为例,但我的数据不同。虹膜数据应该足以解释我的问题:

data(iris)
log.ir <- log(iris[, 1:4])
log.ir[mapply(is.infinite, log.ir)] <- 0
ir.groups<- iris[, 5]
ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE) 

library(ggbiplot)

g <- ggbiplot(ir.pca, obs.scale = 1,var.scale = 1,groups = ir.groups, var.axes=F)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal', 
               legend.position = 'top') + theme(legend.text=element_text(size=15), legend.key.size = unit(2.5, "lines")) + theme(text = element_text(size=20))
ggsave("pca2.pdf", g, width=15, height=15)

当我得到图时,一些组被绘制得太近了,所以我想为这个组子集制作一个新图(而不为子集计算新的 PCA)。

有没有办法让ir.pca 对象的子集只选择特定的groups 进行绘图?

【问题讨论】:

    标签: r pca


    【解决方案1】:

    我想你可以用ggplot2::coord_equal定义一个新的图形窗口,例如:

    g + coord_equal(xlim=c(0, 3))
    

    将从图表中排除 setosa,而不是从 PCA 中排除。


    考虑到您的评论,您可以以编程方式进行:

    # first we filter the scores
    filtered_scores <- ir.pca$x[which(iris$Species != "setosa"), ]
    # then on PC1 and PC2
    g + coord_equal(xlim=range(filtered_scores[, 1]), ylim=range(filtered_scores[, 2]))
    

    这是你想要的吗?

    【讨论】:

    • 感谢您的回答。但我需要一种更直接的方法来制作一个子集。像这样,我需要查看图表并制作一个子集。没有程序化的方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    相关资源
    最近更新 更多