In an R session, given:
foo <- matrix(1:25, 5, 5)
image(foo)
What is the best way for me to add a legend or key for the color gradient used on this image() plot of foo?
This is for a large dataset of global precipitation values so hacking something together with legend() doesn’t seem to be a viable option. filled.contour() has a number of side effects that I’m not happy with. I’m using image() because it is the simplest plotting method to layer or add onto.
For the moment, my biggest issue with filled.contour() is that I’m trying to add contours from a different dataset via contour() to the plot. With filled.contour(), the contours would need to be adjusted to account for the default gradient key on the side of the plot, though I suppose this would also be the case if I added a key to the image() plot.
Thank you kindly for your time.
For future reference:
When using filled.contour(), you can call contour() and/or map() in addition to any other function you like by assigning it to filled.contour()‘s plot.axes argument. It might be helpful to remember that you can stack multiple lines of code with braces.
Here’s some code adapted from the zernike package. You can use it all, or just pull out the piece which creates a gradient key.
# written 13 April 2011 by CGWitthoft. Watch for updates from the
# owner of the zernike package.