21

I have defined a colorRampPalette:

my.colors = colorRampPalette(c("light green", "yellow", "orange", "red"))

How can I plot a colorbar "legend" item for it, preferably using only the base packages? I am after a rectangle filled with that color gradient.

What I am really after is a way to produce the same type of legend (color bar) that is plotted with a "raster" raster:

require(raster)
plot(raster("myfile.tif"), legend=T)

I need to be able to place this on top of another plot.

Benjamin
  • 11,560
  • 13
  • 70
  • 119
  • 2
    It's funny how difficult this is to do in base R. Usually R has so much magic but apparently not for colorbars. I guess statisticians only think in black and white? haha – wordsforthewise Jan 18 '18 at 23:15

4 Answers4

35

I made a nice flexible function awhile ago to do this.

# Function to plot color bar
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
    scale = (length(lut)-1)/(max-min)

    dev.new(width=1.75, height=5)
    plot(c(0,10), c(min,max), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
    axis(2, ticks, las=1)
    for (i in 1:(length(lut)-1)) {
     y = (i-1)/scale + min
     rect(0,y,10,y+1/scale, col=lut[i], border=NA)
    }
}

Then you can do something like:

> color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), -1)

enter image description here

More examples at: http://www.colbyimaging.com/wiki/statistics/color-bars

John Colby
  • 22,169
  • 4
  • 57
  • 69
10

Make a matrix, use image with some axes parameters...

my.colors = colorRampPalette(c("light green", "yellow", "orange", "red"))
z=matrix(1:100,nrow=1)
x=1
y=seq(3,2345,len=100) # supposing 3 and 2345 are the range of your data
image(x,y,z,col=my.colors(100),axes=FALSE,xlab="",ylab="")
axis(2)

image legend

Spacedman
  • 92,590
  • 12
  • 140
  • 224
4

colorbar.plot() from the fields package is a standard solution. Many folks will be looking here for solutions that place the legend outside of the standard plot area or the original x/y range. This limitation of colorbar.plot() is easily overcome by:

  1. plotting your image(),
  2. expanding the size of the plot device with par(pin = c(width, length))
  3. calling colorbar.plot() using original x and y values

Because the area of the plot device is now expanded, the legend will appear outside of the original image() plot. Experimenting with x and y values lands the legend anywhere you wish. The gradient legend can be horizontal or vertical using standard argument options.

Brad Horn
  • 649
  • 6
  • 12
  • Yes but you also need the 'strip' argument and so far it's not as simple as it should be to figure out. A working code example is usually part of a good answer. – wordsforthewise Jan 18 '18 at 23:12
0

I know this is a very old question, and the answers are all great. I just wanted to point out that you could just smooth the color bar from levelplot.

Make some fake data and a color gradient

mat = matrix(rnorm(400,1,1), ncol=20)
grad = rev(rainbow(1000, start=rgb2hsv(col2rgb('green'))[1], 
       end=rgb2hsv(col2rgb('blue'))[1]))

Make the plot and specify breaks in the color bar

levelplot(mat, col.regions=grad, colorkey = list(at=seq(0,max(mat),length.out=400)))

This functionality to levelplot may have been added long after all you folks came up with sweet workaround solutions.

Tad Dallas
  • 1,179
  • 5
  • 13