I have some colors that I would like to give a name to be able to use them in ggplot graphs without having to write the hex codes by hand each time I write the code for a graph.
For example I have the following hex colors #EBD0FC
, #F0FFF0
, #BEBEBE
that I would like to name "mypink"
, "mymint"
, "mygrey"
.
mycolors <-
data.frame(name = c("mypink", "mymint", "mygrey"),
hex = c("#EBD0FC", "#F0FFF0", "#BEBEBE"))
And I would like to use the colors in a ggplot graph like that:
scale_fill_manual(values = c("mypink", "mymint", "mygrey"))
instead of
scale_fill_manual(values = c("#EBD0FC", "#F0FFF0", "#BEBEBE"))
As a follow up question: is there a way to "save" my color palette somewhere (online? and maybe only for me to access?) so that I do not have to specify it at the beginning of each session?
Write a function to translate your names into the hex you defined. for example.
mycolors <-
data.frame(name = c("mypink", "mymint", "mygrey"),
hex = c("#EBD0FC", "#F0FFF0", "#BEBEBE"))
myc <- function(x){
mycolors$hex[which(x %in% mycolors$name)]
}
waldo::compare(scale_fill_manual(values = c("#EBD0FC", "#F0FFF0", "#BEBEBE")),
scale_fill_manual(values = myc(c("mypink", "mymint", "mygrey"))))
# ✔ No differences