Colour tip labels in a tree based on wildcard/expression using ggtree
0
0
Entering edit mode
3.1 years ago
mrmrwinter ▴ 30

Hi,

I need to create a tree with tip labels coloured according to wildcards/expressions in the tip name. Ive been trying to do this with ggtree and having some success but i cant create a consistent colour scheme.

Say I have a tree wherein tiplabels begin with either A1, A2, or B. I would like to code it so that A1 is always colour 1, A2 is always colour 2, and B is always colour 3.

My current code colours them different colours but this colour changed depending on the presence of B in the tree; some trees only contain A1 and A2. Because of this, when the tree contains B the colour scheme changes from those that contain only A1 and A2.

My current code is this:

tree <- read.tree(nwk)
seq = tree$tip.label
dd = data.frame(seq)
dd$newname = 0
dd$color = 0
dd[grep("A1", dd$seq), "color"] <- "A1"
dd[grep("A2", dd$seq), "color"] <- "A2"
tipcol <- rep('black', length(tree$tip.label))
isolates = c("A1", "A2")
colorsList <-c("red", "blue")
p = ggtree(tree)
p = p + geom_label2(aes(label=label, subset = !is.na(as.numeric(label)) & as.numeric(label) > 80))
p = p %<+% dd + geom_tiplab(aes(fill = factor(color)), size = 3, color = "black", geom = "label", label.padding = unit(0.10, "lines"), label.size = 0)
p

I want to create code that labels A1 blue, A2 red, and B, if present in the tree, green.

Thanks for your help

ggtree colour R phylogeny dendrogram • 1.4k views
ADD COMMENT
1
Entering edit mode

You can use block code formatting to format a code block instead of formatting each line. Select all lines and hit the 101010 button on the toolbar.

code_formatting

ADD REPLY

Login before adding your answer.

Traffic: 1604 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6