Hi! All, I am working on a dataset 'rat' with dimension 20500x363. I have calculated pca of samples (columns). Now I am trying to plot first two principle components with specified columns in different color. I have done following so far:> dim(rat)[1] 20500 363>#specifying columns to be colored in red > a1=colnames(rat[,c(1,5,9,12,16,154,227,276,284,298,305,311,318,336,338,drop=F)]) >#specifying columns to be colored in blue > a2=colnames(rat[,c(2,3,4,6,13,14,drop=F)]) >#specifying columns to be colored in green > a3=colnames(rat[,c(7,8,10,11,15,29,35,36,50,54,57,59,61,63,70,71,79,99,110,132,147,163,166,215,275,drop=F)]) >#specifying columns to be colored in brown > d=colnames(rat[,-c(1,5,9,12,16,154,227,276,284,298,305,311,318,336,338,2,3,4,6,13,14,7,8,10,11,15,29,35,36,50,54,57,59,61,63,70,71,79,99,110,132,147,163,166,215,275,drop=F)]) >#calculating pca for columns > rat_pca=prcomp(t(rat),scale=T) >plot(rat_pca$x,pch=20,col=c("red","blue","green","brown")[c(a1,a2,a3,d)])But after executing last command there is nothing plotted in the graph and it also does not give any error. Please help! regards Amit
On 18.04.2010 15:46, Amit wrote:> Hi! All, > > I am working on a dataset 'rat' with dimension 20500x363. I have > calculated pca of samples (columns). Now I am trying to plot first two > principle components with specified columns in different color. I have > done following so far: > >> dim(rat) > [1] 20500 363 >> #specifying columns to be colored in red >> a1=colnames(rat[,c(1,5,9,12,16,154,227,276,284,298,305,311,318,336,338,drop=F)]) >> #specifying columns to be colored in blue >> a2=colnames(rat[,c(2,3,4,6,13,14,drop=F)]) >> #specifying columns to be colored in green >> a3=colnames(rat[,c(7,8,10,11,15,29,35,36,50,54,57,59,61,63,70,71,79,99,110,132,147,163,166,215,275,drop=F)]) >> #specifying columns to be colored in brown >> d=colnames(rat[,-c(1,5,9,12,16,154,227,276,284,298,305,311,318,336,338,2,3,4,6,13,14,7,8,10,11,15,29,35,36,50,54,57,59,61,63,70,71,79,99,110,132,147,163,166,215,275,drop=F)]) >> #calculating pca for columns >> rat_pca=prcomp(t(rat),scale=T) >> plot(rat_pca$x,pch=20,col=c("red","blue","green","brown")[c(a1,a2,a3,d)])Well probably a1, ... contain *names* rather than numbers, hence you get a color vector containing only NAs, i.e. are not plotted. Uwe Ligges> But after executing last command there is nothing plotted in the graph > and it also does not give any error. Please help! > > regards > Amit > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Amit, how to color or label your pca plot has been answered before. Look at this post: http://n4.nabble.com/PCA-analysis-td861508.html#a861509 As for your problem, it's hard to say what went wrong without having the data. You write, there is nothing plotted in the graph. Does that mean you get a graph, and just the points are missing? If so, it is likely because colnames(rat[, ...]) returns NULL. Try it yourself. I believe your a1 through a4 are all NULL. Try removing the comma in colnames(rat[...]) But even if you do that, I doubt that this does what you want to do. By default, your plot command only plots the first two columns of rat_pca$x (or any of the two columns in it as long as you specify them). What has the plotting color to do with the columns anyway? The plotting color would normally vary along the 20500 observations, but not along the 363 columns (since obviously you cannot plot 363 columns at the same time). HTH, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Amit Sent: Sunday, April 18, 2010 9:47 AM To: r-help at r-project.org Subject: [R] plotting pca of samples in different colors Hi! All, I am working on a dataset 'rat' with dimension 20500x363. I have calculated pca of samples (columns). Now I am trying to plot first two principle components with specified columns in different color. I have done following so far:> dim(rat)[1] 20500 363>#specifying columns to be colored in red >a1=colnames(rat[,c(1,5,9,12,16,154,227,276,284,298,305,311,318,336,338,dropF)])>#specifying columns to be colored in blue > a2=colnames(rat[,c(2,3,4,6,13,14,drop=F)]) >#specifying columns to be colored in green >a3=colnames(rat[,c(7,8,10,11,15,29,35,36,50,54,57,59,61,63,70,71,79,99,110,1 32,147,163,166,215,275,drop=F)])>#specifying columns to be colored in brown >d=colnames(rat[,-c(1,5,9,12,16,154,227,276,284,298,305,311,318,336,338,2,3,4 ,6,13,14,7,8,10,11,15,29,35,36,50,54,57,59,61,63,70,71,79,99,110,132,147,163 ,166,215,275,drop=F)])>#calculating pca for columns > rat_pca=prcomp(t(rat),scale=T) >plot(rat_pca$x,pch=20,col=c("red","blue","green","brown")[c(a1,a2,a3,d)])But after executing last command there is nothing plotted in the graph and it also does not give any error. Please help! regards Amit ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.