导语
GUIDE ╲
作为目前最常见的一种可视化手段,热图因其丰富的色彩变化和生动饱满的信息表达被广泛应用于各种大数据分析场景。
背景介绍
相信大家对热图一定都不陌生了,在数据的可视化分析中,是经常使用热图去做展示的,基于R语言的一些热图绘制R包,如pheatmap,complexheatmap等等,都是常用的工具,今天小编给大家介绍的是一个基于ggplot2的热图绘制工具--ggheatmap,基于ggplot2可以使我们在热图的绘制过程中更加个性化!
R包安装
代码语言:javascript复制BiocManager::install("ggheatmap")
library(ggheatmap)
可视化介绍
01
基本用法
首先对数据进行预处理
代码语言:javascript复制library(tidyverse)
library(Jerry)
data(arms_data)
#得到AMP相关性矩阵
arms_data_dist_amp = arms_data %>%
select(starts_with("Amp")) %>%
cor() %>%
as.matrix()
#得到Del相关性矩阵
arms_data_dist_del = arms_data %>%
select(starts_with("Del")) %>%
cor() %>%
as.matrix()
获取相关矩阵的下三角或上三角
代码语言:javascript复制#设置上三角为NA
arms_data_dist_amp %>%
get_tri(diag = TRUE, upper.tri = TRUE)
## Amp_ACA Amp_NEC Amp_CIN Amp_EBV Amp_GS Amp_MSI
## Amp_ACA NA NA NA NA NA NA
## Amp_NEC 0.8971477 NA NA NA NA NA
## Amp_CIN 0.7313157 0.6677643 NA NA NA NA
## Amp_EBV 0.6380635 0.6225627 0.7994932 NA NA NA
## Amp_GS 0.6683191 0.6172034 0.7019668 0.6668315 NA NA
## Amp_MSI 0.4838802 0.5112640 0.5480767 0.5583757 0.8330379 NA
代码语言:javascript复制arms_data_long_amp = arms_data_dist_amp %>%
get_tri(diag = TRUE, upper.tri = TRUE) %>%
reshape2::melt(na.rm = TRUE) %>%
mutate(
Var1 = mapply(function(x) x[2], str_split(Var1, "_") ),
Var2 = mapply(function(x) x[2], str_split(Var2, "_") )
)
head(arms_data_long_amp
代码语言:javascript复制## Var1 Var2 value
## 2 NEC ACA 0.8971477
## 3 CIN ACA 0.7313157
## 4 EBV ACA 0.6380635
## 5 GS ACA 0.6683191
## 6 MSI ACA 0.4838802
## 9 CIN NEC 0.6677643
使用ggheatmap进行可视化
代码语言:javascript复制arms_data_long_amp = arms_data_long_amp %>%
mutate(Var1 = factor(Var1, levels = c("ACA","NEC","CIN","EBV","GS","MSI"),
labels = c("MANEC-Aca","MANEC-Nec","STAD-CIN","STAD-EBV","STAD-GS","STAD-MSI")
),
Var2 = factor(Var2, levels = c("ACA","NEC","CIN","EBV","GS","MSI"),
labels = c("MANEC-Aca","MANEC-Nec","STAD-CIN","STAD-EBV","STAD-GS","STAD-MSI"))
)
# Create a ggheatmap
ggheatmap <- ggplot(arms_data_long_amp, aes(Var2, Var1, fill = value))
geom_tile(color = "white")
scale_fill_gradient2(low = "blue", high = "red", mid = "grey100",
midpoint = 0.5, limit = c(0, 0.9), space = "Lab",
name="PearsonnCorrelation")
theme_minimal() # minimal theme
theme(axis.text.x = element_text(angle = 45, vjust = 1,
size = 10, hjust = 1),
axis.text.y = element_text(
size = 10)
)
coord_fixed()
geom_rect(xmin = 0.5, xmax = 3.5, ymin = 0.5, ymax = 3.5, col = "black", alpha = 0)
ggheatmap
02
绘制复杂一些的热图
代码语言:javascript复制##生成一个矩阵用来绘图
set.seed(123)
df <- matrix(runif(225,0,10),ncol = 15)
colnames(df) <- paste("sample",1:15,sep = "")
rownames(df) <- sapply(1:15, function(x)paste(sample(LETTERS,3,replace = F),collapse = ""))
df[1:4,1:4]
##对数据做一些注释和分组
row_metaData <- data.frame(exprtype=sample(c("Up","Down"),15,replace = T),
genetype=sample(c("Metabolism","Immune","None"),15,replace = T))
rownames(row_metaData) <- rownames(df)
col_metaData <- data.frame(tissue=sample(c("Normal","Tumor"),15,replace = T),
risklevel=sample(c("High","Low"),15,replace = T))
rownames(col_metaData) <- colnames(df)
##定义各个板块的颜色,整合为col
exprcol <- c("#EE0000FF","#008B45FF" )
names(exprcol) <- c("Up","Down")
genecol <- c("#EE7E30","#5D9AD3","#D0DFE6FF")
names(genecol) <- c("Metabolism","Immune","None")
tissuecol <- c("#98D352","#FF7F0E")
names(tissuecol) <- c("Normal","Tumor")
riskcol <- c("#EEA236FF","#46B8DAFF")
names(riskcol) <- c("High","Low")
col <- list(exprtype=exprcol,genetype=genecol,tissue=tissuecol,risklevel=riskcol)
text_rows <- sample(rownames(df),3)
p<- ggheatmap(df,cluster_rows = T,cluster_cols = T,scale = "row",
text_show_rows = text_rows,
cluster_num = c(3,3),
##聚类树的颜色
tree_color_rows = c("#008B45FF","#631879FF","#008280FF"),
tree_color_cols = c("#1F77B4FF","#FF7F0EFF","#2CA02CFF"),
##行和列的注释信息
annotation_rows = row_metaData,
annotation_cols = col_metaData,
annotation_color = col
)
p
展示热图的组成
代码语言:javascript复制ggheatmap_plotlist(p)
03
ggheatmap_theme()
ggheatmap_theme() 是ggheatmap包特有的函数,能设置ggheatmap除聚类树外各组件绘制元素的样式。
代码语言:javascript复制p%>%
ggheatmap_theme(1:5,
theme =list(
##设置列名垂直,字体
theme(axis.text.x = element_text(angle = 90,face = "bold",size = 10),
axis.text.y = element_text(colour = "red",face = "bold")),
theme(legend.title = element_text(face = "bold")),
theme(legend.title = element_text(face = "bold")),
theme(legend.title = element_text(face = "bold")),
theme(legend.title = element_text(face = "bold"))
))
改变马赛克形状
代码语言:javascript复制ggheatmap(df,cluster_rows = T,cluster_cols = T,scale = "row",
text_show_rows = text_rows,
##边框颜色
border = "grey",
##形状
shape = "circle",
cluster_num = c(3,3),
tree_color_rows = c("#008B45FF","#631879FF","#008280FF"),
tree_color_cols = c("#1F77B4FF","#FF7F0EFF","#2CA02CFF"),
annotation_rows = row_metaData,
annotation_cols = col_metaData,
annotation_color = col
)%>%
ggheatmap_theme(1,theme =list(theme(axis.text.x = element_text(angle = 90,face = "bold",size = 10),
axis.text.y = element_text(colour = "red",face = "bold"))))
布局改变
代码语言:javascript复制ggheatmap(df,cluster_rows = T,cluster_cols = T,scale = "row",
text_show_rows = text_rows,
border = "grey",
cluster_num = c(3,3),
tree_color_rows = c("#008B45FF","#631879FF","#008280FF"),
tree_color_cols = c("#1F77B4FF","#FF7F0EFF","#2CA02CFF"),
annotation_rows = row_metaData,
annotation_cols = col_metaData,
annotation_color = col,
##下面参数可以设置各个元素的布局位置
text_position_rows = "left",
text_position_cols = "top",
tree_position_rows = "right",
tree_position_cols = "bottom",
annotation_position_rows = "right",
annotation_position_cols = "bottom"
)%>%
ggheatmap_theme(1,theme =list(theme(axis.text.x = element_text(angle = 90,face = "bold",size = 10),
axis.text.y = element_text(colour = "red",face = "bold"))))
小编总结
关于基本热图的绘制大家也都很熟悉了,通过pheatmap可以简单的画出热图,但是基于ggplot2的诸多参数,可以让我们绘图更加自由,让图形可变性更强,从而达到最理想的效果,大家不妨多多尝试一下ggheatmap!