Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you use the excellent <code>FactoMineR</code> package for pca, you might find this useful for making plots with <code>ggplot2</code></p> <pre><code># Plotting the output of FactoMineR's PCA using ggplot2 # # load libraries library(FactoMineR) library(ggplot2) library(scales) library(grid) library(plyr) library(gridExtra) # # start with a clean slate rm(list=ls(all=TRUE)) # # load example data from the FactoMineR package data(decathlon) # # compute PCA res.pca &lt;- PCA(decathlon, quanti.sup = 11:12, quali.sup=13, graph = FALSE) # # extract some parts for plotting PC1 &lt;- res.pca$ind$coord[,1] PC2 &lt;- res.pca$ind$coord[,2] labs &lt;- rownames(res.pca$ind$coord) PCs &lt;- data.frame(cbind(PC1,PC2)) rownames(PCs) &lt;- labs # # Just showing the individual samples... ggplot(PCs, aes(PC1,PC2, label=rownames(PCs))) + geom_text() # # Now get supplementary categorical variables cPC1 &lt;- res.pca$quali.sup$coor[,1] cPC2 &lt;- res.pca$quali.sup$coor[,2] clabs &lt;- rownames(res.pca$quali.sup$coor) cPCs &lt;- data.frame(cbind(cPC1,cPC2)) rownames(cPCs) &lt;- clabs colnames(cPCs) &lt;- colnames(PCs) # # Put samples and categorical variables (ie. grouping # of samples) all together p &lt;- ggplot() + opts(aspect.ratio=1) + theme_bw(base_size = 20) # no data so there's nothing to plot... # add on data p &lt;- p + geom_text(data=PCs, aes(x=PC1,y=PC2,label=rownames(PCs)), size=4) p &lt;- p + geom_text(data=cPCs, aes(x=cPC1,y=cPC2,label=rownames(cPCs)),size=10) p # show plot with both layers # # clear the plot dev.off() # # Now extract variables # vPC1 &lt;- res.pca$var$coord[,1] vPC2 &lt;- res.pca$var$coord[,2] vlabs &lt;- rownames(res.pca$var$coord) vPCs &lt;- data.frame(cbind(vPC1,vPC2)) rownames(vPCs) &lt;- vlabs colnames(vPCs) &lt;- colnames(PCs) # # and plot them # pv &lt;- ggplot() + opts(aspect.ratio=1) + theme_bw(base_size = 20) # no data so there's nothing to plot # put a faint circle there, as is customary angle &lt;- seq(-pi, pi, length = 50) df &lt;- data.frame(x = sin(angle), y = cos(angle)) pv &lt;- pv + geom_path(aes(x, y), data = df, colour="grey70") # # add on arrows and variable labels pv &lt;- pv + geom_text(data=vPCs, aes(x=vPC1,y=vPC2,label=rownames(vPCs)), size=4) + xlab("PC1") + ylab("PC2") pv &lt;- pv + geom_segment(data=vPCs, aes(x = 0, y = 0, xend = vPC1*0.9, yend = vPC2*0.9), arrow = arrow(length = unit(1/2, 'picas')), color = "grey30") pv # show plot # # clear the plot dev.off() # # Now put them side by side # library(gridExtra) grid.arrange(p,pv,nrow=1) # # Now they can be saved or exported... # # tidy up by deleting the plots # dev.off() </code></pre> <p>And here's what the final plots looks like, perhaps the text size on the left plot could be a little smaller:</p> <p><img src="https://i.stack.imgur.com/M5ASm.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload