带有热图的圆形树

时间:2020-07-27 21:37:19

标签: r ggplot2 tidyverse heatmap ggtree

这个问题很琐碎,但我不能很好地处理。

我正在尝试绘制带有侧面热图的圆形树。

我正在使用mySwitch.enableReceive(digitalPinToInterrupt(13)),但是欢迎使用任何基于ggtree的方法。 我对ggplo2函数不太了解的问题。

我要:
1-在热图后命名
热图后的2至2个文本列(for可能具有相同的值,但我需要知道如何添加它)
3-热图列名称处理得很好,是否应该删除列名称并为每个列使用不同的色标?无论解决方案落在哪里,都可能比现在更好。

gheatmap

This is the result I had with this code

预先感谢

更新:

我找到了一种更好的方法来绘制热图列的名称。
另外,我发现简化数据对于 清理一点技巧标签。 现在,我只需要在Heatmap之后添加两个文本列即可。

library(tidyverse)
library(ggtree)
library(treeio)
library(tidytree)

beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
beast_tree <- read.beast(beast_file)

genotype_file <- system.file("examples/Genotype.txt", package="ggtree")
genotype <- read.table(genotype_file, sep="\t", stringsAsFactor=F)
colnames(genotype) <- sub("\\.$", "", colnames(genotype))
p <- ggtree(beast_tree, mrsd="2013-01-01",layout = "fan", open.angle = -270) + 
  geom_treescale(x=2008, y=1, offset=2) + 
  geom_tiplab(size=2)

gheatmap(p, genotype, offset=5, width=0.5, font.size=3, 
         colnames_angle=-45, hjust=0) +
  scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), 
                    values=c("steelblue", "firebrick", "darkgreen"), name="genotype")

enter image description here

更新2:

非常不好的改进 我只是使用ggplot创建标签并与p <- ggtree(beast_tree) gheatmap( p, genotype, colnames=TRUE, colnames_angle=90, colnames_offset_y = 5, colnames_position = "top", ) + scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), values=c("steelblue", "firebrick", "darkgreen"), name="genotype")

合并
patchwork

enter image description here

1 个答案:

答案 0 :(得分:0)

根据@xiangpin at GitHub的答案。

geom_tiplabel的大偏移值:

p <- ggtree(beast_tree)  
p1 <- gheatmap(
         p, genotype, colnames=TRUE, 
         colnames_angle=-45,
         colnames_offset_y = 5,
         colnames_position = "bottom",
         width=0.3,
         hjust=0, font.size=2) +
         scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), 
                    values=c("steelblue", "firebrick", "darkgreen"), name="genotype") +
         geom_tiplab(align = TRUE, linesize=0, offset = 7, size=2) +
         xlim_tree(xlim=c(0, 36)) +
         scale_y_continuous(limits = c(-1, NA))
p1

Solution 1

使用ggtreeExtra

library(ggtreeExtra)
library(ggtree)
library(treeio)
library(ggplot2)

beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
genotype_file <- system.file("examples/Genotype.txt", package="ggtree")

tree <- read.beast(beast_file)
genotype <- read.table(genotype_file, sep="\t")

colnames(genotype) <- sub("\\.$", "", colnames(genotype))
genotype$ID <- row.names(genotype)

dat <- reshape2::melt(genotype, id.vars="ID", variable.name = "type", value.name="genotype", factorsAsStrings=FALSE)
dat$genotype <- unlist(lapply(as.vector(dat$genotype),function(x)ifelse(nchar(x)==0,NA,x)))

p <- ggtree(tree) + geom_treescale()

p2 <- p + geom_fruit(data=dat,
                     geom=geom_tile,
                     mapping=aes(y=ID, x=type, fill=genotype),
                     color="white") +
          scale_fill_manual(values=c("steelblue", "firebrick", "darkgreen"),
                            na.translate=FALSE) +
          geom_axis_text(angle=-45, hjust=0, size=1.5) +
          geom_tiplab(align = TRUE, linesize=0, offset = 6, size=2) +
          xlim_tree(xlim=c(0, 36)) +
          scale_y_continuous(limits = c(-1, NA))
p2

Solution 2