【问题标题】:Circular tree with heatmap带热图的圆形树
【发布时间】:2020-11-17 07:01:21
【问题描述】:

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

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

我正在使用ggtree,但欢迎任何基于ggplo2 的方法。 gheatmap函数我不太了解的问题。

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

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")

提前致谢

更新:

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

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")

更新 2:

一个非常糟糕的改进 我只是使用ggplot创建标签并与patchwork合并

library(patchwork)

p$data %>% 
  ggplot(aes(1, y= y, label = label )) +
  geom_text(size=2) +
  xlim(NA, 1) +
  theme_classic() +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()) -> adText

pp + adText

【问题讨论】:

    标签: r ggplot2 tidyverse heatmap ggtree


    【解决方案1】:

    根据@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
    

    使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 2021-09-28
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      相关资源
      最近更新 更多