设置好gse_id和工作目录,基本全自动
load(".\\table\\CIBERSORT_Results_fromRcode.RData")
load(".\\data\\pdata_paq.RData")
re <- re <- TCGA_TME.results %>% data.frame() %>% select(-c("P.value","Correlation","RMSE"))
group_info <- pdata
library(pheatmap)
k <- apply(re,2,function(x) {sum(x == 0) < nrow(re)/2})
re2 <- as.data.frame(t(re[,k]))
#re[1:10,]
an = data.frame(group = group_info$group,
row.names = rownames(group_info))
table(group_info$group)
heatmap <- pheatmap(re2,scale = "row",
show_colnames = F,
cluster_cols = F,
annotation_col = an,
color = colorRampPalette(c("navy", "white", "firebrick3"))(50),
fontsize = 8)
ggsave(heatmap,filename = ".\\plots\\cibersort_heatmap.pdf",
width = 89,height = 89,units = "mm")
#--------------------------------------------直方图----------------------------------------------
#可以展示出每个样本的免疫细胞比例
#色板无需修改
library(RColorBrewer)
mypalette <- colorRampPalette(brewer.pal(8,"Set1"))
#宽格式变长格式
dat <- re %>% as.data.frame() %>%
rownames_to_column("Sample") %>%
gather(key = Cell_type,value = Proportion,-Sample)
cibersort_hist <- ggplot(dat,aes(Sample,Proportion,fill = Cell_type)) +
geom_bar(stat = "identity") +
labs(fill = "Cell Type",x = "",y = "Estiamted Proportion") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90,size = 10,vjust = 0.5),
#axis.ticks.x = element_blank(),
axis.text = element_text(size = 10),
legend.position = "right",
legend.justification = c(1,1), #不知道是撒意思
legend.key.size = unit(0.3,"cm"),
legend.text = element_text(size = 10),
) +
guides(fill = guide_legend(ncol = 1))+ #把图例放在一列
scale_y_continuous(expand = c(0.01,0)) +
scale_fill_manual(values = mypalette(22))
ggsave(".\\plots\\cibersort_histplot.pdf",cibersort_hist,width = 120,height = 89,units = "mm")
##------------------------------排序箱线图---------------------------------
a = dat %>%
group_by(Cell_type) %>%
summarise(m = median(Proportion)) %>%
arrange(desc(m)) %>%
pull(Cell_type)
dat$Cell_type = factor(dat$Cell_type,levels = a)
ggplot(dat,aes(Cell_type,Proportion,fill = Cell_type)) +
geom_boxplot(outlier.shape = 21,color = "black") +
theme_bw() +
labs(x = "Cell Type", y = "Estimated Proportion") +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.position = "bottom") +
scale_fill_manual(values = mypalette(22))
##-----------------------------两组对比箱线图------------------------------------
dat$Group = c(rep(group_info$group,22)) #22,是因为有22种免疫细胞
dat %>% dim()
library(ggpubr)
cibersort_compare_boxplot <- ggplot(dat,aes(Cell_type,Proportion,fill = Group)) +
geom_boxplot(outlier.shape = 21,color = "black") +
theme_bw() +
labs(x = "Cell Type", y = "Estimated Proportion") +
theme(legend.position = "top") +
theme(axis.text.x = element_text(angle=80,vjust = 0.5))+
scale_fill_manual(values = mypalette(22)[c(6,1)])+ stat_compare_means(aes(group = Group),
label = "p.signif",
method = "wilcox.test",
hide.ns = T)
ggsave("cibersort_compare_boxplot.pdf",cibersort_compare_boxplot,width = 17.5,height = 12,units = "cm")