1

I am plotting a raster image from the data stored in a file by using the three lines of code given below. However, I wish to generate a legend (or color-chart) below the image. The default legend is generated on the right hand side. Any idea how I may get legend at desired location. Secondly, I am getting a rectangle around the image which I don't require in the final plot. Please help me with these issues.

            temp_var<-raster(input)
    par(mar=c(0,0,0,0))
    plot(temp_var,legend=F,axes=FALSE)

Thanks...

aaron
  • 6,339
  • 12
  • 54
  • 80

2 Answers2

1

I have fought with the rectangle in plot() method for Raster* objects. I am sure there is a way to get rid of it, but you don't need to. I have always used this workaround:

image(temp_var, axes=FALSE, xlab="", ylab="")

As for your legend, I believe you will have to build that manually to your specs. Try:

layout(matrix(c(1,1,1,1,1,2,2), 7, 1))
par(mar=c(0,0,0,0))
image(temp_var, axes=FALSE, xlab="", ylab="")
plot(c(0,1), c(0,1), type="n", xlab="", ylab="", axes=FALSE)
legend(0.1,0.9, legend=c(1:5), fill=rainbow(5), horiz=TRUE)
digitalmaps
  • 2,885
  • 3
  • 22
  • 28
0

I've used colorbar.plot with some success in the past. Disable the legend that plot(raster(x)) automatically inserts with legend=FALSE and then call colorbar.plot(), i.e.

plot(raster(x),legend=FALSE)
colobar.plot(...) 
aaron
  • 6,339
  • 12
  • 54
  • 80
  • PS: The question was posted by a work colleague who accidentally used my logon to post the question. I'm not posting questions then answering them an hour later... – aaron Mar 08 '12 at 04:10