Advertisement
Guest User

Untitled

a guest
May 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. ---
  2. title: "Export PCoA plot in other formats"
  3. output: html_notebook
  4. ---
  5.  
  6. ## Problem
  7.  
  8. 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.
  9.  
  10. ## Solution
  11.  
  12. As the export in other formats is not implemented, it has to be done externally. There are two options here:
  13.  
  14. ### Using the same function as msap but as an external script
  15. 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)
  16.  
  17. ```{r, eval=FALSE}
  18.  
  19. res <- msap("vignettes/example.csv","kk") #CHANGE TO FIT YOPUR ANALYSIS
  20. library(ade4)
  21. library(dplyr)
  22. plotPCs <- function(pca, fullname=" "){
  23. # Function adapted to plot the PCAs/PCOs
  24. var <- pca$eig / sum(pca$eig) *100
  25.  
  26. var1 <- round(var[1], digits=1)
  27. var2 <- round(var[2], digits=1)
  28.  
  29. spcoo<-split(pca$li, groups)
  30. maxX <- max(pca$li[,1])
  31. minX <- min(pca$li[,1])
  32. maxY <- max(pca$li[,2])
  33. minY <- min(pca$li[,2])
  34.  
  35.  
  36. 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)
  37. bgcolors<-rainbow(5)[-2] #No yellow?
  38. symbs <- c(21,22,23,24)
  39. for(i in 1:4){
  40. points(spcoo[[i]], pch=21, col="black", bg=bgcolors[i])
  41. }
  42. s.class(pca$li, groups, cpoint=0, col=bgcolors, add.plot=TRUE, cstar=1)
  43. }
  44.  
  45. #SAVE TO EPS
  46. setEPS()
  47. postscript("PCoA.MSL.eps")
  48. #MSL PCoA reloaded
  49. groups <- res$groups # They are in different order than in morph
  50. plotPCs(dudi.pco(quasieuclid(res$DM.MSL), nf=2, scannf = F), "MSL")
  51. dev.off()
  52. ```
  53.  
  54. ### Make you figure externally (Excel, ....)
  55. 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement