我正在使用一个名为 的软件ggtree
,它基于ggplot2
并允许您可视化系统发育树并向其添加元数据。调用将元数据添加为彩色热图的函数,gheatmap
据我了解,它只是将 data.frame 或矩阵中的行名与树的标签相关联。其余与之相关的语言基本上是ggplot2
.
数据在这里:https ://github.com/CJREID/gheatmap
使用以下代码,我生成了以下图像。
library(ggtree)
#read in tree
tree <- read.tree("example_tree_overflow.tree")
p <- ggtree(tree)
#add tip labels and scale
p <- p + geom_tiplab(size = 2.4, align = TRUE, linesize = .2) +
geom_treescale()
#group and colour clades
p <- p %>% groupClade(node=c(72,75,84)) + aes(color=group) +
scale_color_discrete() +
theme(legend.position = NULL)
#import data.frame and add column names
meta <- read.csv("metadata_overflow.csv", stringsAsFactors = TRUE)
rownames(meta) <- meta[,1]
meta <- meta[,2:7]
colnames(meta) <- c("Origin", "Syndrome", "fimH", "CTX-M","gyrA","parC")
#variable to generate the breaks in the legend
q1 <- c("REF","SAN","OBH","CoH",
"Cystitis","Pyelonephritis","Sepsis",
"H22","H30","H41",
"CTX-M-1_15","CTX-M-9_18","CTX-M-9_27",
"WT","S83L", "S83L_D87N",
"WT","S80I_E84V")
#colour values for the legend, sorry they're awful, I will fix them later
c1 <- c("black","blue","green","red",
"yellow","purple","firebrick",
"turquoise","purple","olivedrab",
"red","purple","turquoise",
"white","purple","turquoise",
"white", "turquoise")
#assign breaks to colours
names(c1) <- q1
#draw heatmap next to tree, assigning colours correctly to the variables
gheatmap(p, meta, colnames_position = "top", width = .06, offset = .025,
font.size = 2, colnames_angle = 50, colnames_offset_y = .2,
colnames_offset_x = -0.0019, hjust = 0) +
theme(legend.title = element_text()) +
scale_fill_manual(name="Origin",
breaks=q1,
values=c1)
如您所见,我添加的数据分为 6 个类别"Origin", "Syndrome", "fimH", "CTX-M", "gyrA", "parC"
,每个类别都有自己的变量,如下所示,
>plots
$Origin
[1] "REF" "SAN" "OBH" "CoH"
$Syndrome
[1] "Cystitis" "Pyelonephritis" "Sepsis"
$fimH
[1] "H22" "H30" "H41"
$`CTX-M`
[1] "CTX-M-1_15" "CTX-M-9_18" "CTX-M-9_27"
$gyrA
[1] "WT" "S83L" "S83L_D87N"
$parC
[1] "WT" "S80I_E84V".
我有两个主要问题:
我想将这些变量拆分为图例上各自的标题,因此它们是六个离散的图例。如您所见,我只设法将一个标题“起源”放在了所有这些标题之上。(我尝试使用上面的这个
plots
对象而不是q1
作为breaks
inscale_fill_manual
无济于事)WT
我希望单独处理undergyrA
和WT
under的变量,parC
即在各自的分类标题下的图例中有自己的框。
希望解决方案没有我想的那么复杂
干杯。