用颜色绘制PCA分数 [英] Plotting PCA scores with color

查看:126
本文介绍了用颜色绘制PCA分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做PCA,我想在R中绘制第一个主成分对第二个:

I'm doing PCA and I would like to plot first principal component vs second in R:

pca<-princomp(~.,data=data, na.action=na.omit
plot(pca$scores[,1],pca$scores[,2])

或几个主要组成部分:

pairs(pca$scores[,1:4])

但是这些点是黑色的.如何为图表适当添加颜色?我需要多少种颜色?我要绘制的每个主要成分中的一个?还是数据矩阵中的每一行一个?

however the points are black. How do I appropriately add color to the graphs? How many colors do I need? One for each principal component I am plotting? Or one for each row in my data matrix?

谢谢

我的数据如下:

> data[1:4,1:4]
                          patient1                     patient2                     patient3                     patient4
2'-PDE                    0.0153750                    0.4669375                   -0.0295625                    0.7919375
7A5                       2.4105000                    0.3635000                    1.8550000                    1.4080000
A1BG                      0.9493333                    0.2798333                    0.7486667                    0.7500000
A2M                       0.2420000                    1.0385000                    1.1605000                    1.6777500

这是否合适:

plot(pca$scores[,1:4], pch=20, col=rainbow(dim(data)[1]))

推荐答案

以下是PCA的一些示例图.摘自此处.

Here are some example plots of PCA. Taken from the here.

z1 <- rnorm(10000, mean=1, sd=1); z2 <- rnorm(10000, mean=3, sd=3); z3 <- rnorm(10000, mean=5, sd=5); z4 <- rnorm(10000, mean=7, sd=7); z5 <- rnorm(10000, mean=9, sd=9); mydata <- matrix(c(z1, z2, z3, z4, z5), 2500, 20, byrow=T, dimnames=list(paste("R", 1:2500, sep=""), paste("C", 1:20, sep=""))) 

summary(pca) 
summary(pca)$importance[, 1:6] 

x11(height=6, width=12, pointsize=12); par(mfrow=c(1,2)) 

mycolors <- c("red", "green", "blue", "magenta", "black") # Define plotting colors. plot(pca$x, pch=20, col=mycolors[sort(rep(1:5, 500))]) 

plot(pca$x, type="n"); text(pca$x, rownames(pca$x), cex=0.8, col=mycolors[sort(rep(1:5, 500))]) 

您可以使用配对

pairs(pca$x[,1:5], col = mycolors) 

绘制前两个主成分的散点图以及存储在pca $ rotation中的相应特征向量.

library(scatterplot3d) 
scatterplot3d(pca$x[,1:3], pch=20, color=mycolors[sort(rep(1:5, 500))]) 

与上述相同,但是在3D散点图中绘制了前三个主要成分.

library(rgl); rgl.open(); offset <- 50; par3d(windowRect=c(offset, offset, 640+offset, 640+offset)); rm(offset); rgl.clear(); rgl.viewpoint(theta=45, phi=30, fov=60, zoom=1); spheres3d(pca$x[,1], pca$x[,2], pca$x[,3], radius=0.3, color=mycolors, alpha=1, shininess=20); aspect3d(1, 1, 1); axes3d(col='black'); title3d("", "", "PC1", "PC2", "PC3", col='black'); bg3d("

稍后,它将使用Open GL创建一个交互式3D散点图.为此,需要安装rgl库.要保存图形快照,可以使用命令rgl.snapshot("test.png").

The later creates an interactive 3D scatter plot with Open GL. The rgl library needs to be installed for this. To save a snapshot of the graph, one can use the command rgl.snapshot("test.png").

require(GGally)
ggpairs(pca$x[,1:5])

这篇关于用颜色绘制PCA分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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