R中的prcomp对象的子集 [英] subset of prcomp object in R

查看:88
本文介绍了R中的prcomp对象的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm basically computing the PCA for a set of variables and everything works fine. Lets say I'm using the iris data as an example, but my data is different. The iris data should be sufficient to explain my question:

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).

When I get the plot, some groups are plotted too close together so I want to make a new plot for this subset of groups (without computing a new PCA for the subset).

是否有一种方法可以使ir.pca对象的子集仅选择要绘制的特定groups?

Is there a way to make a subset of the ir.pca object to select only specific groups to plot?

推荐答案

我认为您可以使用ggplot2::coord_equal定义新的图形窗口,例如:

I think you can define a new graphical window with ggplot2::coord_equal, eg:

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

setosa从图表中排除,而不从PCA中排除.

would exclude setosa from the graph, not from the PCA.

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

Taking your comment into account, you can do it programmatically:

# 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]))

这是您想要的吗?

这篇关于R中的prcomp对象的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