Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set transparency/saturation of palette in ggplot

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. enter image description here.

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)

enter image description here

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

enter image description here

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

enter image description here
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)

enter image description here

How can I reduce saturation/intensity in viridisand increase in the nord palettes in ggplot?

like image 625
fede_luppi Avatar asked Mar 08 '18 06:03

fede_luppi


People also ask

How do I create a transparent fill in ggplot2?

You can optionally make the colour transparent by using the form "#RRGGBBAA" . An NA , for a completely transparent colour.

How do I make a color transparent in R?

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”).

Is ggplot colorblind friendly?

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.

How do I change the color of my ggplot in R studio?

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.


1 Answers

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

enter image description here

And the adjusted colors:

show_col(vir_lite(viridis(5)))

enter image description here

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

enter image description here

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

enter image description here

like image 135
eipi10 Avatar answered Sep 20 '22 00:09

eipi10