Skip to content

Instantly share code, notes, and snippets.

@anpefi
Created May 29, 2017 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anpefi/17e4842c5fb9321cebaaf5fb4c0863d1 to your computer and use it in GitHub Desktop.
Save anpefi/17e4842c5fb9321cebaaf5fb4c0863d1 to your computer and use it in GitHub Desktop.
For pea¡ple asking how to modify the default NJ tree plotted by msap.
---
title: "msap: tweak NJ plot"
output: html_notebook
---
## Problem
The NJ plot form msap is awful, specially when the number of samples is big (>10). It was not intended to be very exahsutive, just as an exploratory analysis, but people seems to want to show it.
## Solution
We need to replot the NJ modifying the source code form msap.
First, we need the stored object with the msap results (i.e. `res <- msap(YOUROPTIONS)`)
Then we can write a small script to plot again the NJ tree and tweak it by changing options in the png() and plot.phylo() functions (see ?png and ?plot.phylo to check options). In the following example it changes the default size (480x480px) for the png to 500x900px. Change those values for different outputs.
```{r, eval=FALSE}
library(ape)
edgeCol <- function(x, n, pal){
if(x > n) col <- "black"
else col <-pal[x]
return(col)
}
DM_copy <- res$DM.MSL
attr(DM_copy, "Labels") <- as.character(res$transformed.MSL[,2])
groups <- res$groups
np <- table(groups)[]
MSL.cluster <-nj(DM_copy)
tipCol<-rep(rainbow(length(np)+1)[-2], np)
ecol<-unlist(lapply(MSL.cluster$edge[,2], edgeCol, length(MSL.cluster$tip.label), tipCol))
png(filename="MSL-NJ.png",width = 500, height = 900) #HERE YOU CAN CHANGE SIZE (SEE ?png for further options)
plot.phylo(MSL.cluster, tip.color=tipCol, use.edge.length=T, edge.color=ecol, edge.width=3, show.tip.label=T, main="MSL")
legend("bottomright", as.character(levels(groups)), col=rainbow(length(np)+1)[-2], lwd=3)
dev.off()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment