Is it possible to place the spplot (spplot polygons) legend within the map, in lower left corner, like this?
The closest I've been able to get is this (I am not posting my data, I just use the example data instead, so in this case, try to place the legend in top left part of the map):
data(meuse.grid)
gridded(meuse.grid)=~x+y
spplot(meuse.grid[,'dist'],
colorkey = list(space = "left", height = 0.5)
)
But the legend is in the middle of the page and is outside of the map. Unfortunatelly, colorkey
argument doesn't support "bottomleft", or x, y, or corner arguments (see ?levelplot
). I also tried to use key.space
argument, but it seems to only work when plotting SpatialPoints*
but it seems ignored for SpatialPolygons*
(or SpatialPixelsDataFrame like in the example above).
The complicating issue here is that, although the
colorkey=
argument is treated very much like thelegend=
argument, it doesn't quite support the full suite of positioning options thatlegend=
does. Whereas legends can be directly placed at"left"
,"right"
,"top"
,"bottom"
, and"inside"
the plot,colorkey=
only supports the first four of those.A fairly clean workaround is to extract the colorkey argument list prepared by one call to
spplot()
, and to pass that in to a secondspplot()
call via itslegend=
argument.colorkey=
"knows" how to prepare a colorkey object, andlegend=
knows how to draw arbitrary objects inside of plots, so we can combine the two to get what we want:Note:
colorkey=
's lack of support for an "inside" option appears to be less a design choice than just a matter of the package authors' not yet having gotten around to implementing the requisite code. As evidence of that, see the documentation forcolorkey=
in?lattice::levelplot
(to which one is directed by `?sp::spplot):Since the key is a grob of its own it is perfectly possible to extract it from the plot object and draw it separately where ever you please.