R中prcomp对象的子集

问题描述 投票:2回答:1

我基本上是为一组变量计算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个回答
2
投票

我想你可以用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]))

这是你想要的吗?

© www.soinside.com 2019 - 2024. All rights reserved.