繪制單細(xì)胞分析組織中細(xì)胞數(shù)量和細(xì)胞比例柱狀圖
爾云間? 一個專門做科研的團(tuán)隊
? ?

今天繪制一下單細(xì)胞分析結(jié)果組織中細(xì)胞數(shù)量和細(xì)胞比例柱狀圖:
1.?安裝需要的R包
install.packages(“Seurat”)
install.packages(“patchwork”)
install.packages(“reshape2”)
install.packages(“RcolorBrewer”)
install.packages(“scales”)
install.packages(“ggplot2”)
install.packages(“tidyverse”)
2.?導(dǎo)入所需要的R包
library(Seurat)
library(patchwork)
library(reshape2)
library(RcolorBrewer)
library(ggplot2)
library(tidyverse)
library(scales)
3.?代碼展示
數(shù)據(jù)下載
wget?https://cf.10xgenomics.com/samples/cell/pbmc3k/pbmc3k_filtered_gene_bc_matrices.tar.gz
tar -zxvf pbmc3k_filtered_gene_bc_matrices.tar.gz
#分析所需要的文件三個在filtered_gene_bc_matrices/hg19/

pbmc.data <- Read10X(data.dir = "filtered_gene_bc_matrices/hg19/")
# Initialize the Seurat object with the raw (non-normalized data).
pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200)
# Normalizing the data
pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)
#Identification of highly variable features
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
all.genes <- rownames(pbmc)
pbmc <- ScaleData(pbmc, features = all.genes)
#Run non-linear dimensional reduction (UMAP/tSNE)
pbmc <- RunUMAP(pbmc, dims = 1:10)
#開始繪圖
colourCount = length(unique(pbmc@meta.data$celltype))
getPalette = colorRampPalette(brewer.pal(9, "Set1"))
celltype_colors <- getPalette(colourCount)
#提取樣本和細(xì)胞數(shù)據(jù),并且進(jìn)行長寬數(shù)據(jù)轉(zhuǎn)換
plotC <- table(pbmc@meta.data$sample, pbmc@meta.data$celltype) %>% melt()
colnames(plotC) <- c("Sample", "CellType","Number")
#繪制每個組織中細(xì)胞數(shù)目柱狀圖
pC1 <- ggplot(data = data_plotC, aes(x = Sample, y = Number, fill = CellType)) +
? geom_bar(stat = "identity", width=0.8,aes(group=CellType),position="stack")+
? scale_fill_manual(values=celltype_colors) +
? theme_bw()+
? theme(panel.grid =element_blank()) +
? labs(x="",y="Average number")+
? theme(axis.text = element_text(size=12, colour = "black"))+
? theme(axis.title.y = element_text(size=12, colour = "black"))+
? theme(panel.border = element_rect(size = 1, linetype = "solid", colour = "black"))+
? theme(axis.text.x = element_text(angle = 45,hjust = 0.8,? vjust = 0.6))
#繪制每個組織中細(xì)胞比例柱狀圖
pC2 <- ggplot(data = plotC, aes(x = Sample, y = Number, fill = CellType)) +
? geom_bar(stat = "identity", width=0.8,aes(group=CellType),position="fill")+
? scale_fill_manual(values=celltype_colors) +
? theme_bw()+
? theme(panel.grid =element_blank()) +
? labs(x="",y="Cell proportion")+
? scale_y_continuous(labels = percent)+? ####用來將y軸移動位置
? theme(axis.text = element_text(size=12, colour = "black"))+
? theme(axis.title.y = element_text(size=12, colour = "black"))+
? theme(panel.border = element_rect(size = 1, linetype = "solid", colour = "black"))+
? theme(axis.text.x = element_text(angle = 45,hjust = 0.8,? vjust = 0.6))#讓橫軸上的標(biāo)簽傾斜45度
#兩個圖片進(jìn)行拼圖
pC <- pC1 + pC2 + plot_layout(ncol = 2, widths = c(1,1),guides = 'collect')
#保存圖片
ggsave(pC,file="plotC.pdf",width = 7, height = 5)

推薦閱讀
關(guān)注小果,小果將會持續(xù)為你帶來更多生信干貨哦。

生信果
生信入門、R語言、生信圖解讀與繪制、軟件操作、代碼復(fù)現(xiàn)、生信硬核知識技能、服務(wù)器等