Home > OS >  Manually setting the colors in a geom_rect chart in ggplot
Manually setting the colors in a geom_rect chart in ggplot

Time:02-02

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. 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. The resulting image

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. Failed plot

Thanks in advance for your help.

Edit: Here's the dataframe I'm working with for the inner portion of the chart. 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))

I won't post that part simply because it's almost 2000 entries long.

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))

CodePudding user response:

As I mentioned in my comment the main issue with your second code is that you dropped the mapping on the fill aes, i.e. use fill=stroma_bins in 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 "#", e.g. use "#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()

enter image description here

  • Related