在ggplot中手动设置geom_rect图表的颜色

nkoocmlb  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(221)

我试图手动设置一种修改过的"sunburst"图的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()

输出如下所示。

这是我要找的颜色。
| 箱|颜色|
| - ------|- ------|
| 0 - 10%基质|00ffff|
| 10 - 20%基质|2efce7|
| 20 - 30%基质|50f8cd|
| 30 - 40%基质|6DF3B3|
| 40 - 50%基质|99年第87版|
| 50 - 60%基质|9Fe681|
| 60 - 70%基质|b6de6b|
| 70 - 80%基质|CBD559|
| 80 - 90%基质|E0ca4d基因|
| 90 - 100%基质|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()

该代码的结果。

先谢谢你的帮助。
编辑:下面是我正在处理的图表内部的 Dataframe ,最外面的部分可以通过注解掉geom_col(data = plot_sample, mapping = aes(x = row_id, y = cd8_density_total)) +来去掉
我不会仅仅因为它有将近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))
dfty9e19

dfty9e191#

正如我在评论中提到的,你的第二个代码的主要问题是你丢弃了fill aes上的Map,即在geom_rect中使用fill=stroma_bins。其次,你用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()

相关问题