Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit the range of the viridis colour scale?

Tags:

r

ggplot2

viridis

I have two sets of data, which I want to present using a heat map with the viridis color scale. For the first data set, my values range from 0 to 1.2 and I can easily see the differences I want to see. However my second data set has some outliers, resulting in a range from 0 to 2. Now it's harder to see the differences in the interesting range between 0 and 1 and it's more diffucult to compare the two images directly. Is there a possibility to show the data from 0 to 1.2 using the viridis colour scale while showing the higher values in yellow ("highest" colour of the viridis scale)? Here is an example:

library(viridis)

#Create Data
DataSet1 <- expand.grid(x = 0:5, y = 0:5)
DataSet1$z <- runif(36, 0, 1.2)

DataSet2 <- expand.grid(x = 0:5, y = 0:5)
DataSet2$z <- runif(36, 0, 2)

#Plot Data
ggplot(DataSet1, aes(x, y, fill = z)) + 
  geom_tile() +
  scale_fill_viridis() +
  geom_text(aes(label = round(z, 2)), size = 2)

DataSet1: Differences between 0.5 and 0.7 are easy to see

enter image description here

ggplot(DataSet2, aes(x, y, fill = z)) + 
  geom_tile() +
  scale_fill_viridis() +
  geom_text(aes(label = round(z, 2)), size = 2)

DataSet2: Differences between 0.5 and 0.7 are diffucult to see

enter image description here

like image 202
Martin B. Avatar asked Jan 24 '18 14:01

Martin B.


People also ask

Is viridis colorblind friendly?

The matplotlib colormaps introduced in 2015 are widely popular, with implementations of the palettes in R, D3js, and others. Popular for good reason, the palettes are colorblind-friendly, retain representational clarity in greyscale, and are generally aesthetically pleasing.

What is scale fill viridis?

scale_viridis.Rd. The viridis scales provide colour maps that are perceptually uniform in both colour and black-and-white. They are also designed to be perceived by viewers with common forms of colour blindness. See also https://bids.github.io/colormap/.

How many colors are in viridis?

The Color Scales The package contains eight color scales: “viridis”, the primary choice, and five alternatives with similar properties - “magma”, “plasma”, “inferno”, “civids”, “mako”, and “rocket” -, and a rainbow color map - “turbo”.

What is viridis?

Viridis is a complete energy management solution developed exclusively for companies which are heavy energy consumers. Viridis includes sophisticated engineering and artificial intelligence tools to facilitate control over processes in real time.


1 Answers

Are you looking for something like this?

ggplot(DataSet2, aes(x, y, fill = z)) + 
  geom_tile() +
  scale_fill_gradient(low="green", high="red", limits=c(0, 1.2), 
                      na.value = "yellow") +
  geom_text(aes(label = round(z, 2)), size = 2)

enter image description here

Using the viridis colors, asper jazzurro recommendation.

ggplot(DataSet2, aes(x, y, fill = z)) + 
  geom_tile() + 
  scale_fill_gradientn(colors = viridis_pal()(9), limits=c(0, 1.2), 
                       na.value = "#FDE725FF") + 
  geom_text(aes(label = round(z, 2)), size = 2)

enter image description here

like image 99
Prradep Avatar answered Sep 19 '22 17:09

Prradep