Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending RColorBrewer to support more colors?

Tags:

r

colors

RColorBrewer allows you to get a small number of visually pleasing colors like so:

> library(RColorBrewer)
> brewer.pal(11, "Spectral")
 [1] "#9E0142" "#D53E4F" "#F46D43" "#FDAE61" "#FEE08B" "#FFFFBF" "#E6F598"
 [8] "#ABDDA4" "#66C2A5" "#3288BD" "#5E4FA2"

But if you ask for more than that amount, they just give you that same maximum amount instead:

> brewer.pal(12, "Spectral")
 [1] "#9E0142" "#D53E4F" "#F46D43" "#FDAE61" "#FEE08B" "#FFFFBF" "#E6F598"
 [8] "#ABDDA4" "#66C2A5" "#3288BD" "#5E4FA2"
Warning message:
In brewer.pal(12, "Spectral") :
  n too large, allowed maximum for palette Spectral is 11
Returning the palette you asked for with that many colors

Is it possible to get a larger number of colors out (perhaps by interpolating between the most distant colors) if a larger number is supplied?

like image 290
rhombidodecahedron Avatar asked Dec 05 '15 14:12

rhombidodecahedron


1 Answers

The standard way of doing this is to use a function that interpolates the set of given colors. It then creates a new color palette. In this case colorRampPalette:

require(RColorBrewer)

colorRampPalette( brewer.pal(9,"YlOrRd") )(50)
# [1] "#FFFFCC" "#FFFCC4" "#FFF9BD" "#FFF6B6" "#FFF3AF" "#FFF0A8" "#FFEDA0"
# [8] "#FEEA9A" "#FEE693" "#FEE38C" "#FEE085" "#FEDD7E" "#FED977" "#FED470"
#[15] "#FECD6A" "#FEC763" "#FEC15C" "#FEBA55" "#FEB44E" "#FDAE4A" "#FDA847"
#[22] "#FDA245" "#FD9C42" "#FD963F" "#FD903D" "#FC873A" "#FC7D37" "#FC7334"
#[29] "#FC6931" "#FC5E2E" "#FC542B" "#FA4A29" "#F64226" "#F23924" "#EE3122"
#[36] "#EA2820" "#E6201D" "#E1181C" "#DB141E" "#D5101F" "#CE0C21" "#C80722"
#[43] "#C20324" "#BB0026" "#B10026" "#A70026" "#9D0026" "#930026" "#890026"
#[50] "#800026"

Also see @rawr 's answer in the comments. Adding the desired output here for a better understanding of what the function does.

like image 177
Andre Wildberg Avatar answered Oct 04 '22 19:10

Andre Wildberg