splitString <- function(text) { strings <- strsplit(text, " ")[[1]] if (length(strings) < 2) return(text) newstring <- strings[1] linewidth <- stringWidth(newstring) gapwidth <- stringWidth(" ") availwidth <- convertWidth(unit(1, "npc"), "in", valueOnly=TRUE) for (i in 2:length(strings)) { width <- stringWidth(strings[i]) if (convertWidth(linewidth + gapwidth + width, "in", valueOnly=TRUE) < availwidth) { sep <- " " linewidth <- linewidth + gapwidth + width } else { sep <- "\n" linewidth <- width } newstring <- paste(newstring, strings[i], sep=sep) } newstring } text <- "The quick brown fox jumps over the lazy dog." grid.text(splitString(text), x=0, y=1, just=c("left", "top")) splitText <- splitTextGrob(text, name="splitText") grid.draw(splitText) pushViewport(viewport(layout=grid.layout(2, 2))) pushViewport(viewport(layout.pos.col=1)) pushViewport(viewport(width=0.5, height=0.9)) grid.rect(gp=gpar(col="gray")) text <- "The quick brown fox jumps over the lazy dog." grid.text(splitString(text), x=0, y=1, just=c("left", "top")) popViewport(2) pushViewport(viewport(layout.pos.col=2, layout.pos.row=1)) pushViewport(viewport(height=0.8)) grid.rect(gp=gpar(col="gray")) splitText <- splitTextGrob(text, name="splitText") grid.draw(splitText) popViewport(2) pushViewport(viewport(layout.pos.col=2, layout.pos.row=2)) pushViewport(viewport(height=0.8)) grid.rect(gp=gpar(col="gray")) grid.draw(editGrob(splitText, gp=gpar(cex=1.5))) popViewport(2) popViewport()