[英]Manually setting the colors in a geom_rect chart in ggplot
I'm trying to manually set the colors of a geom_rect portion of a kind of modified "sunburst" plot to something specific for each rectangle.我正在尝试将一种修改后的“朝阳”plot 的 geom_rect 部分的 colors 手动设置为每个矩形特定的内容。 Thus far all I can do is assign a color gradient to it that doesn't match up from sample to sample in my dataset.
到目前为止,我所能做的就是为其分配一个颜色渐变,该渐变在我的数据集中的样本之间不匹配。
Here's the code I have so far.这是我到目前为止的代码。
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()
Here's what the output looks like.这是 output 的样子。
Here's the color loadout I'm looking for.这是我正在寻找的颜色加载。
Bin![]() |
Color![]() |
---|---|
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 ![]() |
Here's my first attempt at the color matching.这是我第一次尝试配色。
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()
The results of that code.该代码的结果。
Thanks in advance for your help.在此先感谢您的帮助。
Edit: Here's the dataframe I'm working with for the inner portion of the chart.编辑:这是我正在处理图表内部的 dataframe。 The outermost portion can be gotten rid of by commenting out
geom_col(data = plot_sample, mapping = aes(x = row_id, y = cd8_density_total)) +
最外面的部分可以通过注释掉
geom_col(data = plot_sample, mapping = aes(x = row_id, y = cd8_density_total)) +
I won't post that part simply because it's almost 2000 entries long.我不会仅仅因为它有将近 2000 个条目而发布该部分。
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))
As I mentioned in my comment the main issue with your second code is that you dropped the mapping on the fill
aes, ie use fill=stroma_bins
in geom_rect
.正如我在评论中提到的,您的第二个代码的主要问题是您放弃了
fill
aes 上的映射,即在 geom_rect 中使用fill=stroma_bins
geom_rect
。 Second, you are overwriting scale_fill_manual
by scale_fill_discrete
, so remove the latter to use your custom color palette and finally for your color codes you have to add a "#"
, eg use "#00ffff"
.其次,你正在用
scale_fill_discrete
覆盖scale_fill_manual
,所以删除后者以使用你的自定义调色板,最后你必须为你的颜色代码添加一个"#"
,例如使用"#00ffff"
。
library(ggplot2)
library(geomtextpath)
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_bins
), 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"
) +
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") +
theme_void() +
scale_color_discrete(guide = "none") +
coord_polar()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.