Reputation: 792
Suppose I have a data with a very big range
data = c(1:5, 30:40, 58, 200:210, 400)
I tried to plot it with the R packages ‘RColorBrewer’ but the values 1:5 are nearly white color.
How can I plot a nice heatmap? Like from 1:5 is changing gradually from white to green, then 30:40 is changing from blue to purple... etc But another problem is the single value like 58....
Upvotes: 1
Views: 485
Reputation: 2881
As pointed out in the comments, it seems unlikely that your desired palette can be fully automated. However, it is not too hard to manually create a bespoke palette.
In the following, I've taken some base R colour names and interpolated these using the built in function colorRampPalette
, wrapped in a helper f
just for convenience.
In this way, you can create whatever palette you like:
library(ggplot2)
data <- c(1:5, 30:40, 58, 200:210, 400)
f <- function(n, col1, col2 = NULL) colorRampPalette(c(col1, col2))(n)
colours <- c(
f(5, "white", "cyan"),
f(11, "blue", "purple"),
f(1, "violet"),
f(11, "pink", "red"),
f(1, "black")
)
ggplot(NULL, aes(x = factor(data), y = 1, fill = factor(data))) +
geom_tile() +
scale_fill_manual(values = colours) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) # Just to fit reprex
Created on 2020-05-22 by the reprex package (v0.3.0)
Obviously, you'll have to play around with the actual colour values to get something that looks good for your data. Note that you don't need to use the colour names defined by R. colorRampPalette
(and hence f
) also takes hex colours, for instance, which you can grab from ColorBrewer, if you particularly like some of those.
Upvotes: 1