5

I want to create a contour plot that has a color key next to it.

I would make the contour plot like this:

data(volcano)
image(volcano)
contour(volcano, add = TRUE)

However, image() does not support a legend as far as I know. I saw that there is also a contourplot() function in the lattice package, however, I cannot figure it out how to overlay the map with the contour lines when using levelplot(volcano) and contourplot(volcano)

Ideally I want my plot to look like this but with a color key:

enter image description here

  • I'm sure someone has a `ggplot` solution to this, but this was also addressed in this question previously - http://stackoverflow.com/questions/13355176/gradient-legend-in-base – thelatemail Apr 08 '13 at 01:49
  • 3rd example from the bottom: http://docs.ggplot2.org/current/stat_contour.html – Ricardo Saporta Apr 08 '13 at 01:52
  • 1
    Thanks for the comment! Hm, I wanted to avoid using ggplot2, I can't help it, but I don't like the look of the ggplot2 graphics. But if there is no other way I might have to do it this way –  Apr 08 '13 at 01:55

1 Answers1

14

You could use image.plot from the fields package. This creates a legend by default.

library(fields)
image.plot(volcano)
contour(volcano, add = TRUE)

enter image description here

mnel
  • 113,303
  • 27
  • 265
  • 254