问题
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)
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).
Is there a way to make a subset of the ir.pca
object to select only specific groups
to plot?
回答1:
I think you can define a new graphical window with ggplot2::coord_equal
, eg:
g + coord_equal(xlim=c(0, 3))
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]))
Is this what you wanted?
来源:https://stackoverflow.com/questions/37442920/subset-of-prcomp-object-in-r