Set the transparency / saturation of the palette in ggplot

Start with the palette viridis. In my opinion, the colors are too bright for me, and for my purposes they look too artificial. so I would like to apply some 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. enter image description here.

However, when it starts in ggplot, it automatically changes the alpha value to 1 and sets the full viridis intensity:

ggplot(faithfuld, aes(waiting, eruptions)) +
  geom_raster(aes(fill = density)) +
  scale_fill_viridis(5, alpha=.5)

enter image description here

In another example, I found the opposite problem, lack of intensity / saturation. For example, the "shine" palette from the package is nordgorgeous, but it looks a bit dull, lacks saturation, at least for my purposes.

show_col(nord("aurora",5))

enter image description here

, , 1, viridis, .

show_col(nord("aurora", alpha=.5))

enter image description here
alpha(). , .

show_col(alpha(nord("aurora",5)), .5)

enter image description here

/ viridis nord ggplot?

+4
3

viridis, , . , viridis, . - , ( viridis) hsv, saturation value, .

. , .

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]))
}

viridis:

show_col(viridis(5))

enter image description here

:

show_col(vir_lite(viridis(5)))

enter image description here

, ds dv. :

p = ggplot(faithfuld, aes(waiting, eruptions)) +
      geom_raster(aes(fill = density))

p + scale_fill_gradientn(colors=vir_lite(viridis(5)))

enter image description here

p + scale_fill_gradientn(colors=vir_lite(viridis(5), ds=0.6, dv=0.5))

enter image description here

+3

- geom_raster():

ggplot(faithfuld, aes(waiting, eruptions)) +
    geom_raster(alpha = 0.5, aes(fill = density)) +
    scale_fill_viridis(5)

enter image description here

+2

You have a small typo in your last function. .5is within show_col, not inside alpha. Thus, inside show_colit is interpreted as rounded 1, and it is logical TRUE, which leads to the display of HEX values.

So the correct line will be

show_col(alpha(nord("aurora",5), .5))

And it creates faint colors.

+1
source

Source: https://habr.com/ru/post/1694633/


All Articles