1

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

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

我正在使用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

在此处输入图像描述

4

1 回答 1

0

根据GitHub 上的@xiangpin的答案。

大偏移值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

解决方案 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

解决方案 2

于 2020-07-28T14:26:30.023 回答