如何将它们绘制在同一个图形和不同颜色的图形上?

时间:2016-04-23 10:21:55

标签: r ggplot2 pca

我只需将这些图形放在同一个图形上,第一个图形的点需要与第二个图形不同。我觉得这很容易,但我找不到请帮忙。谢谢

library(ggfortify)
myPCA <- prcomp(iris[-5])
# drawing PC1 and PC2
autoplot(myPCA)
# drawing PC3 and PC4
myPCAtrunc <- myPCA
myPCAtrunc[[1]] <- myPCAtrunc[[1]][c(3,4,1,2)]
myPCAtrunc[[2]] <- myPCAtrunc[[2]][,c(3,4,1,2)]
colnames(myPCAtrunc[[2]]) <- c("PC1","PC2","PC3","PC4") # fake names
myPCAtrunc[[5]] <- myPCAtrunc[[5]][,c(3,4,1,2)]
colnames(myPCAtrunc[[5]]) <- c("PC1","PC2","PC3","PC4") # fake names
autoplot(myPCAtrunc, xlab = "PC3", ylab="PC4")

1 个答案:

答案 0 :(得分:0)

欢迎来到SO!

p1 <- autoplot(myPCA, colour = "red")
p2 <- autoplot(myPCAtrunc, xlab = "PC3", ylab="PC4", colour = "blue")
library(gridExtra)
grid.arrange(p1, p2, ncol = 2)

enter image description here