Let's start with the viridis
palette. In my opinion, colours are a bit just too bright for me, and for my purposes they look too artificial. therefore, I would like to apply some sort of transparency or similar to reduce saturation:
library(nord)
library(scales)
library(viridis)
library(nord)
show_col(viridis(5))
show_col(viridis(5, alpha=.5))
Applying alpha transparency internally seems to work. .
However, when run in ggplot, it automatically changes alpha to 1 and plots the original viridis in full intensity:
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density)) +
scale_fill_viridis(5, alpha=.5)
In another example, I found the opposite problem, lack of intensity/saturation. For example, the "aurora" palette from the nord
package is great, but it looks a bit faded, lacking some saturation, at least for my purposes.
show_col(nord("aurora",5))
Similarly, I tried to set alpha internally, in this case to 1, but this pruduces a different effect as compared to viridis, changing the palette.
show_col(nord("aurora", alpha=.5))
Alternatively, I have set alpha as alpha()
. However, this only changes the color names, but they look the same.
show_col(alpha(nord("aurora",5)), .5)
How can I reduce saturation/intensity in viridis
and increase in the nord
palettes in ggplot
?
You can optionally make the colour transparent by using the form "#RRGGBBAA" . An NA , for a completely transparent colour.
Make transparent colors in RThe rgb() command is the key: you define a new color using numerical values (0–255) for red, green and blue. In addition, you set an alpha value (also 0–255), which sets the transparency (0 being fully transparent and 255 being “solid”).
More specifically, ggpubfigs contains six color palettes that are colorblind friendly and aim to increase the accessibility of scientific figures and eight themes that modify 21 parameters of a default ggplot2 figure.
Change ggplot colors by assigning a single color value to the geometry functions ( geom_point , geom_bar , geom_line , etc). You can use R color names or hex color codes. Set a ggplot color by groups (i.e. by a factor variable). This is done by mapping a grouping variable to the color or to the fill arguments.
You can adjust the viridis colors to reduce their saturation without making them transparent. I was hoping you could do this within the viridis
function, but it doesn't look like there's a way to do that. Instead, the example below is a function that converts a vector of hexadecimal input colors (we'll create this vector with the viridis
function) to the hsv
colorspace, adjusts the saturation
and value
levels and then converts back to hexadecimal.
The approach below is a bit convoluted. There are probably more direct ways to transform between color systems.
vir_lite = function(cols, ds=0.4, dv=0.7) {
cols = rgb2hsv(col2rgb(cols))
cols["v", ] = cols["v", ] + dv*(1 - cols["v", ])
cols["s", ] = ds*cols["s", ]
apply(cols, 2, function(x) hsv(x[1], x[2], x[3]))
}
Here are the original viridis
colors:
show_col(viridis(5))
And the adjusted colors:
show_col(vir_lite(viridis(5)))
You can change the adjusted colors by changing the ds
and dv
arguments. Now let's use the adjusted colors in the plot:
p = ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density))
p + scale_fill_gradientn(colors=vir_lite(viridis(5)))
p + scale_fill_gradientn(colors=vir_lite(viridis(5), ds=0.6, dv=0.5))
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