0

If I have hexidecimal color codes saved in my data frame, is there a scale_fill_manual() parameter that will utilize the color codes rather than treating them as strings (as my current code does)?

library(dplyr)
library(ggplot)

mtcars %>% 
    mutate(cyl = as.factor(cyl)) %>% 
    count(cyl) %>% 
    mutate(
        fill = 
            case_when(
                cyl == '4' ~ "#0177c9",
                TRUE ~ "#54b709",
            )
    ) %>% 
    ggplot(aes(x = cyl, y = n, fill = fill)) + 
    geom_col() 

enter image description here

Joe
  • 3,217
  • 3
  • 21
  • 37
  • 1
    Add this `scale_fill_identity() ` to the plot definition – Dave2e Mar 26 '21 at 22:46
  • [Using a pre-defined color palette in ggplot](https://stackoverflow.com/questions/3079264/using-a-pre-defined-color-palette-in-ggplot) – Henrik Mar 26 '21 at 22:49
  • 1
    Does this answer your question? [Using a pre-defined color palette in ggplot](https://stackoverflow.com/questions/3079264/using-a-pre-defined-color-palette-in-ggplot) – Dave2e Mar 26 '21 at 22:51
  • @Dave2e actually your suggestion to add scale_fill_identity() is what I was looking for. If you supply it as the answer I will accept it and close the question. – Joe Mar 26 '21 at 22:54

0 Answers0