如何在ggplot自定义太阳burst图的geom_rect中手动设置矩形颜色
问题描述
我想在ggplot的修改版太阳burst图中,为geom_rect的每个矩形手动设置特定颜色。目前只能用颜色渐变,但这个渐变在不同样本数据集之间无法匹配。
当前代码:
ggplot() + scale_x_continuous(name="x") + scale_y_continuous(name="y") + geom_rect(data=stroma_comp, mapping=aes(xmin=lower_lim, xmax=upper_lim, ymin=ymin,ymax=ymax, fill = stroma_gradient), color='black', alpha=0.5) + geom_col(data = plot_sample, mapping = aes(x = row_id, y = cd8_density_total)) + annotate("text", x = 1, y = -.004, label = sample_name, size = 7, color = "black") + geom_textpath(data = stroma_comp, mapping = aes(x=lower_lim + (upper_lim-lower_lim)/2, y = ymin/2, label = stroma_bins), angle = 90, size = 2, color = "white") + theme_void() + scale_fill_discrete(guide = "none") + scale_color_discrete(guide = "none") + coord_polar()
当前输出效果:基于渐变配色的太阳burst图,颜色随stroma_gradient变量变化,但不同样本间配色无法统一。
期望的配色方案:
| Bin | 十六进制颜色 |
|---|---|
| 0-10% Stroma | 00ffff |
| 10-20% Stroma | 2efce7 |
| 20-30% Stroma | 50f8cd |
| 30-40% Stroma | 6df3b3 |
| 40-50% Stroma | 87ed99 |
| 50-60% Stroma | 9fe681 |
| 60-70% Stroma | b6de6b |
| 70-80% Stroma | cbd559 |
| 80-90% Stroma | e0ca4d |
| 90-100% Stroma | f3be47 |
首次尝试匹配颜色的代码:
ggplot() + scale_x_continuous(name="x") + scale_y_continuous(name="y") + geom_rect(data=stroma_comp, mapping=aes(xmin=lower_lim, xmax=upper_lim, ymin=ymin,ymax=ymax), color='black', alpha=0.5) + scale_fill_manual(values = c("0-10% Stroma" = "00ffff", "10-20% Stroma" = "2efce7", "20-30% Stroma" = "50f8cd", "30-40% Stroma" = "6df3b3", "40-50% Stroma" = "87ed99", "50-60% Stroma" = "9fe681", "60-70% Stroma" = "b6de6b", "70-80% Stroma" = "cbd559", "80-90% Stroma" = "e0ca4d", "90-100% Stroma" = "f3be47")) + geom_col(data = plot_sample, mapping = aes(x = row_id, y = cd8_density_total)) + annotate("text", x = 1, y = -.004, label = sample_name, size = 7, color = "black") + geom_textpath(data = stroma_comp, mapping = aes(x=lower_lim + (upper_lim-lower_lim)/2, y = ymin/2, label = stroma_bins), angle = 90, size = 2, color = "white") + theme_void() + scale_fill_discrete(guide = "none") + scale_color_discrete(guide = "none") + coord_polar()
该代码输出效果:未正确应用指定颜色,矩形呈现ggplot默认的离散配色,没有匹配期望的颜色方案。
补充数据(stroma_comp数据框):
structure(list(stroma_bins = structure(1:10, 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"), n = c(12L, 4L, 19L, 72L, 179L, 300L, 281L, 319L, 307L, 353L), upper_lim = c(12L, 16L, 35L, 107L, 286L, 586L, 867L, 1186L, 1493L, 1846L), lower_lim = c(0L, 12L, 16L, 35L, 107L, 286L, 586L, 867L, 1186L, 1493L), ymax = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ymin = c(-0.002, -0.002, -0.002, -0.002, -0.002, -0.002, -0.002, -0.002, -0.002, -0.002)), class = "data.frame", row.names = c(NA, -10L))
解决方案
你的代码存在三个关键问题导致配色失败:
- 未绑定填充变量:
geom_rect的aes中没有指定fill = stroma_bins,ggplot无法将手动设置的颜色与分箱变量关联。 - 重复比例尺覆盖:先设置了
scale_fill_manual,后续又用scale_fill_discrete(guide = "none")覆盖了手动配色,导致自定义颜色失效。 - 颜色格式错误:十六进制颜色值缺少前缀
#,ggplot无法正确识别这些颜色代码。
修正后的代码如下:
ggplot() + scale_x_continuous(name="x") + scale_y_continuous(name="y") + # 绑定fill到stroma_bins变量,建立颜色映射关系 geom_rect(data=stroma_comp, mapping=aes(xmin=lower_lim, xmax=upper_lim, ymin=ymin,ymax=ymax, fill = stroma_bins), color='black', alpha=0.5) + # 添加#前缀修正颜色格式,同时在这里设置隐藏图例 scale_fill_manual(values = c("0-10% Stroma" = "#00ffff", "10-20% Stroma" = "#2efce7", "20-30% Stroma" = "#50f8cd", "30-40% Stroma" = "#6df3b3", "40-50% Stroma" = "#87ed99", "50-60% Stroma" = "#9fe681", "60-70% Stroma" = "#b6de6b", "70-80% Stroma" = "#cbd559", "80-90% Stroma" = "#e0ca4d", "90-100% Stroma" = "#f3be47"), guide = "none") + geom_col(data = plot_sample, mapping = aes(x = row_id, y = cd8_density_total)) + annotate("text", x = 1, y = -.004, label = sample_name, size = 7, color = "black") + geom_textpath(data = stroma_comp, mapping = aes(x=lower_lim + (upper_lim-lower_lim)/2, y = ymin/2, label = stroma_bins), angle = 90, size = 2, color = "white") + theme_void() + scale_color_discrete(guide = "none") + coord_polar()
修改后,每个stroma_bins对应的矩形会严格匹配你指定的固定颜色,且不同样本间的配色映射保持一致。
内容的提问来源于stack exchange,提问作者fgootkind
相关产品推荐
相关产品推荐

