【问题标题】:How to add text to a specific/fixed location in rasterVis levelplot如何将文本添加到 rasterVis levelplot 中的特定/固定位置
【发布时间】:2017-03-24 18:59:03
【问题描述】:

事实上,这个问题由两个针对相同行为的问题组成。

  1. 如何将文本(因每个面板而异)添加到 面板区?我知道panel.textlatticeExtra::layer solution 但它使用绘图区域坐标添加文本。为了 例如,我想在每个面板的右下角添加文本 即使他们的尺度不同。

  2. 如何在 levelplot 面板区域之外添加文本? Method explained here 要求 levelplot 有一个 plot_01.legend.top.vp 区域 添加我没有的文本并绘制​​了trellis 对象 前。此外,我想在ylab 的左侧添加文本,如 下图。我在这里使用ylab 来说明行的含义,但我 需要第二个表示 y 轴值的 ylab。我找到了另一个 question 解决这个问题,但它不起作用。

上面的图是由raster::stack 对象和rasterVis::levelplot 方法创建的。即使我更喜欢优雅的解决方案,我也同意一个肮脏的解决方案。此外,尽管有上述问题,我对使用 levelplot 的其他方法持开放态度。

【问题讨论】:

  • 不确定是否有帮助,但levelplot 中的names.attr 允许您为每个面板指定名称。例如。 names=c("One","Two","Three")levelplot(yourstack, names.attr=names)

标签: r raster levelplot rastervis


【解决方案1】:

R-sig-Geo 目前正在讨论一个非常相似的问题,只需看看我在那里提供的解决方案。下面是相应的示例代码,可让您使用 lattice 中的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()

您可以在这里找到更多信息:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    相关资源
    最近更新 更多