Build a multi-line text box in R

I am creating a multi-line layout using R, and it would be convenient to create a multi-line text box on one of the graphs.

I am familiar with the use of Sweave to combine images, text and R code, but for various reasons I need it to be a single page graph created in R. Thus, it is necessary to build a text field, and not use Latex mark-up.

Is there a function in an existing package that can do this? Otherwise, can anyone suggest a simple approach to handle this?

Consider this situation:

## 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 (ie 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)) 

Output from example

+7
source share
2 answers

Try splitTextGrob() from graphic book 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) 

enter image description here

(try resizing the window)

This approach is based on grid graphics, you can either

  • use the grid / ggplot2 / grid for graphs

  • use the gridBase package to place basic graphics in grid viewports

+6
source

Some other viewing options (you can configure the code in one of these functions) include the textplot function in the gplots package and addtable2plot in the plotrix package.

+2
source

All Articles