基因集可视化是很常见的分析内容,山脊图使用较少,原因可能是默认生成的图片不美观。本文提供山脊图的美化后的效果图,供选择。
rm(list=ls())
library(Seurat)
library(purrr)
library(dplyr)
library(ggplot2)
library(cowplot)
library(ggridges)
library(RColorBrewer)
library(viridis)
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
p <- RidgePlot(seurat_object, features = gene_set[1], fill.by = 'feature')
p

# 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)))

# 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)))

# 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)))

# 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
