1. 获得免疫浸润细胞组分的数据

如果是TCGA的数据,可以从TCGA网站获得计算好的免疫浸润细胞分数的数据,https://gdc.cancer.gov/about-data/publications/panimmune。
或者从cibersort网站利用自己整理好的数据进行计算,得出CIBERSORTx_Results。免疫细胞的名称可以调整也可以不用修改,检索出来的文章两种方式都有。

2. 数据整理

获得样本免疫细胞浸润分数的数据之后,下一步就是要根据自己的需要分组,然后再比较分组之间的差异。这里使用的是根据COX2基因log2TPM数据的中位数分组,分为高表达和低表达组。然后进行绘图。

1
2
3
4
5
6
7
8
9
10
11
12
rm(list = ls())
library(tidyverse)
load("log2_tpm_data.Rdata")#
cell_prop <- read_csv("CIBERSORTx_Results.csv")

#read_csv不会使列名的-变为.,但是得出的数据是tibble,如果需要改行名,还需要转换为data.frame

cell_prop$group <- ifelse(as.numeric(log2_tpm_data["COX2",])>median(as.numeric(log2_tpm_data["COX2",])), "high","low")#添加分组
cell_prop <- as.data.frame(cell_prop)
rownames(cell_prop) <- str_sub(cell_prop$Mixture, 1,12)
cell_prop <- cell_prop[,-1]

3. 绘图

3.1热图

1
2
3
4
5
6
7
8
9
10
11
12
#热图
library(pheatmap)
library(RColorBrewer)
png(file="heatmap.png")
annotation_col = data.frame(
CellType = cell_prop$group
)
rownames(annotation_col) = rownames(cell_prop)
pheatmap(t(cell_prop[,1:22]),
annotation_col = annotation_col
)
dev.off()

upload successful

具体的细节可以根据pheatmap包进行调整。

3.2 基于ggplot2的其他绘图

先将数据整理成ggplot适用的数据形式,这里用到了pivot_longer()函数,将免疫浸润细胞和比例整理成两列。

1
2
3
4
5
6
dd1 <- cell_prop %>% 
as.data.frame() %>%
rownames_to_column("sample") %>%
pivot_longer(cols=2:23,
names_to= "celltype",
values_to = "Proportion")
1
2
3
4
5
library(ggplot2)
ggplot(dt1,aes(sample,Proportion,fill = celltype)) +
geom_bar(position = "stack",stat = "identity")+
theme_bw()+
guides(fill=guide_legend(ncol=1))

upload successful

3.3 各个细胞类型比例的箱式图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
library(ggpubr)
# boxplot
ggboxplot(
dt1,
x = "celltype",
y = "Proportion",
color = "black",
fill = "celltype",
xlab = "",
ylab = "Cell composition",
main = "TME Cell composition"
) +
theme(axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 1
))

upload successful

3.4 根据group分组的箱式图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ggboxplot(
dt1,
x = "celltype",
y = "Proportion",
color = "black",
fill = "group",
xlab = "",
ylab = "Cell composition",
main = "TME Cell composition group by COX2 expression"
) +
stat_compare_means(
aes(group = group),
label = "p.signif",
method = "wilcox.test",
hide.ns = T,
size = 4.5
) +
theme(axis.text.x = element_text(
angle = 45,
hjust = 1,
vjust = 1
))

upload successful

3.5 将每种细胞组分分开进行绘图,采用分面的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gplot(data = dt1, aes(x = group, y = Proportion, fill = group)) +
geom_boxplot()+
stat_compare_means(aes(label = paste0("p = ", ..p.format..)), label.x = 1.35, vjust = 0.8, size = 4.5) +
guides(fill = FALSE)+
labs(x = "",
y = "Cell composition fraction")+
facet_wrap("celltype", scales = "free", nrow = 3)+
theme(axis.title=element_text(face = 'bold',size=12),
axis.text = element_text(size = 12, colour = 'black'),
axis.ticks.length=unit(.2, "cm"),
axis.ticks = element_line(colour = "black"),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black",size = 0, fill=NA),
plot.margin = margin(1, 1, 1, 1, "cm"))

upload successful

有的免疫细胞的名称显示不全,可以通过保存成不同分辨率的tif格式的图来改变字体的相对大小,也可以在theme里面添加strip.text语句设置字体大小

1
strip.text = element_text(size = 10, face = "bold")

3.6 将每种细胞组分分开进行绘图,采用批量单个绘图的方法的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
for (i in unique(dt1$celltype)){
dt = filter(dt1,dt1$celltype == i)
p = ggplot(data = dt, aes(x = group, y = Proportion, fill = group)) +
geom_boxplot()+
guides(fill = FALSE)+
stat_compare_means(aes(label = paste0("p = ", ..p.format..)), label.x = 1.35, vjust = 0.8, size = 4.5) +
labs(title = i,
x = "",
y = "Cell composition fraction")+
theme(axis.title=element_text(face = 'bold',size=12),
axis.text = element_text(size = 12, colour = 'black'),
axis.ticks.length=unit(.2, "cm"),
axis.ticks = element_line(colour = "black"),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black",size = 0, fill=NA),
plot.margin = margin(1, 1, 1, 1, "cm"))
f = paste0(i, ".tif")
png(filename = f, width = 320, height = 500, res = 100)
print(p)
dev.off()
}

upload successful

然后可以使用cowplot包将需要的图整合起来。