Trying to find a good color palette in R is difficult. I think most people search google for
ggplot colors and end up looking at some wild image like shown below on the right. These colors are used in
grDevices::colors and are actually from the X11 colors that were developed in the 1980s. Unfortunately, they have inconsistent names and the lightness/saturation are all over the place. Using the
colorspace::HLS() function however, a smaller, consistent set of colors allows palettes to be made both within one color or across hues.
Here’s a comparison of the base X11 colors (left) and the
To get started there are 8 hues, 4 types of saturation, and 5 levels of lightness plus a greyscale. To use a color, just combine the 3 parts:
|optional saturation||color name||lightness|
For example, the following code will return the corresponding hex values
This function stands for simplecolors. You can specify base colors
#>  "#E44444" "#9444E4" "#E444E4"
or add modifiers
#>  "#003CB3" "#4D3C19" "#949494"
There are multiple ways to access palettes
sc_within()- within 1 hue, the default is a lightness of 2:6, and no modifier (sat == "")
sc_across()- holding light and saturation constant (at your choosing), a palette will be built across hues in the order you specify. The palette order is created using the first letter (uppercase) of each color, or “Gy” for “grey”.
sc_[color]()there is a shortcut of
sc_within()for each of the hues, for example
#>  "#29FFFF" "#00B3B3" "#006666"
#>  "#86682D" "#2D8686" "#592D86" "#595959"
There are 3 main outputs for these palettes that can be specified via
#>  "#9DB9F1" "#4479E4" "#16439C" "#0D2659"
#>  "#E44444" "#E4AF44" "#E4E444"
Here is a list of all colors abbreviations you can use in the palette
There is also a
sc_within() palette defaulted for each color
#>  "#FFCCCC" "#FF8F8F" "#FF2929" "#B30000"
And here’s an example where you might use it in a ggplot
blue_and_red <- c( sc_blue(4:2, "bright"), sc_red(2:4, "bright") ) iris %>% mutate(cut = ntile(Sepal.Length, 6)) %>% ggplot(aes(Sepal.Width, Sepal.Length, color = factor(cut))) + geom_count(size = 5) + #setting your custom gradients scale_color_manual(values = blue_and_red) + theme_minimal()