代码之家  ›  专栏  ›  技术社区  ›  Geo-sp

在水平图面板区域外添加文本

  •  5
  • Geo-sp  · 技术社区  · 9 年前

    我想在levelplot的绘图区域外添加文本。在下面的示例中,我需要将文本放在指向的位置。

    library (raster)
    library(rasterVis)
    
    f <- system.file("external/test.grd", package="raster")
    r <- raster(f)
    levelplot(r) 
    

    我尝试了多行文字功能,但没有成功。有什么建议吗?

    mtext("text", side=3, line=0)
    

    enter image description here

    1 回复  |  直到 9 年前
        1
  •  9
  •   Josh O'Brien    9 年前

    太长,读不下去了

    可以使用较低级别对绘图进行注释 网格 图形功能。在这种情况下,请执行以下操作:

    library(grid)
    seekViewport("plot_01.legend.top.vp")
    grid.text("Hello", x=0, y=unit(1,"npc") + unit(0.4, "lines"), just=c("left", "bottom"),
              gp=gpar(cex=1.6))
    

    光栅Vis 和其他 格子木架 -基于的包使用 网格 图形系统,而不是其基本图形系统 mtext() 是一部分。

    这里,使用 网格 ,是我在视口左上角上方0.4行的位置添加文本的方式(技术 网格 术语),其中打印上边距图。

    • 首先,找到相关视口的名称。

      library(grid)
      levelplot(r)
      grid.ls(viewport=TRUE, grobs=FALSE)  ## Prints out a listing of all viewports in plot
      

      快速扫描返回的列表 grid.ls() 打开名为 plot_01.legend.top.vp ,看起来很有前途。如果您想检查它是否正确,可以围绕它绘制一个矩形,如下所示(使用视口的完整路径):

      grid.rect(vp = "plot_01.toplevel.vp::plot_01.legend.top.vp",
                gp = gpar(col = "red"))
      
    • 然后,使用 网格 非常灵活的坐标系,将所需的文本放在视口左上角的正上方。

      ll <- seekViewport("plot_01.legend.top.vp")
      grid.text("Hello", x = 0, y = unit(1,"npc") + unit(0.4, "lines"), 
                just = c("left", "bottom"),
                gp = gpar(cex=1.6))
      upViewport(ll)  ## steps back up to viewport from which seekViewport was called
      

    enter image description here