ggplot2绘图可视化基因组数据绘图

如何给热图加两个树 | R

2021-12-13  本文已影响0人  kkkkkkang

pheatmap出的图可以有上和左两个hclust聚类图,但那是根据矩阵数据聚类的,若是换成了进化树怎么办?左边一个进化树很容易,ggtree的gheatmap一句话就加上了,但还要再加一个对应的呢?

使用aplot包拼图实现

先读入需要的数据

# sessionInfo()
# R version 4.1.1 (2021-08-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 18363)

setwd("D:/heat_two_tree/")
library(ggplot2)
library(ggtree)
library(treeio)
library(aplot)
library(reshape2)
tr <- read.tree("tree.bestTree") #首先读入树

heat <- read.table("heat.txt", check.names = F) %>%
    rownames_to_column("x") # 读入热图矩阵

heat2 <- melt(heat, id.vars = "x", variable.name = "y") # 变长数据供ggplot使用
heat2$value <- as.character(heat2$value) # 二元数据换成character
anno <- read.table("annota.txt", sep = "\t", header = T) # 树的注释数据

树文件


树文件

热图数据文件(随机生成的)


热图矩阵
树注释文件
树注释

下面开始画图

p1 <- ggtree(tr) %<+% anno # %<+% 是把注释信息加到树上的一个符号
p1
p2 <- p1 + geom_tippoint(aes(color = Species)) +
    # geom_tiplab(aes(color = Species), offset = 0) + # 可添加枝末文字注释,这里是直接在热图里加上的,因为后面用到的翻转树,它字体并没有翻转,不行
    geom_treescale(x= 0.5, y = 0, offset = 0.5) + 
    scale_color_manual(values = c("#e76f51","#ca6702","#d3d3d3",
                                  "#ee9b00","#94d2bd","#98c1d9",
                                  "#0a9396","#e9d8a6")) +
    theme(legend.text=element_text(size=12, face = "italic"),
          legend.title = element_text(size = 12, face = "bold")) 
p2
# 这里是对树进行注释

p3 <- p1 + geom_tippoint(aes(color = Species)) +
    # geom_treescale(x= 0.1, y = 5, offset = 0.5) + # 已经有了标尺
    scale_color_manual(values = c("#e76f51","#ca6702","#d3d3d3",
                                  "#ee9b00","#94d2bd","#98c1d9",
                                  "#0a9396","#e9d8a6")) +
    coord_flip() +
    theme(legend.position = "none") # 去掉重复的图例
p3

pp <- ggplot(heat2, aes(x = x, y = y)) + 
    geom_tile(aes(fill = value)) + 
    theme(axis.ticks = element_blank(),
          axis.title = element_blank(),
          axis.text.x = element_text(angle = 90, vjust = 0.1),
          rect = element_blank()) +
    scale_fill_manual(values = c("white","tomato"),
                      labels = c("No","Yes"),name = "Legend")+
    theme(legend.text=element_text(size=12),
          legend.title = element_text(size = 12, face = "bold"))
pp
p <- pp %>% insert_left(p2, width = 0.18) %>%
    insert_bottom(p3, height = 0.1)
p
ggsave("heat_two_tree.pdf", p, width = 7, height = 4.5)

p2


p2

p3


p3
pp
pp

p


p
上一篇下一篇

猜你喜欢

热点阅读