分享

使用 aplot “拼”一个热图

 阿越就是我 2023-10-12 发布于上海
  Stay hungry, stay foolish!   

aplot也是一个拼图包,但是使用场景和普通的拼图R包略有不同,它可以让你的坐标轴完全对齐!这让你在拼不同类型但是又使用同一个数据的图形时,更加具有可比性。

  • 安装

  • 使用

    • 添加边际图形

    • 拼图

  • 拼热图

安装

# 2选1
install.packages("aplot")

## install.packages("remotes")
remotes::install_github("YuLab-SMU/aplot")

使用

aplot是可以进行拼接简单的边际图形的,这有点类似于ggExtra,但是比ggExtra更加好用,可以自动对齐坐标轴。

添加边际图形

接下来先说一个aplot的简单应用场景,添加边际图形!

library(ggplot2)
library(aplot)
p1 <- ggplot(iris, aes(Sepal.Length,Sepal.Width))+
  geom_point(aes(color = Species))+theme_bw()
p1
plot of chunk unnamed-chunk-3
p2 <- ggplot(iris, aes(x=Species, y=Sepal.Length))+
  geom_boxplot(aes(fill=Species))+theme_bw()+labs(x=NULL)+guides(fill="none")
p2
image-20220505152731375
p3 <- ggplot(iris, aes(x=Sepal.Length))+
  geom_density(fill="steelblue",alpha=0.6)+theme_bw()
p3
plot of chunk unnamed-chunk-5
ap <- p1 %>% 
  insert_top(p3,height = 0.2) %>% 
  insert_left(p2,width = 0.1)
ap
image-20220505152755830

你看上面这个示例,可以说是成也萧何败萧何,因为它会给你对齐坐标轴,就像上图中的上下图一样,横坐标是完全一样的,对于纵坐标它也是这么处理的。所以会导致你的箱线图显示不出来!当然这和你的数据也有关系!

你可以认为这是aplot这个包不太擅长这种拼图,也可以认为是你没理解这个包的真正用处。因为下面的例子才是aplot真正的主场!

下面是更合适的用法!

拼图

比如你要组合聚类图形和其他图形时,聚类图形和热图等组合时。

library(ggtree)
## ggtree v3.2.1  For help: https:///treedata-book/
## 
## If you use ggtree in published research, please cite the most appropriate paper(s):
## 
## 1. Guangchuang Yu. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics. 2020, 69:e96. doi:10.1002/cpbi.96
## 2. Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution. 2018, 35(12):3041-3043. doi:10.1093/molbev/msy194
## 3. Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
set.seed(2020-03-27)
x <- rtree(10)
d <- data.frame(taxa=x$tip.label, value = abs(rnorm(10)))
p <- ggtree(x) + geom_tiplab(align = TRUE) + xlim(NA3)

library(ggstance)
## 
## 载入程辑包:'ggstance'
## The following objects are masked from 'package:ggplot2':
## 
##     geom_errorbarh, GeomErrorbarh
p2 <- ggplot(d, aes(value, taxa)) + geom_colh() + 
  scale_x_continuous(expand=c(0,0)) 
p2 %>% insert_left(p)
plot of chunk unnamed-chunk-8

你看,在这个例子中,左边和右边的图纵坐标自动对齐了,一模一样!

当然,你还可以很方便组合更多的图形,坐标轴都会给你对齐。

p3 <- ggplot(d, aes(taxa,value))+geom_line(aes(group=1))+geom_point()+coord_flip()

p2 %>% 
  insert_left(p) %>% 
  insert_right(p3,width = 0.3)
plot of chunk unnamed-chunk-9

是不是很简单?

拼热图

有了这个思路,那我们就可以联合ggplot2拼一个热图出来!

众所周知,ggplot2是可以画热图的,我前面的推文也是有介绍的,请翻看:R语言ggplot2画相关性热图

