You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建外层为直方图的旭日图?肿瘤免疫细胞分布可视化

实现“外层直方图+旭日图”的方案

目前没有现成的函数可以直接生成你需要的这种组合图表,但可以用ggplot2通过图层叠加的方式快速实现,以下是针对你提供的DataFrame的具体步骤:

1. 准备工作与数据预处理

首先加载所需的R包,并对数据进行简单预处理,为CD8分组赋予层级数值,方便后续绘图:

library(tidyverse)

# 加载你的数据
df <- structure(list(slide = c("LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095"), stroma_bins = structure(c(1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), levels = c("0-10% Stroma", "10-20% Stroma", "20-30% Stroma", "30-40% Stroma", "40-50% Stroma", "50-60% Stroma", "60-70% Stroma", "70-80% Stroma", "80-90% Stroma", "90-100% Stroma"), class = "factor"), cd8_percent_bins = structure(c(1L, 1L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), levels = c("0-2% CD8+ Cells", "2-4% CD8+ Cells", "4-6% CD8+ Cells", "6-8% CD8+ Cells", "8-10% CD8+ Cells", "10-15% CD8+ Cells", "15-20% CD8+ Cells", ">20% CD8+ Cells"), class = "factor"), Freq = c(8L, 5L, 1L, 7L, 1L, 7L, 2L, 15L, 4L, 4L, 2L, 15L, 4L, 3L, 2L, 12L, 15L, 1L, 4L, 2L, 1L, 1L, 16L, 12L, 8L, 8L, 4L, 1L, 3L, 1L, 14L, 4L, 17L, 6L, 9L, 11L, 5L, 2L, 51L, 18L, 24L, 24L, 17L, 32L, 21L, 11L)), row.names = c(NA, -46L), class = c("data.table", "data.frame")) %>% 
  as_tibble()

# 为CD8分组添加层级数值,用于区分径向高度
df <- df %>%
  mutate(cd8_level = as.integer(cd8_percent_bins))

2. 绘制组合图表

利用ggplot2的极坐标转换,将柱状图转化为你需要的“旭日图+径向直方图”效果:

ggplot(df, aes(x = stroma_bins, y = Freq, fill = cd8_percent_bins)) +
  # 绘制径向直方图的柱子,按CD8分组堆叠
  geom_col(width = 1, position = position_stack()) +
  # 添加内层扇区的白色分隔(可选,增强旭日图的分区感)
  geom_col(aes(y = 0), width = 1, fill = "white", color = "black") +
  # 转换为极坐标,实现环形布局
  coord_polar(start = 0) +
  # 调整主题样式,简化视觉干扰
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 0, hjust = 1),
    axis.title = element_blank(),
    panel.grid = element_blank()
  ) +
  # 设置图例和标题
  labs(fill = "CD8+ 细胞占比", title = "肿瘤基质占比与CD8+细胞分布的径向旭日图")

3. 效果说明与调整

  • 这个图表中,每个环形扇区对应一个stroma_bins(基质占比区间),扇区内的不同高度柱子对应该基质区间下各CD8+细胞占比的频数(Freq),完全匹配你想要的效果。
  • 你可以根据需求灵活调整:比如修改geom_col的width参数调整扇区间隙,通过scale_fill_manual自定义颜色,添加geom_text标注数值,或者用scale_y_continuous限制径向范围。

内容的提问来源于stack exchange,提问作者fgootkind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:31:01