Skip to content

Instantly share code, notes, and snippets.

@anpefi
Last active May 29, 2017 09:28
Show Gist options
  • Save anpefi/382ebbbe74be1e0d6b704c3fd04f043e to your computer and use it in GitHub Desktop.
Save anpefi/382ebbbe74be1e0d6b704c3fd04f043e to your computer and use it in GitHub Desktop.
Export PCoA plots from msap analyses to eps format
---
title: "Export PCoA plot in other formats"
output: html_notebook
---
## Problem
Some users of the *msap* package could be interested in exporting plots (PCoA) in a format other than png. Currently this is not implemented in msap.
## Solution
As the export in other formats is not implemented, it has to be done externally. There are two options here:
### Using the same function as msap but as an external script
The folloowing is an R script modified form the msap source to allow saving PCoA plots as eps. You need to store your msap analysis as an object (called kk here) nand then use the plotPCs function (that you can modify to tune in the final look&feel)
```{r, eval=FALSE}
res <- msap("vignettes/example.csv","kk") #CHANGE TO FIT YOPUR ANALYSIS
library(ade4)
library(dplyr)
plotPCs <- function(pca, fullname=" "){
# Function adapted to plot the PCAs/PCOs
var <- pca$eig / sum(pca$eig) *100
var1 <- round(var[1], digits=1)
var2 <- round(var[2], digits=1)
spcoo<-split(pca$li, groups)
maxX <- max(pca$li[,1])
minX <- min(pca$li[,1])
maxY <- max(pca$li[,2])
minY <- min(pca$li[,2])
plot(0,0, main=fullname, 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)
bgcolors<-rainbow(5)[-2] #No yellow?
symbs <- c(21,22,23,24)
for(i in 1:4){
points(spcoo[[i]], pch=21, col="black", bg=bgcolors[i])
}
s.class(pca$li, groups, cpoint=0, col=bgcolors, add.plot=TRUE, cstar=1)
}
#SAVE TO EPS
setEPS()
postscript("PCoA.MSL.eps")
#MSL PCoA reloaded
groups <- res$groups # They are in different order than in morph
plotPCs(dudi.pco(quasieuclid(res$DM.MSL), nf=2, scannf = F), "MSL")
dev.off()
```
### Make you figure externally (Excel, ....)
From the msap analysis you have two files MSL-PCoA.coor.csv and MSL-PCoA.eige.csv that you can use to plot it in any other plotting system. The coordinates file have the coordinates for each individual in each axis. The eigen file has the eigenvalues for each axis (so its relative explained variance if you divide by the sum of all of them)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment