[R] Add title to color spectrum legend in xyplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Mar 26 12:13:43 CET 2010


On Tue, Mar 23, 2010 at 4:28 AM, Nick Ackerman <nick.ackerman at pgn.com> wrote:
>
> Hi,
>
> I have a plot that is essentially the same as that in Figure 5.6 of "Lattice
> - Multivariate Data Visualization with R". The key difference is that I
> would like to add a title to top of the grey color spectrum legend, but have
> thus far been unsuccessful. I've tried a variety of options, but to no
> avail. I'm new to R, and admittedly I don't understand the "legend" argument
> that well, and in this case how it interacts with the "key" argument. Thanks
> in advance for any help. For reference, here is the code to Figure 5.6.

draw.colorkey() doesn't support a title for the legend. The easiest
alternative is to place the legend on top and add a main title:

xyplot(lat ~ long | Magnitude, data = quakes.ordered,
       aspect = "iso", groups = color, cex = 2, col = "black",
       panel = function(x, y, groups, ..., subscripts) {
           fill <- groups[subscripts]
           panel.grid(h = -1, v = -1)
           panel.xyplot(x, y, pch = 21, fill = fill, ...)
       },
       legend =
       list(top =
            list(fun = draw.colorkey,
                 args = list(key = list(space = "top",
                                        col = gray.colors,
                                        at = depth.breaks),
                             draw = FALSE))),
       xlab = "Longitude", ylab = "Latitude",
       main = "Main title")

-Deepayan



More information about the R-help mailing list