本期小数给大家带来的是Nature里面的配图教程(原文Single-cell transcriptional profile of CD34+ hematopoietic progenitor cells from del(5q) myelodysplastic syndromes and impact of lenalidomide.)
这张图的数据其实是就几个简单的点数据,而恰恰是这种简单的数据往往很难把图形画的好看,而这篇文章就给大家展示了一个很好的样例。
在开始正式的教程之前,我先给大家做一些关于我推文教程的说明。目前我的推文都是关于R语言的教程,其他的比如python、matlab之类的代码是没有的。以后也是以R的教程为主(目前主要是做数据可视化的教程,我已经很多年不用python画图了)。
下面进入正式的教程吧!!
本期教程需要使用以下包,没有安装的同学请先安装以下:
#没有安装包的需要提前安装包
# ComplexHeatmap这个包安装比较麻烦,按照下面的方法安装,其他包就直接安装就行
# devtools::install_github("jokergoo/ComplexHeatmap")
# 或者BiocManager::install("ComplexHeatmap")
library(tidyverse)
library(ComplexHeatmap)
library(cowplot)
-
左图的教程
这里直接使用
ComplexHeatma包的
Heatmap函数就可以了
library(tidyverse)
library(ComplexHeatmap)
data = readxl::read_xlsx("./data/heatmap20240820.xlsx",
sheet = 1) %>%
column_to_rownames(var = "row")
logs_pval_res as.matrix(data),
col = circlize::colorRamp2(c(0, 1, 3, 5, 10),
c("white", "white", "#f7bc65", "#f97c4c", "#D53E4F")),
heatmap_legend_param = list(
direction = "horizontal",
title = "-log Adj.Pvalue", at = c(0, 3, 5, 10),
labels = c('0', '3\n(Adj.Pval<0.05)' , '5', '10'),
border = "black"
),
rect_gp = gpar(col = "grey30", lwd = 0.5),
cluster_rows = FALSE,
cluster_columns = FALSE,
column_names_gp = grid::gpar(fontsize = 8, angle=45, fontface='italic'),
row_names_gp = grid::gpar(fontsize = 8),
width = ncol(data)*unit(4, "mm"),
height = nrow(data)*unit(6, "mm"),
column_names_rot = 90)
ComplexHeatmap::draw(logs_pval_res, heatmap_legend_side = "bottom")
2.右图教程
右图是用ggplot2来画的,右边这幅图本质上就是个气泡图,直接用geom_point()画出点然后映射散点的大小即可~
data = readxl::read_xlsx("./data/heatmap20240820.xlsx",
sheet = 2)
cell_type_order
'HSC',
'LMPP',
'GMP',
'Granulocyte',
'Monocytes',
'DendriticCell',
'CLP',
'pro-B',
'T',
'MEP',
'MK_Prog',
'EarlyErythroid',
'LateErythroid',
'Basophil')
data %>%
mutate(Combined.Score = if_else(Combined.Score>200, 200, Combined.Score),
Term = stringr::str_remove(tmp$Term,'\\([^)]*\\)'),
cell_type = factor(cell_type, levels=cell_type_order)) -> df
pal = c("#544580", "#3b7ac5", "#38c0a8", "#8de2a1", "#f7bc65", "#f97c4c", "#D53E4F")
ggplot(data = df) +
geom_point(mapping= aes(y=Term, x=cell_type, size =Adjusted.P.value, fill=Combined.Score),
color='black', shape=21, stroke=0.35) +
scale_fill_gradientn(colors = pal)+
scale_size_continuous(range = c(4, 0.5)) +
scale_y_discrete(labels = function(x) stringr::str_wrap(x, width = 40)) +
facet_wrap(~Comparison) +
guides(fill=guide_colorbar(title='Combined\nScore'),
size=guide_legend(title='Adjusted\nP.value')) +
theme_minimal() +
theme