Search code examples
rggplot2ggforce

Fill aesthetic used twice with continuous and discrete scales


I've got a data like below:

structure(list(bucket = structure(1:23, .Label = c("(1.23,6.1]", 
"(6.1,10.9]", "(10.9,15.6]", "(15.6,20.4]", "(20.4,25.1]", "(25.1,29.9]", 
"(29.9,34.6]", "(34.6,39.4]", "(39.4,44.2]", "(44.2,48.9]", "(48.9,53.7]", 
"(53.7,58.4]", "(58.4,63.2]", "(63.2,68]", "(68,72.7]", "(72.7,77.5]", 
"(77.5,82.2]", "(82.2,87]", "(87,91.7]", "(91.7,96.5]", "(96.5,101]", 
"(101,106]", "(106,111]"), class = "factor"), value = c(0.996156321090158, 0.968144290236367, 0.882793110384066, 0.719390676388129, 0.497759597498133, 
0.311721580067415, 0.181244079443301, 0.0988516758834657, 0.0527504526341006, 
0.0278716018561911, 0.0145107725175315, 0.00785033086321829, 
0.00405759957072942, 0.00213190168252939, 0.00109610249274952, 
0.000578154695264754, 0.000301095727545301, 0.000155696457494707, 
8.2897211122996e-05, 4.09225082176349e-05, 2.33782236798641e-05, 
1.21665352966827e-05, 6.87373003802479e-06), bucket_id = 1:23), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -23L))

Which I want to visualise as a circular stacked bar plot:

cutoff_values <- seq(0, 115, by = 5)


library(tidyverse)
ex %>% 
  mutate(r0 = cutoff_values[-length(cutoff_values)],
         r = cutoff_values[-1]) %>% 
  mutate(x0 = 100, 
         y0 = 50) %>% 
  ggplot(aes(x0 = x0, y0 = y0, r0 = r0, r = r)) +
  ggforce::geom_arc_bar(aes(start = 0, end = 2 * pi, fill = value),
                        colour = NA) +
  theme_void() +
  labs(fill = 'colour')

enter image description here

But I also need to be able to mark out some particular bucket with different filling at best. So I need to be able to preserve filling using value with continuous scale, but also fill one particular stratum (let's say bucket == 15) with another colour, leaving the other strata (buckets) as they are. Is it possible? What are the alternatives to mark out bucket 15th?


Solution

  • I believe that this can be done with the relayer package, which is still highly experimental. You can copy a subset of your data in a seperate geom and give it another fill aesthetic. This seperate geom can then be piped into rename_geom_aes() and you would have to set the scale_fill_*() for your renamed aesthetic. You'd probably get a warning about that the geom is ignoring unknown aesthetics, but I don't know if that can be helped.

    Below is an example for making bucket 15 red.

    library(tidyverse)
    library(relayer) # https://github.com/clauswilke/relayer
    
    ex <- df %>% 
      mutate(r0 = cutoff_values[-length(cutoff_values)],
             r = cutoff_values[-1]) %>% 
      mutate(x0 = 100, 
             y0 = 50)
    
    ggplot(ex, aes(x0 = x0, y0 = y0, r0 = r0, r = r)) +
      ggforce::geom_arc_bar(aes(start = 0, end = 2 * pi, fill = value),
                            colour = NA) +
      ggforce::geom_arc_bar(data = ex[ex$bucket_id == 15,], # Whatever bucket you want
                            aes(start = 0, end = 2 * pi, fill2 = as.factor(bucket_id))) %>% 
      rename_geom_aes(new_aes = c("fill" = "fill2")) +
      scale_fill_manual(aesthetics = "fill2", values = "red", guide = "legend") +
      theme_void() +
      labs(fill = 'colour', fill2 = "highlight")