cellchat-气泡图美化

# 参考:https://mp.weixin.qq.com/s/YTO02blpdAi0PTI63TNtjA, ##高分杂志同款cellchat细胞通讯结果气泡图绘制
library(Seurat)
library(tidyverse)
library(CellChat)
library(NMF)
library(ggalluvial)
library(patchwork)
ptm = Sys.time()
library(ComplexHeatmap)
# rm(list = ls())
options(stringsAsFactors = FALSE)

setwd("./cellchat/epi.c8-macro.allsub")
# load 运行好的cellchat对象
cellchat_path <- "./cellchat/epi.c8-macro/"
load(paste0(cellchat_path,"cellchat.Control.RData"))
load(paste0(cellchat_path,"cellchat.Stress.RData"))
object.list <- list(Control = cellchat.Control, Stress = cellchat.Stress)
# object.list <- list(NL = cellchat.NL, LS = cellchat.LS)

cellchat <- mergeCellChat(object.list, add.names = names(object.list))
cellchat

# 查看celltype
levels(cellchat@idents$joint)
# plot1 不指定通路-原始bubble
p <- netVisual_bubble(cellchat, 
                      sources.use = 1, #修改
                      targets.use = c(2:11),  #修改
                      comparison = c(1, 2), 
                      max.dataset = 2,  # 取在 Stress 中表达高的
                      title.name = "Increased signaling in Stress",
                      angle.x = 45, remove.isolate = T)
ggsave("test1-Compare_LR_bubble-communication probabities-Increased.pdf", p, width = 8, height = 15)

# plot2 指定通路-原始bubble
df.net <- subsetCommunication(cellchat, thresh = 0.05)
head(df.net)

df.net$Stress$pathway_name%>%unique
pairLR.use <- extractEnrichedLR(cellchat, signaling = c("COLLAGEN"))
pairLR.use
p <- netVisual_bubble(cellchat, 
                      sources.use = 1, #修改
                      targets.use = c(2:11),  #修改
                      comparison = c(1, 2), 
                      max.dataset = 2,  # 取在 Stress 中表达高的
                      title.name = "Increased signaling in Stress",
                      angle.x = 90, 
                      pairLR.use = pairLR.use,
                      remove.isolate = F)
ggsave("test2-COLLAGEN-Compare_LR_bubble-communication probabities-Increased.pdf", p, width = 8, height =5)


# plot3 指定通路-美化bubble
p <- netVisual_bubble(cellchat, 
                      sources.use = 1, 
                      targets.use = c(4),
                      comparison = c(1, 2),
                      remove.isolate = F,
                      pairLR.use = pairLR.use,
                      grid.on=T,color.grid = "black")
# 提取数据
df.net <- p$data

# 安全生成 x/y 线条位置
x_len <- length(unique(df.net$source))
y_len <- length(unique(df.net$interaction_name_2))

x_lines <- if (x_len > 1) seq(1.5, x_len - 0.5, by = 1) else numeric(0)
y_lines <- if (y_len > 1) seq(1.5, y_len - 0.5, by = 1) else numeric(0)

p1 <- p +
  scale_size_continuous(range = c(4, 8), guide = "none") +
  scale_color_gradientn(
    colours = c("#2760a9", "white", "#e50f20"),
    values = scales::rescale(c(0.2, 0.35, 0.5)),
    name = "Commun. Prob."
  ) +
  xlab(NULL) + ylab(NULL) +
  geom_vline(xintercept = x_lines, lwd = 0.5) +
  geom_hline(yintercept = y_lines, lwd = 0.5) +
  theme(
    axis.title.x = element_text(size = 16),
    axis.title.y = element_text(size = 16),
    axis.text.x = element_text(size = 14),
    axis.text.y = element_text(size = 14, face = "italic"),
    panel.border = element_rect(color = "black", fill = NA, size = 1),
    legend.key.size = unit(0.6, 'cm'),
    legend.text = element_text(size = 12),
    legend.title = element_text(size = 13)
  )
ggsave("test3.cellchat_bubble_美化.pdf", plot = p1, width = 7.6, height = 9)
posted @ 2025-08-06 22:16  corrschi  阅读(40)  评论(0)    收藏  举报