Mark Wekking
Mark Wekking

Reputation: 283

in R how can I color the labels from my phylogenetic tree? (using BioNj from ape)

So I have a dataset which looks like this:

Pos sample_1 sample_2 celltypeX_sample3 celltypeY_sample4 celltypeX_sample5
0     0        0              3                  0             1
2     2        1              3                  0             0
5     0        0              0                  0             1
6     1        0              0                  1             0
12    0        1              0                  1             1

from this dataset I can calculate a correlation matrix and a heatmap in R with:

data = read.table(file = "fileNameX", row.names = 1, header = T, sep = "\t")
correlationData = cor(data)
heatmap(correlationData, cexRow = 0.25, cexCol = 0.25, symm = T)

after this I want to make a phylogenetic tree using the bionj function of the ape library

arbol <- bionj(correlationData)

plot(arbol1, cex = 0.25, edge.width = 0.5)

Here is where I get stuck, so i have read that it is possible to change te color of the labels by adding a row that indicates in which color group it should be in. So I added this column which creates the new dataset:

Pos sample_1 sample_2 celltypeX_sample3 celltypeY_sample4 celltypeX_sample5
0     0        0              3                  0             1
2     2        1              3                  0             0
...
7026  0        1              0                  1             1
clr   0        0              1                  2             1

Is there any way I could color the labels in this way? So everything without a celltype in the name (thus named sample_x) should have the same colour and all cell types should have the same color (thus named celltypeX_sampleY)

I hope my question is clear and it is even possible to do this...

a link to the dataset

Upvotes: 1

Views: 2161

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

You can specify it in the plot.phylo function. bionj returns a "phylo" class, and when you call plot(arbol1, cex = 0.25, edge.width = 0.5), you are actually using plot.phylo. You can type ?plot.phylo to see the options.

I don't have your data, but below I use an example dataset to add the color label.. Hope this is what you wanted

library(ape)
data(woodmouse)
trw <- bionj(dist.dna(woodmouse))
# we label samples that have No120 as blue
# others orange
COLS = ifelse(grepl("No120",trw$tip.label),"blue","orange")
plot(trw,tip.color=COLS)

enter image description here

To add colours to different labels, you can try this:

# from https://www.r-bloggers.com/the-paul-tol-21-color-salute/
tol18rainbow=c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122", "#AA4455", "#DD7788")
# I assume here, the word before the "_" tells us how to colour the label
TYPE = gsub("_[^ ]*","",arbol$tip.label)
# check the TYPE numbers are correct
col_assignment = tol18rainbow[1:length(unique(TYPE))]
names( col_assignment) = unique(TYPE)
COLS = col_assignment[TYPE]
# then pass COLS into your plot

Upvotes: 2

Related Questions