简介
本文记录了:
- 1、箱型图的绘制,并进行显著性差异检验,一是两两比较指定组,二是两两比较所有组;
- 2、以及比较“某个变量”在两个分组间是否存在差异。
加载所需要的包
library(magrittr)
library(Seurat) %>% suppressMessages()
paste("Seurat", packageVersion("Seurat")) %>% print()
library(dplyr) %>% suppressMessages()
paste("dplyr", packageVersion("dplyr")) %>% print()
library(survival) %>% suppressMessages()
paste("survival", packageVersion("survival")) %>% print()
library(dittoSeq) %>% suppressMessages()
paste("dittoSeq", packageVersion("dittoSeq")) %>% print()
library(ggplot2) %>% suppressMessages()
paste("ggplot2", packageVersion("ggplot2")) %>% print()
library(ggpubr) %>% suppressMessages()
paste("ggpubr", packageVersion("ggpubr")) %>% print()
[1] "dplyr 1.1.4"
[1] "survival 3.8.3"
[1] "dittoSeq 1.20.0"
[1] "ggplot2 3.5.2"
[1] "ggpubr 0.6.0"
构建数据的格式
data <- data.frame("hcc_type" = mye$HCC_type,
"cell_clusters" = mye$identify_myeloid,
"risk_score" = mye$risk_score)
head(data)
hcc_type cell_clusters risk_score
P01_T_0014 R Macro AZGP1 0.34462416
P01_T_0121 R Macro AZGP1 0.82306410
P01_T_0159 R Macro AZGP1 1.37164070
P01_T_0167 R Macro MT1X 0.12383450
P01_T_0172 R Macro CXCL3 0.13059335
P01_T_0187 R Macro HAVCR1 0.03061694
一、两两比较指定组
my_comparisons <- list(c("Macro AZGP1", "Macro MT1X"), c("Macro AZGP1", "Macro CXCL3"))
ggboxplot(data, x = "cell_clusters", y = "risk_score", fill = "cell_clusters",
ylab = "log(TPM + 1)",
xlab = "",
legend.title = "",
palette = dittoColors(),
width = 0.6
)+
NoLegend()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
ggtitle("risk scores")+theme(plot.title = element_text(hjust = 0.5))+
stat_compare_means(comparisons = my_comparisons,
method = "wilcox.test",
symnum.args = list(cutpoints = c(0, 0.001, 0.01, 0.05, 1),
symbols = c("***", "**", "*", "ns")
),
label = "p.signif",
hide.ns = TRUE
)
注:my_comparisons是设置要比较的两组。

二、两两比较所有组
data$cell_clusters <- as.factor(data$cell_clusters)
all_pairs <- combn(levels(data$cell_clusters), 2, simplify = FALSE)
ggboxplot(data, x = "cell_clusters", y = "risk_score", fill = "cell_clusters",
ylab = "log(TPM + 1)",
xlab = "",
legend.title = "",
palette = dittoColors(),
width = 0.6)+
NoLegend()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
ggtitle("risk scores")+theme(plot.title = element_text(hjust = 0.5))+
stat_compare_means(comparisons = all_pairs,
method = "wilcox.test",
symnum.args = list(cutpoints = c(0, 0.001, 0.01, 0.05, 1),
symbols = c("***", "**", "*", "ns")
),
label = "p.signif",
hide.ns = TRUE
)
注:要将要比较的那个变量设置为因子。

三、比较“某个变量”在两个分组间是否存在差异。
ggboxplot(data, x="cell_clusters", y="risk_score", fill = "hcc_type",
ylab="log(TPM + 1)",
xlab="",
legend.title="HCC type",
palette = c("#3C4A93","#C7263B"),
width = 0.6
)+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
ggtitle("risk score") + theme(plot.title = element_text(hjust = 0.5))+
stat_compare_means(aes(group = hcc_type),
method = "wilcox.test",
symnum.args = list(cutpoints = c(0, 0.001, 0.01, 0.05, 1),
symbols = c("***", "**", "*", "ns")
),
label = "p.signif"
)

附录
代码:
https://github.com/wPencil/MyNotes/blob/9b354894c20a59a9e94d64d28337020b01fe3e75/R/BoxPlot