在R中绘制一个多行文本框

dig*_*aps 7 r

我正在使用R创建一个多绘图布局图,并且可以方便地在其中一个图中创建多行文本框.

我熟悉使用Sweave来组合图像,文本和R代码,但由于各种原因,我需要将其作为R中生成的单页图.因此需要绘制文本框而不是使用Latex标记.

现有包中是否有可以执行此操作的功能?如果做不到这一点,有人会建议一个简单的方法来处理这个问题

考虑这种情况:

## Specify the dimensions of the plot
## that we require
win.graph(8,4)

## Two panel layout
layout(matrix(1:2, 1, 2))
par(mar=c(0,0,0,0))

## Left panel shows picture
plot(rep(1:10, each=10), rep(1:10, times=10), 
col=rainbow(100), pch=20, cex=5)


## Right panel discusses the data
plot.default(c(0,100), c(0,100), type="n", axes=FALSE,
ylab="", xlab="")

text(20, 30, "It would be great if this text box\n
could handle word wrap, and ideally given the size\n
of the font (i.e. the cex parameter) and the area\n
of the plot it should be able to do this dynamically,\n
without requiring the newline characters I am\n
manually inserting.  Ability to control the line\n
height would also be nice.\n
Am I dreaming?", cex=0.75, adj=c(0,0))
Run Code Online (Sandbox Code Playgroud)

从示例输出

bap*_*ste 6

试试splitTextGrob()R图形书

text = paste(capture.output(licence()),collapse=" ")
library(RGraphics)
library(gridExtra)

grid.arrange(rectGrob(), splitTextGrob(text), ncol=2)

d <- expand.grid(seq(0.1, 0.9, length=10), seq(0.1, 0.9, length=10))
grid.arrange(pointsGrob(d[, 2], d[, 1], pch=21, 
 gp=gpar(fill=rainbow(100))), splitTextGrob(text), ncol=2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

(尝试调整窗口大小)

这种方法基于网格图形,你也可以

  • 使用lattice/ggplot2/grid作为图

  • 使用该gridBase包将基本图形放置在网格视口中