Creating a graph takes time to decide on the structure, but choosing colors often takes even longer. That’s because colors can make a graph look either outdated or aesthetically pleasing. Therefore, I tend to spend a lot of time on color selection. Here, I’ll briefly summarize functions that helps me with color selection. The program used is R
, and I’ve utilized the ggplot2
package.
viridis scales
When mapping colors to continuous numeric values, I primarily use the viridis
scale.
The viridis
color scale can be applied using two functions: scale_fill_viridis_c
and scale_fill_viridis_d
. Since these functions automatically set the colors according to the range, it’s not possible to know the exact color hash value. If you want to customize and combine some colors, you can obtain color values using the pal_viridis
function.
> scales::pal_viridis(option = "D")(10)
[1] "#440154FF" "#482878FF" "#3E4A89FF" "#31688EFF" "#26828EFF"
[6] "#1F9E89FF" "#35B779FF" "#6DCD59FF" "#B4DE2CFF" "#FDE725FF"
You can directly visualize colors using show_col
.
scales::show_col(scales::pal_viridis(option = "D")(10))
brewer scales
Typically, when representing colors based on discontinuous data, such as code or string-based data, the brewer
scale is used. You can use scale_fill_brewer
, and specify the color theme in the palette parameter. The color themes are as follows.
# Diverging
BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral
# Qualitative
Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3
# Sequential
Blues, BuGn, BuPu, GnBu, Greens, Greys,
Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples,
RdPu, Reds, YlGn, YlGnBu, YlOrBr, YlOrRd
Additionally, with the brewer
scale, the number of colors is limited. Each theme is different, but most have a maximum of 9 to 12 colors, so you should keep this in mind when using them.
> scales::pal_brewer(palette = "Purples")(9)
[1] "#FCFBFD" "#EFEDF5" "#DADAEB" "#BCBDDC" "#9E9AC8"
[6] "#807DBA" "#6A51A3" "#54278F" "#3F007D"
You can verify the colors using show_col
.
scales::show_col(scales::pal_brewer(palette = "Purples")(9))