Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a PCA plots as I posted here

I know how to use the PCA results to draw the circle, but failed to draw the x.lab and the y.lab based on the plotting results from s.class.

How to make a plot as I posted here? two muppets I would like to ask more about this.

  1. How to make the points bigger or smaller according to another integer variable?

  2. Can ggplot2 draw the same circle as s.class? The previous answers do not show how to draw circles.

like image 728
Ming Avatar asked Jan 19 '16 06:01

Ming


People also ask

How do you color PCA?

Hi, In the PCA plot, right click on the graph and select "ROW LEGEND", you can then choose the column that will be used to color code the dots on your plot.


2 Answers

The size of the points cane adjusted using size. The ellipses can be added via stat_ellipsis

pca <- prcomp(iris[iris$Species %in% c("virginica","versicolor"),1:4], retx = TRUE,  scale = TRUE,tol=0.4)
predicted <-predict(pca,iris[,1:4])
ggplot(data.frame(predicted))+aes(x=PC1,y=PC2,color=iris$Species)+geom_point(aes(size=iris$Sepal.Length))+stat_ellipse()+stat_ellipse(level=0.8)

enter image description here

like image 129
CAFEBABE Avatar answered Oct 19 '22 11:10

CAFEBABE


What I did in the pcoa function of my msap package was to use s.class only for the ellipses and centroids:

  1. Add empty plot, with labels and limits depending on several variables:

    plot(0,0, main=paste(name,surname, sep=": "), type = "n",
      xlab=paste("C1 (",var1,"%)"),ylab=paste("C2 (",var2,"%)"), 
      xlim=c(minX-10^floor(log10(abs(minX))),maxX+10^floor(log10(abs(maxX)))), 
      ylim=c(minY-10^floor(log10(abs(minY))),maxY+10^floor(log10(abs(maxY)))),
     frame=TRUE, cex=1.5)
    

    Look at the xlab.

  2. Plot the points for the different treatments/groups with different colors/simbols. Here you could set the points size using the cex parameter.

    for(i in 1:ntt){
        points(spcoo[[i]], pch=21, col="black", bg=bgcolors[i])
    }
    
  3. Finally use ade4's c.class to plot ellipses, stars and group labels, but not the points (cpoint=0)

    s.class(pcol$points, groups, cpoint=0, col=bgcolors, add.plot=TRUE)
    

In my code I get a figure like this PCOA figure:

enter image description here

like image 32
anpefi Avatar answered Oct 19 '22 10:10

anpefi