Hi, I have matrix of 300,000*115 (snps*individual). I ran the PCA on the covariance matrix which has a dimention oof 115*115. I have the first 100 individuals from group A and the rest of 15 individuals from group B. I need to plot the data in two and 3 dimentions with respect to PC1 and PC2 and (in 3D with respect to PC1, PC2 and PC3). I do not know how to have the plot ploting the first 100 points corresponding to group A in red (for example) and the rest of the 15 points in Blue? i.e I want the each group in a diffrent color in the same plot. I appreciate if someone can help. Thanks, -- View this message in context: http://www.nabble.com/Plot-Principal-component-analysis-tp15700123p15700123.html Sent from the R help mailing list archive at Nabble.com.
If your samples are in the specified order (i.e. first 100 from group A and remaining from group B) you can try the following in your plot call: plot(..., col=c("red", "blue")[c(rep(100, 1), rep(15, 2))]) -Christos> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of SNN > Sent: Tuesday, February 26, 2008 4:11 PM > To: r-help at r-project.org > Subject: [R] Plot Principal component analysis > > > Hi, > > I have matrix of 300,000*115 (snps*individual). I ran the PCA > on the covariance matrix which has a dimention oof 115*115. I > have the first 100 individuals from group A and the rest of > 15 individuals from group B. I need to plot the data in two > and 3 dimentions with respect to PC1 and PC2 and (in 3D with > respect to PC1, PC2 and PC3). I do not know how to have the > plot ploting the first 100 points corresponding to group A in > red (for example) and the rest of the 15 points in Blue? i.e > I want the each group in a diffrent color in the same plot. I > appreciate if someone can help. > > Thanks, > -- > View this message in context: > http://www.nabble.com/Plot-Principal-component-analysis-tp1570 > 0123p15700123.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > >
SNN wrote:> Hi, > > I have matrix of 300,000*115 (snps*individual). I ran the PCA on the > covariance matrix which has a dimention oof 115*115. I have the first 100 > individuals from group A and the rest of 15 individuals from group B. I need > to plot the data in two and 3 dimentions with respect to PC1 and PC2 and (in > 3D with respect to PC1, PC2 and PC3). I do not know how to have the plot > ploting the first 100 points corresponding to group A in red (for example) > and the rest of the 15 points in Blue? i.e I want the each group in a > diffrent color in the same plot. I appreciate if someone can help. >Hi Nancy, (if indeed you are a Nancy and that is not a webnym) Say that your groups really are coded "A" and "B", the group coding variable is called "group". You can define a color vector like this: colorvector<-ifelse(group=="A","red","blue") Jim
Jim Lemon wrote:>SNN wrote: > > >>Hi, >> >>I have matrix of 300,000*115 (snps*individual). I ran the PCA on the >>covariance matrix which has a dimention oof 115*115. I have the first 100 >>individuals from group A and the rest of 15 individuals from group B. I need >>to plot the data in two and 3 dimentions with respect to PC1 and PC2 and (in >>3D with respect to PC1, PC2 and PC3). I do not know how to have the plot >>ploting the first 100 points corresponding to group A in red (for example) >>and the rest of the 15 points in Blue? i.e I want the each group in a >>diffrent color in the same plot. I appreciate if someone can help. >> >> >> >Hi Nancy, >(if indeed you are a Nancy and that is not a webnym) >Say that your groups really are coded "A" and "B", the group coding >variable is called "group". You can define a color vector like this: > >colorvector<-ifelse(group=="A","red","blue") > >Jim > >______________________________________________ >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. > > > >Hi Nancy, in your case you may also use inertia ellipses to represent your groups, in addition to different colors. Here is an example using a microsatellite dataset from adegenet (but valid for SNPs of course): ##### library(ade4) library(adegenet) data(microbov) # dataset # replace missing values obj=na.replace(microbov,method="mean") # perform your pca, keep 3 axes pca1=dudi.pca(obj$tab,scannf=FALSE,nf=3,scale=FALSE) # plot the result s.class(pca1$li,obj$pop) s.class(pca1$li,obj$pop,col=sample(colors(),15)) # here, replace "col" by the appropriate vector of colors. ##### The resulting graphic represents each genotype by a point, and adds ellipses of different color for each group; each ellipse represents 95 % of the inertia of the corresponding group. The more ellipses overlap, the less your groups are differentiated on the factorial plane. Cheers, Thibaut. -- ###################################### Thibaut JOMBART CNRS UMR 5558 - Laboratoire de Biom?trie et Biologie Evolutive Universite Lyon 1 43 bd du 11 novembre 1918 69622 Villeurbanne Cedex T?l. : 04.72.43.29.35 Fax : 04.72.43.13.88 jombart at biomserv.univ-lyon1.fr http://lbbe.univ-lyon1.fr/-Jombart-Thibaut-.html?lang=en http://pbil.univ-lyon1.fr/software/adegenet/
Thanks for your help, Can R plot the data in 3 dimention, with different colors for each group ? for exmple I would like to have the plot with respect to PC1, PC2 and PC3. Thanks, SNN wrote:> > Hi, > > I have matrix of 300,000*115 (snps*individual). I ran the PCA on the > covariance matrix which has a dimention oof 115*115. I have the first 100 > individuals from group A and the rest of 15 individuals from group B. I > need to plot the data in two and 3 dimentions with respect to PC1 and PC2 > and (in 3D with respect to PC1, PC2 and PC3). I do not know how to have > the plot ploting the first 100 points corresponding to group A in red (for > example) and the rest of the 15 points in Blue? i.e I want the each group > in a diffrent color in the same plot. I appreciate if someone can help. > > Thanks, >-- View this message in context: http://www.nabble.com/Plot-Principal-component-analysis-tp15700123p15717669.html Sent from the R help mailing list archive at Nabble.com.
SNN <s.nancy1 at yahoo.com> wrote in news:15717669.post at talk.nabble.com:> > Thanks for your help, > Can R plot the data in 3 dimention, with different colors for each > group ? > for exmple I would like to have the plot with respect to PC1, PC2 > and PC3. >See if these answer your desires: <http://www.jstatsoft.org/v08/i11/paper> <http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=44> -- David Winsemius> SNN wrote: >> >> Hi, >> >> I have matrix of 300,000*115 (snps*individual). I ran the PCA on >> the covariance matrix which has a dimention oof 115*115. I have the >> first 100 individuals from group A and the rest of 15 individuals >> from group B. I need to plot the data in two and 3 dimentions with >> respect to PC1 and PC2 and (in 3D with respect to PC1, PC2 and >> PC3). I do not know how to have the plot ploting the first 100 >> points corresponding to group A in red (for example) and the rest >> of the 15 points in Blue? i.e I want the each group in a diffrent >> color in the same plot. I appreciate if someone can help. >> >> Thanks, >> >
Thanks for your help. I downloaded the scatterplot3d package. plot3d(pr$x, col = c("red", "blue")[c(rep(1, 100), rep(2, 15))], pch=20) where pr$x is the object of the PCA. this works but the graph does not look good. the other option that I tried is scatterplot3d(pr$x, type="p", highlight.3d=T, pch=16) where the graph looks much nicer but i do not know how to have each group with a different color. Does anyone know how to do it? SNN wrote:> > Hi, > > I have matrix of 300,000*115 (snps*individual). I ran the PCA on the > covariance matrix which has a dimention oof 115*115. I have the first 100 > individuals from group A and the rest of 15 individuals from group B. I > need to plot the data in two and 3 dimentions with respect to PC1 and PC2 > and (in 3D with respect to PC1, PC2 and PC3). I do not know how to have > the plot ploting the first 100 points corresponding to group A in red (for > example) and the rest of the 15 points in Blue? i.e I want the each group > in a diffrent color in the same plot. I appreciate if someone can help. > > Thanks, >-- View this message in context: http://www.nabble.com/Plot-Principal-component-analysis-tp15700123p15751493.html Sent from the R help mailing list archive at Nabble.com.