Closed ixxmu closed 3 months ago
library(Seurat) # DotPlot()
library(tidyverse) # 数据整理
library(ComplexHeatmap) # 绘制气泡热图
library(circlize) # circlize::colorRamp2()
library(scRNAtoolVis) # 为了使用pbmc数据集
然后我们加载pbmc数据集
httest <- system.file("extdata", "htdata.RDS", package = "scRNAtoolVis")
pbmc <- readRDS(httest)
# add groups
pbmc$groups <- rep(c('stim','control'),each = 1319)
# add celltype
pbmc$celltype <- Seurat::Idents(pbmc)
# load markergene
data("top3pbmc.markers")
# check
head(top3pbmc.markers,3)
p <- DotPlot(object = pbmc, features = top3pbmc.markers$gene)
p
#提取数据
df<- p$data
exp_mat <- df %>%
dplyr::select(-pct.exp, -avg.exp) %>%
pivot_wider(names_from = id, values_from = avg.exp.scaled) %>%
as.data.frame()
row.names(exp_mat) <- exp_mat$features.plot
exp_mat <- exp_mat[,-1] %>% as.matrix()
head(exp_mat)
#数据2
percent_mat <- df %>%
dplyr::select(-avg.exp, -avg.exp.scaled) %>%
pivot_wider(names_from = id, values_from = pct.exp) %>%
as.data.frame()
row.names(percent_mat) <- percent_mat$features.plot
percent_mat <- percent_mat[,-1] %>% as.matrix()
head(percent_mat)
#调色板
library(RColorBrewer)
quantile(exp_mat, c(0.1, 0.5, 0.9, 0.99))
col_fun = circlize::colorRamp2(c(-0.6, 0, 2), brewer.pal(11, "PiYG")[c(11,8,1)])
cell_fun = function(j, i, x, y, w, h, fill){
grid.rect(x = x, y = y, width = w, height = h,
gp = gpar(col = NA, fill = NA))
grid.circle(x=x,y=y,r= percent_mat[i, j]/100 * min(unit.c(w, h)),
gp = gpar(fill = col_fun(exp_mat[i, j]), col = NA))}
Heatmap(exp_mat,
name = "hp",
heatmap_legend_param = list(title = "expression"),
col = col_fun,
rect_gp = gpar(type = "none"),
cell_fun = cell_fun,
row_names_gp = gpar(fontsize = 5),
row_km = 4,
border = "black")
httest <- system.file("extdata", "htdata.RDS", package = "scRNAtoolVis")
pbmc <- readRDS(httest)
# add groups
pbmc$groups <- rep(c('stim','control'),each = 1319)
# add celltype
pbmc$celltype <- Seurat::Idents(pbmc)
# load markergene
data("top3pbmc.markers")
# check
head(top3pbmc.markers,3)
# Seurat::DotPlot()
p <- DotPlot(object = pbmc, features = top3pbmc.markers$gene)
下面重新整理绘图的数据
# 提取并重新整理绘图数据
df<- p$data
exp_mat <- df %>%
dplyr::select(-pct.exp, -avg.exp) %>%
pivot_wider(names_from = id, values_from = avg.exp.scaled) %>%
as.data.frame()
row.names(exp_mat) <- exp_mat$features.plot
exp_mat <- exp_mat[,-1] %>% as.matrix()
percent_mat <- df %>%
dplyr::select(-avg.exp, -avg.exp.scaled) %>%
pivot_wider(names_from = id, values_from = pct.exp) %>%
as.data.frame()
row.names(percent_mat) <- percent_mat$features.plot
percent_mat <- percent_mat[,-1] %>% as.matrix()
# 填充色设置
col_fun = circlize::colorRamp2(c(-0.6, 0, 2), c("#424da7","#ffffff","#dd2b19"))
# 增加细胞百分比和细胞群注释
cluster <- colnames(exp_mat)
column_ha <- HeatmapAnnotation(
cluster = cluster,
col = list(cluster = setNames(brewer.pal(9, "Paired"), unique(cluster))
),
na_col = "grey"
)
# 图层设置
layer_fun = function(j, i, x, y, w, h, fill){
grid.rect(x = x, y = y, width = w, height = h,
gp = gpar(col = NA, fill = NA))
grid.circle(x=x,y=y,r= pindex(percent_mat, i, j)/100 * unit(2, "mm"),
gp = gpar(fill = col_fun(pindex(exp_mat, i, j)), col = NA))}
# 图例设置
lgd_list = list(
Legend( labels = c(0,0.25,0.5,0.75,1), title = "pt",
graphics = list(
function(x, y, w, h) grid.circle(x = x, y = y, r = 0 * unit(2, "mm"),
gp = gpar(fill = "black")),
function(x, y, w, h) grid.circle(x = x, y = y, r = 0.25 * unit(2, "mm"),
gp = gpar(fill = "black")),
function(x, y, w, h) grid.circle(x = x, y = y, r = 0.5 * unit(2, "mm"),
gp = gpar(fill = "black")),
function(x, y, w, h) grid.circle(x = x, y = y, r = 0.75 * unit(2, "mm"),
gp = gpar(fill = "black")),
function(x, y, w, h) grid.circle(x = x, y = y, r = 1 * unit(2, "mm"),
gp = gpar(fill = "black")))
))
上述设置好了之后,我们开始绘制图片,先看一下气泡热图
# 气泡热图
hp <- Heatmap(exp_mat,
name = "hp",
heatmap_legend_param = list(title = "expression"),
col = col_fun,
rect_gp = gpar(type = "none"),
layer_fun = layer_fun,
row_names_gp = gpar(fontsize = 5),
row_names_side = "left",
row_title = NULL,
row_km = 4,
column_names_rot = 45,
border = "black",
bottom_annotation = column_ha,
# 去掉行聚类树的虚线
show_parent_dend_line = F)
draw(hp, annotation_legend_list = lgd_list)
# 装饰行name
decorate_row_names("hp", {
grid.rect(gp = gpar(fill = "#FFA50080", col = "white"))
}, slice = 1)
decorate_row_names("hp", {
grid.rect(gp = gpar(fill = "#FF000040", col = "white"))
}, slice = 2)
decorate_row_names("hp", {
grid.rect(gp = gpar(fill = "#00FF0040", col = "white"))
}, slice = 3)
decorate_row_names("hp", {
grid.rect(gp = gpar(fill = "#0000FF40", col = "white"))
}, slice = 4)
大海哥还提供思路设计、定制生信分析、文献思路复现;有需要的小伙伴欢迎直接扫码咨询~
定制生信分析
服务器租赁
扫码咨询大海哥
往期回顾
01 |
02 |
03 |
04 |
https://mp.weixin.qq.com/s/tDPW1h9ELPJn8mtHkZ85bw