What Is a "Good" Palette for Divergent Colors in R? (Or: Can Viridis and Magma Be Combined Together)

What is a good palette for divergent colors in R? (or: can viridis and magma be combined together?)

I find Kenneth Moreland's proposal quite useful. It has now been implemented as cool_warm in heatmaply:

# install.packages("heatmaply")
img(heatmaply::cool_warm(500), "Cool-warm, (Moreland 2009)")

cool-warm colormap
This it how it looks like in action compared to an interpolated RColorBrewer "RdBu": comparison

Color palettes in the eulerr package

Yes, it is possible

library(viridis)
library(eulerr)

Pull some viridis colors. I'm using the plasma scale (you can use magma, inferno, whatever scale you like) and pulling 4 colors because that's how many overlap areas there are in the demo plot further down.

colors <- c(viridis::plasma(n = 4))

Make a eulerr diagram, from eulerr demo here

fit1 <- euler(c("A" = 25, "B" = 5, "C" = 5,
"A&B" = 5, "A&C" = 5, "B&C" = 3,
"A&B&C" = 3))
set.seed(1)
mat <-
cbind(
A = sample(c(TRUE, TRUE, FALSE), size = 50, replace = TRUE),
B = sample(c(TRUE, FALSE), size = 50, replace = TRUE),
C = sample(c(TRUE, FALSE, FALSE, FALSE), size = 50, replace = TRUE)
)
fit2 <- euler(mat)

Plot the eulerr diagram with our viridis colors

plot(fit2,
fills = list(fill = colors),
edges = list(lty = 1:3),
labels = list(font = 2))

Best color to annotate against viridis

Following the comment of @JohanC, I agree that using different face and edge colors is a good idea. Here is the example:

# It is only a fraction of code to reproduce the figure, just an example of text highlight
import matplotlib.patheffects as PathEffects
txt = axes.text(1.15, 8.25,"Good fit at dense and\nCO-rich conditions", color='w', rotation=45)
txt.set_path_effects([PathEffects.withStroke(linewidth=1, foreground='k')])

patheffects on viridis

Understanding color scales in ggplot2

This is a good question... and I would have hoped there would be a practical guide somewhere. One could question if SO would be a good place to ask this question, but regardless, here's my attempt to summarize the various scale_color_*() and scale_fill_*() functions built into ggplot2. Here, we'll describe the range of functions using scale_color_*(); however, the same general rules will apply for scale_fill_*() functions.

Overall Categorization

There are 22 functions in all, but happily we can group them intelligently based on practical usage scenarios. There are three key criteria that can be used to define practically how to use each of the scale_color_*() functions:

  1. Nature of the mapping data. Is the data mapped to the color aesthetic discrete or continuous? CONTINUOUS data is something that can be explained via real numbers: time, temperature, lengths - these are all continuous because even if your observations are 1 and 2, there can exist something that would have a theoretical value of 1.5. DISCRETE data is just the opposite: you cannot express this data via real numbers. Take, for example, if your observations were: "Model A" and "Model B". There is no obvious way to express something in-between those two. As such, you can only represent these as single colors or numbers.

  2. The Colorspace. The color palette used to draw onto the plot. By default, ggplot2 uses (I believe) a color palette based on evenly-spaced hue values. There are other functions built into the library that use either Brewer palettes or Viridis colorspaces.

  3. The level of Specification. Generally, once you have defined if the scale function is continuous and in what colorspace, you have variation on the level of control or specification the user will need or can specify. A good example of this is the functions: *_continuous(), *_gradient(), *_gradient2(), and *_gradientn().

Continuous Scales

We can start off with continuous scales. These functions are all used when applied to observations that are continuous variables (see above). The functions here can further be defined if they are either binned or not binned. "Binning" is just a way of grouping ranges of a continuous variable to all be assigned to a particular color. You'll notice the effect of "binning" is to change the legend keys from a "colorbar" to a "steps" legend.

The continuous example (colorbar legend):

library(ggplot2)
cont <- ggplot(mtcars, aes(mpg, disp, color=cyl)) + geom_point(size=4)

cont + scale_color_continuous()

Sample Image

The binned example (color steps legend):

cont + scale_color_binned()

Sample Image

The following are continuous functions.





















































































Name of FunctionColorspaceLegendWhat it does
scale_color_continuous()defaultColorbarbasic scale (as if you did nothing)
scale_color_gradient()user-definedColorbardefine low and high values
scale_color_gradient2()user-definedColorbardefine low mid and high values
scale_color_gradientn()user_definedColorbardefine any number of incremental val
scale_color_binned()defaultColorstepsbasic scale, but binned
scale_color_steps()user-definedColorstepsdefine low and high values
scale_color_steps2()user-definedColorstepsdefine low, mid, and high vals
scale_color_stepsn()user-definedColorstepsdefine any number of incremental vals
scale_color_viridis_c()ViridisColorbarviridis color scale. Change palette via option=.
scale_color_viridis_b()ViridisColorstepsViridis color scale, binned. Change palette via option=.
scale_color_distiller()BrewerColorbarBrewer color scales. Change palette via palette=.
scale_color_fermenter()BrewerColorstepsBrewer color scale, binned. Change palette via palette=.



Related Topics



Leave a reply



Submit