5

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.

Community
  • 1
  • 1
Ming
  • 181
  • 2
  • 10
  • Are you looking for something like [here](http://stackoverflow.com/questions/13936051/adding-ellipses-to-a-principal-component-analysis-pca-plot) or [here](http://stackoverflow.com/questions/18039313/pca-scaling-with-ggbiplot)? –  Jan 19 '16 at 06:27

2 Answers2

6

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

CAFEBABE
  • 3,983
  • 1
  • 19
  • 38
2

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

anpefi
  • 83
  • 6