简体   繁体   English

在地图中自定放置spplot图例

[英]Custom placement of spplot legend in the map

Is it possible to place the spplot (spplot polygons) legend within the map, in lower left corner, like this? 是否可以将spplot(spplot polygons)图例放在地图的左下角,就像这样?

在此输入图像描述

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 ). 不幸的是, colorkey参数不支持“bottomleft”或x,y或corner参数(参见?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). 我也尝试使用key.space参数,但它似乎只在绘制SpatialPoints*时才起作用,但似乎忽略了SpatialPolygons* (或上面例子中的SpatialPixelsDataFrame)。

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. 由于键是它自己的grob,因此完全可以从绘图对象中提取它并在任何地方单独绘制它。

library(grid)

#  Separate plot and key
s <- spplot(meuse.grid[,'dist'],
    colorkey = list(space = "left", height = 0.5)
)
key <- draw.colorkey(s$legend[[1]]$args$key)
s$legend <- NULL # Otherwise we'd get two keys

# Modify key
key$framevp$x <- unit(0.15, "npc")
key$framevp$y <- unit(0.68, "npc")

# Plot
s
grid.draw(key)

在此输入图像描述

The complicating issue here is that, although the colorkey= argument is treated very much like the legend= argument, it doesn't quite support the full suite of positioning options that legend= does. 这里的复杂问题是,尽管colorkey=参数与legend=参数非常相似,但它并不完全支持legend=所做的全套定位选项。 Whereas legends can be directly placed at "left" , "right" , "top" , "bottom" , and "inside" the plot, colorkey= only supports the first four of those. 传说可以直接放置在情节的"left""right""top""bottom""inside" ,而colorkey=仅支持前四个。

A fairly clean workaround is to extract the colorkey argument list prepared by one call to spplot() , and to pass that in to a second spplot() call via its legend= argument. 一个相当干净的解决方法是提取一次调用spplot()准备的colorkey参数列表,并通过其legend=参数将其传递给第二个spplot()调用。 colorkey= "knows" how to prepare a colorkey object, and legend= knows how to draw arbitrary objects inside of plots, so we can combine the two to get what we want: colorkey=colorkey= ”如何准备一个colorkey对象, legend=知道如何在绘图中绘制任意对象,所以我们可以将两者结合起来得到我们想要的东西:

library(sp)
library(grid)
library(lattice)
data(meuse.grid)
gridded(meuse.grid)=~x+y

## Call spplot() once as a way to construct a list of arguments
## to draw.color.key
SP <- spplot(meuse.grid[,'dist'],
    colorkey = list(space = "left", height = 0.4)
)
args <- SP$legend$left$args$key

## Prepare list of arguments needed by `legend=` argument (as described in ?xyplot)
legendArgs <- list(fun = draw.colorkey,
                   args = list(key = args),
                   corner = c(0.05,.75))

## Call spplot() again, this time passing in to legend the arguments
## needed to print a color key
spplot(meuse.grid[,'dist'], colorkey = FALSE,
       legend = list(inside = legendArgs))

在此输入图像描述

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. 注意: colorkey=缺乏对“内部”选项的支持似乎不是一个设计选择,而只是包装作者尚未实现必要代码的问题。 As evidence of that, see the documentation for colorkey= in ?lattice::levelplot (to which one is directed by `?sp::spplot): 作为证据,请参阅colorkey= in ?lattice::levelplot (由??sp :: spplot指向哪一个):

colorkey: logical specifying whether a color key is to be drawn
          alongside the plot, or a list describing the color key. The
          list may contain the following components:

          ‘space’: location of the colorkey, can be one of ‘"left"’,
              ‘"right"’, ‘"top"’ and ‘"bottom"’.  Defaults to
              ‘"right"’.

          ‘x’, ‘y’: location, currently unused

          ‘corner’: Interacts with x, y; currently unimplemented

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM