How to Use Viridis Colors with Plotly and Leaflet
“… avoiding catastrophe becomes the first principle in bringing color to information: Above all, do no harm.”
- Envisioning Information, Edward Tufte, Graphics Press, 1990
Choosing colors for your plot is not so simple. Why is that so?
First of all, it depends on numerous things… What plot are you creating? What is its purpose? Who are the readers? Will it be readable when printed in black-and-white? And so on…
There are very interesting StackExchange discussions where you can read on how colors are perceived and why it’s not such a good idea to use red and green.
Probably, there is a place for a blog post solely on choosing colors for different kinds of plots, but let’s save that for another time.
Viridis and other color pallets in R
If you are creating a data visualisation in R there are already a few color palettes available to make your life easier. The pallete that is colorful, perceptually uniform, robust and at the same time pleasing is viridis.
We cannot forget ColorBrewer palettes which are sequential, colorblind-safe and print-friendly. Check out this comparison. At the end the user needs to make a decision and choose the one “she” thinks suits the analysis the most and makes the visualization look pretty, which is of course a very subjective thing.
In this blog post we want to focus solely on viridis and show you how well it plays with ‘plotly’ and ‘leaflet’. ‘Ggplot2’ example is covered by ‘viridis’ documentation.
At this points make sure you have this package installed. Getting the package is easy since it’s available on CRAN so just run:
Viridis and Plotly
First let’s make a very simple plot using ‘plotly’.
We provide viridis colors to plotly using
viridis_pal and by setting
option argument to “D” the default “viridis” is selected. Other options are “A” for “magma” theme, “B” – “inferno” and “C” – “plasma”. Play with letters to check which one you like the most or which suits your plot the best.
As you can see this is pretty straightforward and there is no need to convert ‘viridis’ outcome in any way. ‘Plotly’ and ‘viridis’ play nice together.
Viridis and Leaflet
Now let’s have a look at using ‘viridis’ with ‘leaflet’ maps. For this visualization we use Maritime data from our previous post on cross-filtering. Data contains information about a ship: it’s name, speed and position.
We want to display which ships are sailing and which are moored. In order to do that a new column ‘type’ is added to the data, assuming that if vessel speed is smaller than 4 knots the ship is ‘moored’, otherwise it’s ‘sailing’. That might not be 100% correct, but it will work for the purpose of this example.
To provide colors to ‘leaflet’ we need to make use of
colorFactor function and create mapping between group variable (in our case moored/sailing) and pallete colors.
Next we use our new variable
pal and provide it to
addCircleMarkers method. We call this method, because we want ships to be displayed as small circles on the map.
Last but not least, we need to add a legend to inform our readers which color represents which ship movement type. This is done by providing our viridis palette to
colors argument in
We have ships positions colored differently on the map, but something is wrong with the legend. Is doesn’t display our viridis colors.
Why? The reason is that viridis colors are specified as RGBA which are RGB color values with an alpha opacity parameter. The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
We need to convert our viridis colors to standard RGB (sRGB) in order to show the legend correctly. This can be done using
col2Hex function from
mapview package. Please note that function isn’t available in package namespace
col2Hex is a very short function and can be easily reimplemented without using internal package function.
When we know what is going wrong with our legend we can fix it by creating a helper function
get_viridis_color which will return our viridis palette in sRGB.
It works! We get a nice legend displaying correct viridis colors!
Some concepts presented in this blog post were proposed by Tim Salabim in this answer on Stackoverflow.