Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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}
edgeCol <- function(x, n, pal){
if(x > n) col <- "black"
else col <-pal[x]
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment