Sez*_*zen 7 r raster levelplot rastervis
实际上,这个问题包含针对相同行为的两个问题.
如何将文本(因每个面板而异)添加到面板区域中的固定位置?我知道panel.text并latticeExtra::layer
解决方案,但它使用绘图区域坐标添加文本.例如,我想在每个面板的右下角添加文本,即使它们的比例不同.
如何从levelplot面板区域添加文本?这里解释的方法要求levelplot有一个plot_01.legend.top.vp区域来添加我没有的文本,并且trellis之前绘制了对象.此外,我想在左下ylab图中添加文字.我ylab在这里用来说明行的含义但我需要第二个ylab来表示y轴值.我发现了这个问题的另一个
问题,但它不起作用.
上图是由raster::stack对象和rasterVis::levelplot方法创建的.我同意一个肮脏的解决方案,即使我更喜欢优雅的解决方案.尽管有上述问题,我仍然对其他使用的方法持开放态度levelplot.
R-sig-Geo目前正在讨论一个非常类似的问题,只需看看我在那里提供的解决方案即可。以下是相应的示例代码,可让您使用 fromlattice 在网格图的面板区域内部或外部添加自定义trellis.focus(..., clip.off = TRUE)文本注释。
library(rasterVis)
library(grid)
## sample data
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
s <- stack(r, r+500, r-500, r+200)
p <- levelplot(s, layout = c(2, 2), names.att = rep("", 4),
scales = list(y = list(rot = 90)))
## labels
cls <- c("col1", "col2")
rws <- c("row1", "row2")
png("~/rasterVis.png", width = 14, height = 16, units = "cm", res = 300L)
grid.newpage()
print(p, newpage = FALSE)
## loop over panels to be labelled (ie 1:3)
panels = trellis.currentLayout()
for (i in 1:3) {
# focus on current panel of interest and disable clipping
ids <- which(panels == i, arr.ind = TRUE)
trellis.focus("panel", ids[2], ids[1], clip.off = TRUE)
# add labels
if (i %in% c(1, 3)) {
if (i == 1) {
grid.text(cls[1], x = .5, y = 1.1) # add 'col1'
grid.text(rws[1], x = -.35, y = .5, rot = 90) # add 'row1'
} else {
grid.text(rws[2], x = -.35, y = .5, rot = 90) # add 'row2'
}
} else {
grid.text(cls[2], x = .5, y = 1.1) # add 'col2'
}
trellis.unfocus()
}
dev.off()
Run Code Online (Sandbox Code Playgroud)
您可以在这里找到更多信息: