pythonic生物人

复现Nature cancer期刊图,换数据可用!

看到Nature cancer上好看的配图(b)

Image

以及(a、b)

Image

使用R复现下,效果图,

Image

该图展示人cDC和经典单核细胞在转移灶(左与健康组织(右) 中的频率,基于scRNA‑seq泛癌髓系APC图谱:

  • 小提琴的宽度代表该癌种中比例分布的密度
  • 箱线图展示中位数(中心线)和四分位距(IQR)
  • 散点代表单个患者样本
  • 同时每1组交替使用背景色

R语言复现

ggplot(df, aes(x = Tissue, y = Proportion, fill = CellType))+
# 交替背景色
for (i in seq_len(n_tissues)) {
    bg_col <- ifelse(i %% 2 == 1, BG_CREAM, "white")
    p <- p + annotate("rect",
      xmin = i - 0.5, xmax = i + 0.5,
      ymin = -Inf, ymax = Inf,
      fill = bg_col, color = NA
    )
  } +
# 小提琴图层
    geom_violin(
      aes(color = CellType),
      position = position_dodge(width = dodge_width),
      scale = "width",
      width = 0.8,
      linewidth = 0.3,
      alpha = 0.4,
      trim = TRUE
    ) +

# 抖动散点 (数据点)
    geom_jitter(
      position = position_jitterdodge(
        jitter.width = 0.18,
        jitter.height = 0,
        dodge.width = dodge_width,
        seed = 42
      ),
        aes(fill = CellType),
      size = 1.5,
      alpha = 0.90,
      shape = 21,
      color = "black",
      show.legend = FALSE
    ) +

# 中位数横线
    stat_summary(
      aes(group = CellType),
      fun = median,
      geom = "crossbar",
      position = position_dodge(width = dodge_width),
      width = 0.45,
      linewidth = 0.45,
      color = MED_COLOR,
      fill = NA,
      show.legend = FALSE
    ) +
   theme_custom
Image

值得学习的点:

  • annotate交替添加背景色
annotate("rect",
      xmin = i - 0.5, xmax = i + 0.5,
      ymin = -Inf, ymax = Inf,
      fill = bg_col, color = NA
    )
Image
  • geom_violin添加小提琴图层
# 小提琴图层
    geom_violin(
      aes(color = CellType),
      position = position_dodge(width = dodge_width),
      scale = "width",
      width = 0.8,
      linewidth = 0.3,
      alpha = 0.4,
      trim = TRUE
    )
Image
  • geom_jitter添加抖动散点图层
# 抖动散点 (数据点)
    geom_jitter(
      position = position_jitterdodge(
        jitter.width = 0.18,
        jitter.height = 0,
        dodge.width = dodge_width,
        seed = 42
      ),
        aes(fill = CellType),
      size = 1.5,
      alpha = 0.90,
      shape = 21,
      color = "black",
      show.legend = FALSE
    ) 
Image

关于抖动散点图的更多妙用看看之前的案例

复现Nature子刊上3张图,换数据即可用!

Image

复现IF50.0期刊上6张图,换数据即可用!

Image

一起学习~