单细胞基因表达相关性散点图可视化
时长: 13 分钟
字数: 3.0k 字
更新: 2026-02-27
阅读: 0 次
环境配置
R
# 加载所需的R包
library(Seurat) # 用于单细胞数据分析
library(ggplot2) # 用于数据可视化
library(ggpubr) # 用于添加统计信息到图形
library(ggExtra) # 用于添加边缘分布图
library(dplyr) # 用于数据处理和管道操作数据读取与预处理
R
# 读取数据和预处理
seurat.obj <- readRDS("data/AY1739512568405/input.rds") # 读取Seurat对象
meta <- read.table("data/AY1739512568405/meta.tsv", header=T, sep="\t", row.names = 1) # 读取元数据
obj <- AddMetaData(seurat.obj, meta) # 将元数据添加到Seurat对象
DefaultAssay(obj) = "RNA" # 设置默认分析数据为RNA表达数据
head(obj@meta.data) # 查看元数据的前几行R
# 查看样本和细胞类型信息
unique(obj@meta.data$Sample) # 查看所有唯一的样本名
unique(obj@meta.data$celltype) # 查看所有唯一的细胞类型R
# 设置目标细胞类型和样本
target_celltypes <- c('T cell') # 指定目标细胞类型为T细胞
target_samples1 <- c('S133T') # 指定单个目标样本
# 创建子集对象
sub_obj1 <- subset(obj, subset = Sample %in% target_samples1 & celltype %in% target_celltypes) # 创建单样本T细胞子集
sub_obj2 <- subset(obj, subset = celltype %in% target_celltypes) # 创建所有样本T细胞子集
# 验证子集数据
unique(sub_obj1@meta.data$Sample) # 检查子集1的样本
unique(sub_obj1@meta.data$celltype) # 检查子集1的细胞类型
unique(sub_obj2@meta.data$Sample) # 检查子集2的样本
unique(sub_obj2@meta.data$celltype) # 检查子集2的细胞类型单样本相关性分析
R
# 提取单样本的基因表达数据
plot_data1 <- FetchData(sub_obj1,
vars = c("PPT1", "TRAF1"),
slot = "data") # 从标准化数据中提取PPT1和TRAF1的表达值
# 数据质量检查
print("数据概要:")
summary(plot_data1) # 显示数据的统计摘要
print("NA值数量:")
print(colSums(is.na(plot_data1))) # 检查NA值的数量
print("0值数量:")
print(colSums(plot_data1 == 0)) # 检查0值的数量
print("两个基因都为0的细胞数:")
sum(plot_data1$PPT1== 0 & plot_data1$TRAF1 == 0) # 检查双零表达的细胞数R
# 过滤数据
plot_data1_filtered <- plot_data1[plot_data1$PPT1 > 0 & plot_data1$TRAF1 > 0, ] # 筛选双基因表达细胞
print("筛选后的数据概要:")
summary(plot_data1_filtered) # 显示筛选后的统计摘要
print(paste0("筛选后的细胞数:", nrow(plot_data1_filtered))) # 显示筛选后的细胞数R
# 创建单样本相关性散点图
options(repr.plot.height=8, repr.plot.width=8) # 设置图形大小
p1 <- ggplot(plot_data1, aes(x = PPT1, y = TRAF1)) +
geom_point(size = 1, alpha = 0.5, color = "grey30") + # 添加散点
geom_smooth(method = "lm", color = "blue", se = TRUE, fill = "grey90") + # 添加拟合线
labs(x = "PPT1 expression", y = "TRAF1 expression") + # 设置轴标签
theme_bw() + # 设置主题
# 自定义主题细节
theme(
panel.grid.major = element_line(color = "grey90", linewidth = 0.3),
panel.grid.minor = element_line(color = "grey90", linewidth = 0.3),
panel.background = element_rect(fill = "white"),
panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5),
axis.ticks = element_line(color = "black", linewidth = 0.3),
axis.text = element_text(color = "black", size = 10),
axis.title = element_text(color = "black", size = 16),
axis.ticks.length = unit(0.2, "cm"),
plot.background = element_rect(fill = "white")
) +
# 设置x轴刻度
scale_x_continuous(
breaks = seq(0, max(plot_data1$PPT1), by = 1),
minor_breaks = seq(0, max(plot_data1$TRAF1), by = 0.5)
) +
# 设置y轴刻度
scale_y_continuous(
limits = c(0, max(plot_data1$TRAF1)),
breaks = seq(0, max(plot_data1$TRAF1), by = 1),
minor_breaks = seq(0, max(plot_data1$TRAF1), by = 0.5)
) +
# 添加相关性统计信息
stat_cor(method = "pearson",
label.x.npc = "left",
label.y.npc = "top",
size = 4)
# 添加边缘分布图
p1_with_marginal <- ggMarginal(p1,
type = "density", # 设置边缘图类型为密度图
margins = "both", # 同时显示x和y轴的边缘分布
size = 5,
xparams = list(fill = "orange", alpha = 1), # 设置x轴边缘图样式
yparams = list(fill = "blue", alpha = 1)) # 设置y轴边缘图样式
p1_with_marginal # 显示图形R
# 创建过滤后数据的散点图
options(repr.plot.height=8, repr.plot.width=8) # 设置图形大小
p1_filtered <- ggplot(plot_data1_filtered, aes(x = PPT1, y = TRAF1)) +
geom_point(size = 1, alpha = 0.5, color = "grey30") + # 添加散点
geom_smooth(method = "lm", color = "blue", se = TRUE, fill = "grey90") + # 添加拟合线和置信区间
labs(x = "PPT1 expression", y = "TRAF1 expression") + # 设置轴标签
# 设置主题和样式
theme_bw() +
theme(
panel.grid.major = element_line(color = "grey90", linewidth = 0.3), # 主网格线
panel.grid.minor = element_line(color = "grey90", linewidth = 0.3), # 次网格线
panel.background = element_rect(fill = "white"), # 背景颜色
panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5), # 边框
axis.ticks = element_line(color = "black", linewidth = 0.3), # 刻度线
axis.text = element_text(color = "black", size = 10), # 轴文本
axis.title = element_text(color = "black", size = 16), # 轴标题
axis.ticks.length = unit(0.2, "cm"), # 刻度线长度
plot.background = element_rect(fill = "white") # 图形背景
) +
# 设置x轴刻度
scale_x_continuous(
breaks = seq(0, max(plot_data1_filtered$PPT1), by = 1),
minor_breaks = seq(0, max(plot_data1_filtered$TRAF1), by = 0.5)
) +
# 设置y轴刻度
scale_y_continuous(
limits = c(0, max(plot_data1_filtered$TRAF1)),
breaks = seq(0, max(plot_data1_filtered$TRAF1), by = 1),
minor_breaks = seq(0, max(plot_data1_filtered$TRAF1), by = 0.5)
) +
# 添加相关性统计信息
stat_cor(method = "pearson",
label.x.npc = "left",
label.y.npc = "top",
size = 4)
# 为过滤后的散点图添加边缘分布图
p1_filtered_with_marginal <- ggMarginal(p1_filtered,
type = "density", # 设置边缘图类型为密度图
margins = "both", # 显示两个轴的边缘分布
size = 5,
xparams = list(fill = "orange", alpha = 1), # x轴边缘图样式
yparams = list(fill = "blue", alpha = 1)) # y轴边缘图样式
p1_filtered_with_marginal # 显示图形图片说明
这是一个基因表达相关性散点图,展示了 PPT1 和 TRAF1 这两个基因在细胞中表达水平的关系:
(1) 主图部分
- X轴:代表 PPT1 基因的表达水平
- Y轴:代表 TRAF1 基因的表达水平
- 点:每个灰色点代表一个细胞
- 拟合线:蓝色线是拟合线,表示两个基因表达的总体趋势
- 置信区间:浅灰色区域是拟合线的置信区间
(2) 图中的统计信息
- R = 0.72:表示相关系数,范围在 -1 到 1 之间。0.72 表示这两个基因有较强的正相关关系
- p = 2.5e-16:p 值极小,说明这种相关性具有统计学显著性
(3) 边缘分布图
- 上方橙色曲线:显示 PPT1 表达量的分布
- 右侧蓝色曲线:显示 TRAF1 表达量的分布
生物学意义 PPT1 和 TRAF1 的表达呈现显著的正相关,即当一个基因表达量增加时,另一个基因的表达量也倾向于增加。这种相关性可能暗示这两个基因在细胞中可能有功能上的联系或者受到类似的调控机制。
多样本相关性分析
R
# 提取多样本表达数据和分组信息
plot_data2 <- FetchData(sub_obj2,
vars = c("PPT1", "TRAF1", "Sample"), # 提取基因表达值和样本信息
slot = "data") # 使用标准化数据
# 数据质量检查
print("数据概要:")
summary(plot_data2) # 显示数据概要
print("NA值数量:")
print(colSums(is.na(plot_data2))) # 检查NA值
print("0值数量:")
print(colSums(plot_data2 == 0)) # 检查0值
print("两个基因都为0的细胞数:")
sum(plot_data2$PPT1== 0 & plot_data2$TRAF1 == 0) # 检查双零表达细胞R
# 过滤多样本数据
plot_data2_filtered <- plot_data2[plot_data2$PPT1 > 0 & plot_data2$TRAF1 > 0, ] # 筛选双基因表达细胞
print("筛选后的数据概要:")
summary(plot_data2_filtered) # 显示筛选后的统计摘要
print(paste0("筛选后的细胞数:", nrow(plot_data2_filtered))) # 显示筛选后的细胞数R
# 创建多样本散点图
options(repr.plot.height=8, repr.plot.width=8) # 设置图形大小
p2 <- ggplot(plot_data2, aes(x = PPT1, y = TRAF1, color = Sample)) + # 按样本着色
geom_point(size = 1.5, alpha = 0.8) + # 添加散点
labs(title = "PPT1 vs TRAF1 Correlation", # 设置标题
x = "PPT1 expression", # x轴标签
y = "TRAF1 expression") + # y轴标签
# 设置主题和样式
theme_bw() +
theme(
panel.grid.major = element_line(color = "grey90", linewidth = 0.3), # 主网格线
panel.grid.minor = element_line(color = "grey95", linewidth = 0.3), # 次网格线
panel.background = element_rect(fill = "white"), # 背景
panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5), # 边框
axis.ticks = element_line(color = "black", linewidth = 0.3), # 刻度线
axis.text = element_text(color = "black", size = 16), # 轴文本
axis.title = element_text(color = "black", size = 16), # 轴标题
legend.title = element_text(size = 16), # 图例标题
legend.text = element_text(size = 16), # 图例文本
legend.position = "right", # 图例位置
plot.title = element_text(size = 16, hjust = 0) # 图标题
) +
# 设置x轴刻度
scale_x_continuous(
breaks = seq(0, max(plot_data2$PPT1), by = 1),
minor_breaks = seq(0, max(plot_data2$TRAF1), by = 0.5)
) +
# 设置y轴刻度
scale_y_continuous(
limits = c(0, max(plot_data2$TRAF1)),
breaks = seq(0, max(plot_data2$TRAF1), by = 1),
minor_breaks = seq(0, max(plot_data2$TRAF1), by = 0.5)
)
p2 # 显示图形图片说明
这是一个多样本的基因表达相关性散点图,展示了不同样本中 PPT1 和 TRAF1 两个基因的表达关系(具体每个样本中两基因的相关性系数和 P 值可以从下面输出的 correlation_results.txt 中看到):
(1) 图的基本结构
- X轴:PPT1 基因的表达水平
- Y轴:TRAF1 基因的表达水平
- 图例:右侧图例,不同颜色代表不同的样本(S133A 到 S159T,以 "A" 结尾的为癌旁样本,以 "T" 结尾的为肿瘤样本)
- 点:每个点代表一个细胞
(2) 数据特征
- 可以看到多个样本中都存在 PPT1 和 TRAF1 的正相关趋势
- 在 X 轴和 Y 轴接近 0 的位置有较多的点,表示有些细胞这两个基因的表达量都很低
- 不同样本的相关性模式可能略有不同
生物学意义
- 可以比较不同样本(尤其是肿瘤和癌旁)中这两个基因的表达关系差异
- 有助于理解这两个基因在不同病理状态下的表达调控关系
- 可能发现潜在的样本特异性表达模式
R
# 计算每个样本的相关性统计
correlation_results <- plot_data2 %>%
group_by(Sample) %>% # 按样本分组
summarise(
Correlation = cor(PPT1, TRAF1, method = "pearson"), # 计算Pearson相关系数
P_value = cor.test(PPT1, TRAF1, method = "pearson")$p.value, # 计算p值
Sample_size = n() # 计算样本量
) %>%
mutate(
P_value = format(P_value, scientific = TRUE, digits = 3) # 格式化p值为科学计数法
)
# 保存相关性结果
write.table(correlation_results,
file = "Tcell_allsampes_PPT1_TRAF1_correlation_results.txt",
sep = "\t", # tab分隔
row.names = FALSE, # 不包含行名
quote = FALSE) # 不加引号R
# 创建过滤后的多样本散点图
options(repr.plot.height=8, repr.plot.width=8) # 设置图形大小
p2_filtered <- ggplot(plot_data2_filtered, aes(x = PPT1, y = TRAF1, color = Sample)) + # 按样本着色
geom_point(size = 1.5, alpha = 0.8) + # 添加散点
labs(title = "PPT1 vs TRAF1 Correlation", # 设置标题
x = "PPT1 expression", # x轴标签
y = "TRAF1 expression") + # y轴标签
# 设置主题和样式
theme_bw() +
theme(
panel.grid.major = element_line(color = "grey90", linewidth = 0.3), # 主网格线
panel.grid.minor = element_line(color = "grey95", linewidth = 0.3), # 次网格线
panel.background = element_rect(fill = "white"), # 背景
panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5), # 边框
axis.ticks = element_line(color = "black", linewidth = 0.3),# 刻度线
axis.text = element_text(color = "black", size = 16), # 轴文本
axis.title = element_text(color = "black", size = 16), # 轴标题
legend.title = element_text(size = 16), # 图例标题
legend.text = element_text(size = 16), # 图例样本
legend.position = "right", # 图例位置
plot.title = element_text(size = 16, hjust = 0) # 图标题
) +
# 设置x轴刻度
scale_x_continuous(
breaks = seq(0, max(plot_data2_filtered$PPT1), by = 1),
minor_breaks = seq(0, max(plot_data2_filtered$TRAF1), by = 0.5)
) +
# 设置y轴刻度
scale_y_continuous(
limits = c(0, max(plot_data2_filtered$TRAF1)),
breaks = seq(0, max(plot_data2_filtered$TRAF1), by = 1),
minor_breaks = seq(0, max(plot_data2_filtered$TRAF1), by = 0.5)
)
p2_filtered # 显示图形R
# 计算过滤后每个样本的相关性统计
correlation_results_filtered <- plot_data2_filtered %>%
group_by(Sample) %>% # 按样本分组
summarise(
Correlation = cor(PPT1, TRAF1, method = "pearson"), # 计算Pearson相关系数
# 计算p值
P_value = cor.test(PPT1, TRAF1, method = "pearson")$p.value, # 计算p值
Sample_size = n() # 计算样本量
) %>%
# 将数值转换为科学计数法
mutate(
P_value = format(P_value, scientific = TRUE, digits = 3)
)
# 保存相关性结果
write.table(correlation_results_filtered, # 格式化p值为科学计数法
file = "Tcell_allsampes_PPT1_TRAF1_filtered_correlation_results.txt",
sep = "\t", # 使用tab分隔
row.names = FALSE, # 不包含行名
quote = FALSE) # 不给字符串加引号结果保存
R
# 保存图形
ggsave("Tcell_S133T_PPT1_TRAF1_filtered_correlation_with_marginal.pdf",
plot = p1_filtered_with_marginal, width = 8, height = 8) # 保存单样本过滤后的图
ggsave("Tcell_allsamples_PPT1_TRAF1_correlation.pdf",
plot = p2, width = 8, height = 8) # 保存多样本原始图
ggsave("Tcell_allsamples_PPT1_TRAF1_filtered_correlation.pdf",
plot = p2_filtered, width = 8, height = 8) # 保存多样本过滤后的图