[R] help combining mtext and strwrap?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 29 20:05:41 CET 2005


On Tue, 29 Nov 2005, Jake Michaelson wrote:

> I've got some image plots on which I'd like to include some gene information
> (in the margins using mtext).  Unfortunately, the description is rather long
> and will need to be wrapped to fit on several lines.  From what I know about
> mtext, it's really only meant for single-line labels, not paragraphs.
>
> Here's some sample code of the idea I'm trying to accomplish:
>
> notes=c("Repressible alkaline phosphatase, a glycoprotein localized to the
> vacuole; regulated by levels of inorganic phosphate and by a system
> consisting of Pho4p, Pho9p, Pho80p, Pho81p and Pho85p; dephosphorylates
> phosphotyrosyl peptides")
>
> par(mar=c(10,3,10,3))
>
> image(as.matrix(c(1,2,3,4,5)))
>
> mtext(strwrap(notes, width=60), line=5, side=3)
>
> ..as you can see, the long text plots over itself and doesn't wrap.  Does
> anyone know how to include a paragraph in the margins?

The clue is 'line = 5'.  You asked for this to be plotted on one line.
You can strsplit the result and plot the lines of text separately. E.g.

par(mar=c(10,3,10,3))
image(as.matrix(c(1,2,3,4,5)))
res <- strsplit( strwrap(notes, width=60), "\n")
for(i in seq(along=res)) mtext(res[[i]], line = 10-i, side=3)

Or just use title(), which seems to be what you are trying to emulate.

title(strwrap(notes, width=60), cex.main=1, font.main=1, line=5)


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list