Reputation: 1171
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
?
Upvotes: 9
Views: 6534
Reputation: 93811
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))
Upvotes: 8
Reputation: 11762
You have a little typo in your last function. the .5
is within show_col
and not within alpha
. So within show_col
it gets interpret as a rounded 1
and this is boolean TRUE
which leads to show the HEX values.
So the correct line would be
show_col(alpha(nord("aurora",5), .5))
And this produces the faint colors.
Upvotes: 1
Reputation: 7610
Add your alpha value to the geom_raster()
layer:
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(alpha = 0.5, aes(fill = density)) +
scale_fill_viridis(5)
Upvotes: 5