在R中,如何为系统发育树中的标签着色? (使用猿的BioNj)

时间:2019-10-25 08:30:07

标签: r heatmap

所以我有一个像这样的数据集:

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

从该数据集中,我可以使用以下公式计算R中的相关矩阵和热图:

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)

此后,我想使用猿库的bionj函数制作系统发育树

arbol <- bionj(correlationData)

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

在这里我被卡住了,所以我读到可以通过添加一行指示标签应位于哪个颜色组中来更改标签的颜色。因此,我添加了此列以创建新的数据集:

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

我可以通过这种方式为标签上色吗? 因此,名称中没有单元格类型的所有内容(因此命名为sample_x)应具有相同的颜色,所有单元格类型均应具有相同的颜色(因此名为celltypeX_sampleY)

我希望我的问题很清楚,甚至有可能做到这一点...

指向dataset

的链接

1 个答案:

答案 0 :(得分:2)

您可以在plot.phylo函数中指定它。 bionj返回一个“ phylo”类,当您调用plot(arbol1,cex = 0.25,edge.width = 0.5)时,您实际上是在使用plot.phylo。您可以键入?plot.phylo以查看选项。

我没有您的数据,但是下面我使用示例数据集添加颜色标签。希望这就是您想要的

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

要将颜色添加到不同的标签,您可以尝试以下操作:

# 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