A package to support easy porting and usage of custom color palettes for ggplot.
# install.packages("devtools")
devtools::install_github("milesdwilliams15/coolorrr")
The {coolorrr}
package provides tools to make the process of porting and using custom color palettes with ggplot()
easier. The main functions are:
set_palette()
set_theme()
ggpal()
coolors()
The first function must be run before you can use ggpal()
. It sets up four palettes (with default selections for each):
- A qualitative palette
- A diverging palette
- A sequential palette
- A qualitative palette for two cases
The function is meant to make working with your own color palettes either directly specified by the user or ones you can make for free at https://coolors.co/ easy and straightforward. ggpal()
will then apply your chosen theme for the relevant palette type when you call it for use with ggplot()
from the {ggplot2}
package.
To use the default values simply run:
set_palette()
Alternatively, to use a custom palette from coolors.co, simply drop in the url. For instance, here's a qualitative palette I picked: https://coolors.co/ee6352-08b2e3-efe9f4-57a773-484d6d.
To use it, I would simply run:
set_palette(qualitative = "https://coolors.co/ee6352-08b2e3-efe9f4-57a773-484d6d")
You aren't restricted to only using palettes from coolors.co. You may also input your own "manually" by providing a vector of colors. For example:
set_palette(qualitative = c("royalblue", "firebrick", "forestgreen", "gold", "orange"))
Note: You can possibly use a mix of palettes, with some from coolors.co and others set manually like above.
For instance, say we were to set the qualitative palette using color names and the two-case qualitative palette as a custom palette set at coolors.co. We would write:
set_palette(qualitative = c("royalblue", "firebrick", "forestgreen", "gold", "orange"),
binary = "https://coolors.co/ee6352-08b2e3")
After you've set your palettes, you can use ggpal()
using the + ggpla()
syntax to update the default palettes used by ggplot()
for aesthetic color or fill mapping. ggpal()
has five basic commands and also allows you to pass any other commands to standard {ggplot2}
functions that ggpal()
calls under the hood.
type
: specifies whether the palette to apply is for the qualitative, diverging, sequential, or binary palette;aes
: specifies whether the palette should be applied for the color or fill aesthetic;midpoint
: specifies the midpoint value if the diverging palette is used;ordinal
: default isFALSE
. If you are using either a sequential or diverging palette with ordinal rather than numerical data, you should set toTRUE
. The default assumes that sequential or diverging palettes are for continuous or discrete numerical data.levels
: default isNULL
. If you are using either a sequential or diverging palette with ordinal data, in addition to settingordinal = TRUE
you should specify how many levels are present for the ordinal data. This is unnecessary if either of these palettes are numerical.
library(ggplot2)
library(coolorrr)
set_palette() # with defaults
p <- ggplot(mtcars) +
aes(x = wt,
y = mpg,
color = as.factor(cyl)) +
geom_point() # basic output
p + # custom output with ggpal()
ggpal(type = "qualitative",
aes = "color")