I have a known universe of nine colors that I'd like to use to create user defined color palettes with a function.
col_universe <- list(dark_blue = "#034772", med_blue = "#2888BC",
light_blue = "#73B7CE", green = "#699D46", orange = "#EA8936", gold = "#F9C347",
dark_grey = "#58595B", medium_grey = "#7D7E81",light_grey = "#C1C2C4")
What'd I like to do is create arbitrary length color palettes by name.
custom_colors <- function(color1, color2, color3, ..., color9)
So then I'd use it like the following,
pal1 <- custom_colors(dark_blue, green, gold)
pal2 <- custom_colors(gold, orange, light_grey, dark_grey)
I desire pal1
and pal2
to be character vectors (to be supplied to ggplot2)
c("#034772", "#699D46", "#F9C347")
c("#F9C347", "#EA8936", "#C1C2C4", "#58595B")
To create a color palette in Java. Step 1: Import necessary packages. Step 2: Write applet code which should specify width and height of applet window. Step 4: Create a button and named as Change Color.
A color palette is a combination of colors used by UI designers when designing an interface. When used correctly, color palettes form the visual foundation of your brand, help to maintain consistency, and make your user interface aesthetically pleasing and enjoyable to use.
Use match.call
to capture parameters:
custom_colors <- function(...) {
cl = match.call(expand.dots = TRUE)
sapply(cl[-1], function(col) col_universe[[as.character(col)]])
}
custom_colors(dark_blue, green, gold)
[1] "#034772" "#699D46" "#F9C347"
custom_colors(gold, orange, light_grey, dark_grey)
[1] "#F9C347" "#EA8936" "#C1C2C4" "#58595B"
Data:
col_universe <- list(dark_blue = "#034772", med_blue = "#2888BC",
light_blue = "#73B7CE", green = "#699D46", orange = "#EA8936", gold = "#F9C347",
dark_grey = "#58595B", medium_grey = "#7D7E81",light_grey = "#C1C2C4")
You can turn you list into a named vector and subset it using the vector names. It seems like you'll need to extract the values only in order to pass it along to ggplot.
col_universe <- list(dark_blue = "#034772", med_blue = "#2888BC",
light_blue = "#73B7CE", green = "#699D46", orange = "#EA8936", gold = "#F9C347",
dark_grey = "#58595B", medium_grey = "#7D7E81",light_grey = "#C1C2C4")
pal1 <- c('dark_blue', 'green', 'gold')
ggplot(iris, aes(Sepal.Length, Sepal.Width, col = Species)) + geom_point() +
scale_color_manual(values = unname(col_universe[pal1]))
You can write it up in a custom function if you want
custom_palette = function(universe, palette) {
return(unname(universe[palette]))
}
ggplot(iris, aes(Sepal.Length, Sepal.Width, col = Species)) + geom_point() +
scale_color_manual(values = custom_palette(col_universe, pal1))
Using ...
in function arguments allows you to define any number of elements you need. These may be stored in a vector if they are provided as character strings.
custom_colors <- function(universe, ...){
col.names <- c(...)
cols <- sapply(col.names, FUN=function(x) universe[[x]])
return(unname(cols))
}
The implementation works for the defined color universe.
custom_colors(col_universe, "green", "med_blue")
[1] "#699D46" "#2888BC"
custom_colors(col_universe, "dark_blue", "gold", "orange")
[1] "#034772" "#F9C347" "#EA8936"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With