R可视化之美化山脊图

2022-03-14 17:45:08 浏览数 (2)

基因集可视化是很常见的分析内容,山脊图使用较少,原因可能是默认生成的图片不美观。本文提供山脊图的美化后的效果图,供选择。

1、加载R包

代码语言:javascript复制
rm(list=ls())
library(Seurat)
library(purrr)
library(dplyr)
library(ggplot2)
library(cowplot)
library(ggridges)
library(RColorBrewer)
library(viridis)

2、读取Seurat object,生成可视化基因集

代码语言:javascript复制
sce <- readRDS('F:/R_Language/R_Practice/scRNA_Seq_column/src/scRNA-seq_advance/Data/sce.rds')

seurat_object <- sce
# 可视化基因集来源有三种:初始矩阵所在行、高可变基因、各细胞类型间差异表达基因。
# 第三种基因集表达量高且差异较大,可视化的山脊图较为美观。
# gene_set <- rownames(seurat_object@assays$RNA)
# gene_set <- VariableFeatures(seurat_object)

DefaultAssay(seurat_object) <- 'RNA'
data.markers <- FindAllMarkers(seurat_object, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25, verbose = F)
gene_set <- unique(data.markers$gene)
length(gene_set)
## [1] 1768

3、山脊图可视化

3.1 Seurat内置函数绘制ridgeplot

代码语言:javascript复制
p <- RidgePlot(seurat_object, features = gene_set[1], fill.by = 'feature')
p

3.2 美化默认ridgeplot:对基因表达量着色

代码语言:javascript复制
# Color by gene expression level
p$data %>% ggplot(aes_string(x = gene_set[1], y = 'ident'))  
  geom_density_ridges_gradient(aes(fill=stat(x)), scale = 2, rel_min_height = 0.00, size = 0.3)  
  scale_fill_gradientn(name = "Gene Expression Level", colours = colorRampPalette(rev(brewer.pal(11,'Spectral')))(32))  
  theme_classic()   labs(caption = '核密度估计峰峦图', x='', y='', title = gene_set[1])  
  theme(plot.caption = element_text(hjust=0.5, size=rel(1.2))) 

3.3 美化默认ridgeplot:对表达的细胞数目着色

代码语言:javascript复制
# Fill by expressed cells level of gene
p$data %>% ggplot(aes_string(x = gene_set[1], y = 'ident'))  
  geom_density_ridges_gradient(aes(fill = ..density..), scale = 2, rel_min_height = 0.00, size = 0.3)  
  scale_fill_gradientn(name = "Expressed cells Level", colours = colorRampPalette(rev(brewer.pal(11,'Spectral')))(32))  
  theme_classic()   labs(caption = '核密度估计峰峦图', x='', y='', title = gene_set[1])  
  theme(plot.caption = element_text(hjust=0.5, size=rel(1.2))) 

3.4 美化默认ridgeplot:对表达的细胞数目着色(第二种配色方案)

代码语言:javascript复制
# Set other colors
p$data %>% ggplot(aes_string(x = gene_set[1], y = 'ident'))  
  geom_density_ridges_gradient(aes(fill = ..density..), scale = 2, rel_min_height = 0.00, size = 0.3)  
  scale_fill_viridis(name = "Expressed cells Level", option = "C")  
  theme_classic()   labs(caption = '核密度估计峰峦图', x='', y='', title = gene_set[1])  
  theme(plot.caption = element_text(hjust=0.5, size=rel(1.2))) 

3.5 对基因集批量美化着色

代码语言:javascript复制
# Batch
beautifulRidgePlot <- function(gene_set){
  p <- RidgePlot(seurat_object, features = gene_set, fill.by = 'feature')
  p$data %>% ggplot(aes_string(x = gene_set, y = 'ident'))  
    geom_density_ridges_gradient(aes(fill = ..density..), scale = 2, rel_min_height = 0.00, size = 0.3)  
    scale_fill_gradientn(name = "Expressed cells Level", colours = colorRampPalette(rev(brewer.pal(11,'Spectral')))(32))  
    theme_classic()   labs(x='', y='', title = gene_set)   theme(legend.position = "top")
}

ridge.plot <- plot_grid(plotlist=purrr::map(gene_set[1:2], beautifulRidgePlot))
ridge.plot

0 人点赞