TMS*_*TMS 8 gis r spatial lattice r-sp
是否可以将spplot(spplot polygons)图例放在地图的左下角,就像这样?
我能得到的最接近的是(我没有发布我的数据,我只是使用示例数据,所以在这种情况下,尝试将图例放在地图的左上角):
data(meuse.grid)
gridded(meuse.grid)=~x+y
spplot(meuse.grid[,'dist'],
colorkey = list(space = "left", height = 0.5)
)
Run Code Online (Sandbox Code Playgroud)
但是图例位于页面中间,位于地图之外.不幸的是,colorkey
论证不支持"bottomleft",或者x,y或者角落参数(参见参考资料?levelplot
).我也尝试使用key.space
参数,但它似乎只在绘图时起作用,SpatialPoints*
但它似乎被忽略 SpatialPolygons*
(或者像上面例子中的SpatialPixelsDataFrame).
Bac*_*lin 10
由于键是它自己的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)
Run Code Online (Sandbox Code Playgroud)
这里的复杂问题是,尽管colorkey=
参数与参数非常相似legend=
,但它并不完全支持完整的定位选项legend=
.而图例可以直接放置在"left"
,"right"
,"top"
,"bottom"
,和"inside"
的情节,colorkey=
仅支持那些的第4位.
一个相当干净的解决方法是提取一次调用准备的colorkey参数列表spplot()
,并spplot()
通过其legend=
参数将其传递给第二个调用.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))
Run Code Online (Sandbox Code Playgroud)
注意: colorkey=
缺乏对"内部"选项的支持似乎不是设计选择,而只是包装作者尚未实现必要代码的问题.作为证据,请参阅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
Run Code Online (Sandbox Code Playgroud)