Skip to content
Prev 22230 / 29559 Next

How to show the color bar legend in R?

Hi Ferra,
Without reproducible code, it's difficult to help. You might have a look at
image.plot() in the fields library, and image()

I'm not sure if this is exactly what you're looking for, but here is a
quick example of one way to control the image and legend in a plot, using
the layout command. You can create a small panel below your main plot and
then add your legend there with more control.

I hope this is helpful.

###########################################
# example code for image plot of volcano data
require(fields)
require(raster)

dat <- raster(volcano)


zc <- as.matrix(seq(minValue(dat), maxValue(dat), length.out=100))

mycolor1 <- two.colors(n=100, start="darkblue", end="red",
middle="yellow1", alpha=.9)

layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(4,1), widths=c(4) )
layout.show(2)
image(dat, axes=F, col=mycolor1, xlab="", ylab="")

par(mar=c(2,4,.1,2))
image(zc, col=mycolor1, axes=F, xlab="", ylab="" )

axis(1, at=seq(0,1, length.out=5),  labels=round(seq(minValue(dat),
maxValue(dat),length.out=5), digits=0), las=1,  tcl=-.15, padj=-1, cex=1.5)
# every .1 corr
# end code
#############################################



On Mon, Jan 26, 2015 at 10:36 AM, Ferra Xu via R-sig-Geo <
r-sig-geo at r-project.org> wrote: