在水平图面板区域之外添加文本 [英] Add text out of levelplot panel area
问题描述
我想在levelplot的绘图区域之外添加文本.在下面的示例中,我需要在指定位置的某个地方输入文本.
I want to add text out of plot area in levelplot. In the following example, I need the text in somewhere in the pointed location.
library (raster)
library(rasterVis)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
levelplot(r)
我尝试了多行文字功能,但没有成功.有什么建议吗?
I tried mtext function with no success. Any suggestions?
mtext("text", side=3, line=0)
推荐答案
tldr;
您可以使用较低级别的网格图形功能对图进行注释.在这种情况下,请执行以下操作:
tldr;
You can annotate the plot using lower-level grid graphical functions. In this case, do something like:
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))
rasterVis 和其他基于 lattice 的程序包使用 grid 图形系统,而不是mtext()
组成部分的基本图形系统
rasterVis and other lattice-based packages use the grid graphical system, not the base graphical system of which mtext()
is a part.
在这里,我将使用 grid 来在视口左上角上方0.4行的位置添加文本(技术性 grid 术语) ),并在其中打印上边距图.
Here, using grid, is how I'd go about adding text at a position 0.4 lines above the upper-left corner of the viewport (a technical grid term) in which that upper margin plot is printed.
-
首先,找到相关视口的名称.
First, find the name of the relevant viewport.
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
的视口,看起来像是一个有前途的候选对象.如果要检查它是否正确,可以在其周围绘制一个矩形,如下所示(使用视口的完整路径):
A quick scan of the listing returned by grid.ls()
turns up a viewport named plot_01.legend.top.vp
, which looks like a promising candidate. If you'd like to check whether it's the correct one, you can plot a rectangle around it with something like the following (which uses the full path to the viewport):
grid.rect(vp = "plot_01.toplevel.vp::plot_01.legend.top.vp",
gp = gpar(col = "red"))
然后,使用 grid 的灵活的坐标系,将所需的文本放置在该视口的左上角上方.
Then, using grid's terrifically flexible coordinate system, place the desired text just above the upper left corner of that viewport.
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
这篇关于在水平图面板区域之外添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!