ggplot2 | 单细胞类间比较的火山图 - 经典效果

1.定义画图函数

定义函数。

VolcanoPlot=function(dif, log2FC=log2(1.5), padj=0.05, 
                 label.symbols=NULL, label.max=30,
                 cols=c("#497aa2", "#ae3137"), title=""){
  if( all( !c("log2FoldChange", "padj", "symbol") %in% colnames(dif) )){
    stop("Colnames must include: log2FoldChange, padj, symbol")
  }
  rownames(dif)=dif$symbol
  
  # (1) define up and down
  dif$threshold="ns";
  dif[which(dif$log2FoldChange > log2FC & dif$padj <padj),]$threshold="up";
  dif[which(dif$log2FoldChange < (-log2FC) & dif$padj < padj),]$threshold="down";
  dif$threshold=factor(dif$threshold, levels=c('down','ns','up'))
  #head(dif)
  #
  tb2=table(dif$threshold); print(tb2)
  library(ggplot2)
  # (2) plot
  g1 = ggplot(data=dif, aes(x=log2FoldChange, y=-log10(padj), color=threshold)) +
    geom_point(alpha=0.8, size=0.8) +
    geom_vline(xintercept = c(-log2FC, log2FC), linetype=2, color="grey")+
    geom_hline(yintercept = -log10(padj), linetype=2, color="grey")+
    labs(title= ifelse(""==title, "", paste("DEG:", title)))+
    xlab(bquote(Log[2]*FoldChange))+
    ylab(bquote(-Log[10]*italic(P.adj)) )+
    theme_classic(base_size = 14) +
    theme(legend.box = "horizontal",
          legend.position="top",
          legend.spacing.x = unit(0, 'pt'),
          legend.text = element_text( margin = margin(r = 20) ),
          legend.margin=margin(b= -10, unit = "pt"),
          plot.title = element_text(hjust = 0.5, size=10)
          ) +
    scale_color_manual('',labels=c(paste0("down(",tb2[[1]],')'),'ns',
                                   paste0("up(",tb2[[3]],')' )),
                       values=c(cols[1], "grey", cols[2]) )+
    guides(color=guide_legend(override.aes = list(size=3, alpha=1))); g1;
  # (3)label genes
  if(is.null(label.symbols)){
    dif.sig=dif[which(dif$threshold != "ns" ), ]
    len=nrow(dif.sig)
    if(len<label.max){
      label.symbols=rownames(dif.sig)
    }else{
      dif.sig=dif.sig[order(dif.sig$log2FoldChange), ]
      dif.sig= rbind(dif.sig[1:(label.max/2),], dif.sig[(len-label.max/2):len,])
      label.symbols=rownames(dif.sig)
    }
  }
  dd_text = dif[label.symbols, ]
  print((dd_text))
  # add text
  library(ggrepel)
  g1 + geom_text_repel(data=dd_text,
                       aes(x=log2FoldChange, y=-log10(padj), label=row.names(dd_text)),
                         #size=2.5, 
                         colour="black",alpha=1)
}

2.获得差异基因

直接使用Seurat 4 的函数比较两个类,获得DEG:

deg_all=FindMarkers(scObj, ident.1 = "DSS", ident.2 = "WT", group.by="origin", min.pct = 0.001)
dim(deg_all) #2137    5
head(deg_all)

3.构建中间数据 dif

dif=data.frame(
  symbol=rownames(deg_all),
  log2FoldChange=deg_all$avg_log2FC,
  padj=deg_all$p_val_adj
)

4.画图

# 可以指定要标记的DEG数量,选出FC最大和最小的基因标记
VolcanoPlot(dif, padj=0.05, title="DSS vs WT", label.max = 50)
# 自定义颜色
VolcanoPlot(dif, padj=0.05, title="DSS vs WT", label.max = 50, cols=c("blue", "red"))


# 也可以指定要标记的基因名字
VolcanoPlot(dif, padj=1e-10, title="DSS vs WT -2", 
            label.symbols=dif[ ((abs(dif$log2FoldChange) > 2) & (dif$padj < 1e-50) ) | 
                                      abs(dif$log2FoldChange) > 4,]$symbol )

在这里插入图片描述

在这里插入图片描述

请关注 生物慕课 查看更多教程。

### 多组单细胞转录组数据分析方法与工具 #### 数据预处理 对于多组单细胞RNA测序数据,通常需要先进行质量控制和标准化处理。这一步骤可以去除低质量的细胞以及标准化基因表达矩阵,以便后续分析能够更加准确可靠[^1]。 ```r library(Seurat) # 质量控制参数设定 qc_params <- list(min_features = 200, max_features = 6000, min_cells = 3) # 创建Seurat对象并应用QC过滤 sce_list <- lapply(files, CreateSeuratObject, project = "MultiGroup", do.fastpca = TRUE) for(i in seq_along(sce_list)){ sce_list[[i]] <- subset(x = sce_list[[i]], subset = nFeature_RNA >= qc_params$min_features & nFeature_RNA <= qc_params$max_features & percent.mt < 5) } ``` #### 细胞聚与标注 为了识别不同样本的共有模式或特异性特征,在完成预处理之后会采用无监督学习算法来进行细胞型的分。常用的技术包括但不限于层次聚、k-means聚和支持向量机等。此外,还可以利用已知标记基因来辅助确认特定型的细胞群体。 ```r # 合并多个Seurat对象成一个整体用于联合分析 combined_sce <- merge(x = sce_list[[1]], y = sce_list[-1], add.cell.ids = names(sce_list)) # 进行PCA降维及UMAP可视化 DefaultAssay(combined_sce) <- "RNA" combined_sce <- ScaleData(object = combined_sce) combined_sce <- RunPCA(object = combined_sce, npcs = 30) combined_sce <- RunUMAP(object = combined_sce, reduction = "pca", dims = 1:30) # 使用FindClusters函数执行基于论的社区检测算法实现自动化的细胞分群 combined_sce <- FindNeighbors(object = combined_sce, reduction = "umap", k.param = 20) combined_sce <- FindClusters(object = combined_sce, resolution = 0.8) DimPlot(combined_sce, group.by = 'seurat_clusters') ``` #### 差异表达分析 当完成了初步的数据探索后,则可以通过统计测试找出各条件存在显著差异变化的目标分子。例如,通过`FindMarkers()`命令比较两个指定簇之的上调/下调情况;也可以借助外部包如DESeq2来进行更复杂的实验设计下的差异检验。 ```r # 寻找每个cluster相对于其他clusters中的marker genes markers <- FindAllMarkers(combined_sce, only.pos=TRUE, min.pct=0.25, logfc.threshold=0.25) # 利用ggplot绘制火山展示logFC vs p-value关系 volcano_data <- markers %>% mutate(significant = ifelse(p_val_adj<0.05,"Significant","Not Significant")) ggplot(volcano_data,aes(x=log2FoldChange,y=-log10(p_val))) + geom_point(aes(color=significant))+ theme_bw() ``` #### 功能富集分析 最后,针对筛选出来的候选列表进一步开展生物学意义解读工作,比如GO term enrichment 或 KEGG pathway mapping 等操作可以帮助理解这些改变背后潜在机制所在[^2]。 ```bash # 安装gprofiler2 R package 并调用其API接口获取功能注释信息 install.packages('remotes') remotes::install_github("aghozlane/gProfiler") library(gprofiler2) res <- gprofileR(query=list(marker_genes), organism="hsapiens") print(res) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值