library(tidyverse)
## -- Attaching packages ----------------------------- tidyverse 1.3.1 --
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.1     v forcats 0.5.1
## v purrr   0.3.4
## -- Conflicts -------------------------------- tidyverse_conflicts() --
## x tidyr::expand()            masks ggtree::expand()
## x dplyr::filter()            masks stats::filter()
## x ggstance::geom_errorbarh() masks ggplot2::geom_errorbarh()
## x dplyr::lag()               masks stats::lag()
library(ggtree)


## 编一个数据
set.seed(123)
d <- matrix(rnorm(100), ncol=10)
rownames(d) <- paste0('g'1:10)
colnames(d) <- paste0('s'1:10)

## 画聚类树
hc <- hclust(dist(d))
hcc <- hclust(dist(t(d)))
phr <- ggtree(hc)
phc <- ggtree(hcc) + layout_dendrogram()

phr
plot of chunk unnamed-chunk-10
phc
plot of chunk unnamed-chunk-10
## 画热图
df_heat <- as.data.frame(d) %>% 
  rownames_to_column(var = "gene") %>% 
  pivot_longer(cols = - gene, names_to = "sample",values_to = "value")

pheat <- ggplot(df_heat, aes(sample,gene,fill=value))+geom_tile()+
  scale_y_discrete(position = "right")+
  scale_fill_gradient2(low = "blue",high = "red")+
  labs(x=NULL,y=NULL)+
  theme_minimal()

pheat
plot of chunk unnamed-chunk-11
## 给热图增加一个注释条
df_anno <- data.frame(samples = paste0("s",1:10),
                      col1 = rep(LETTERS[1:3],times=c(3,3,4)),
                      col2 = rep(LETTERS[4:7],times=c(2,3,3,2))
                      )

df_anno <- df_anno %>% 
  pivot_longer(cols = 2:3,names_to = "col",values_to = "split")
df_anno
## # A tibble: 20 x 3
##    samples col   split
##    <chr>   <chr> <chr>
##  1 s1      col1  A    
##  2 s1      col2  D    
##  3 s2      col1  A    
##  4 s2      col2  D    
##  5 s3      col1  A    
##  6 s3      col2  E    
##  7 s4      col1  B    
##  8 s4      col2  E    
##  9 s5      col1  B    
## 10 s5      col2  E    
## 11 s6      col1  B    
## 12 s6      col2  F    
## 13 s7      col1  C    
## 14 s7      col2  F    
## 15 s8      col1  C    
## 16 s8      col2  F    
## 17 s9      col1  C    
## 18 s9      col2  G    
## 19 s10     col1  C    
## 20 s10     col2  G


pheat_anno <- ggplot(df_anno,aes(samples,col,fill=split))+geom_tile()+
  scale_y_discrete(position = "right")+
  labs(x=NULL,y=NULL)+
  theme_minimal()+
  theme(axis.text.x = element_blank(), 
        axis.ticks.x = element_blank())

pheat_anno
plot of chunk unnamed-chunk-12

一个热图的基本组件就画完了,接下来就可以愉快的组图了!

pheat %>% 
  insert_top(pheat_anno,height = 0.1) %>% 
  insert_top(phc,height = 0.1) %>% 
  insert_left(phr,width = 0.1)
plot of chunk unnamed-chunk-13

你看着这图,是不是很像模像样?你分别画了4个图,但是经过神奇的操作之后,它就变成了一个热图!这就是aplot这个包最神奇的地方。

既然如此,那么再组合其他图形也是不在话下了!

## 再画一个箱线图
pbox <- ggplot(df_heat,aes(gene,value))+
  geom_boxplot()+
  theme_minimal()+
  theme(axis.text.y = element_blank(), 
        axis.ticks.y = element_blank())+
  coord_flip()+
  labs(x=NULL,y=NULL)
pbox
plot of chunk unnamed-chunk-14

全部组合到一起!

pheat %>% 
  insert_top(pheat_anno,height = 0.1) %>% 
  insert_top(phc,height = 0.1) %>% 
  insert_left(phr,width = 0.1) %>% 
  insert_right(pbox,width = 0.3)
Snipaste_2022-05-05_12-55-26

大功告成!

就是这么神奇!就是这么简单,且好玩!

大家快快操作起来!

以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章