[R] Mixing text and graphics in PDF

Ben Bolker ben at zoo.ufl.edu
Thu Nov 21 21:38:31 CET 2002


  Here's something I hacked up a while ago to put a text box onto a 
figure.  It uses a crude line-breaking algorithm.  Hope it helps.

  Ben Bolker

## think about: justification etc.
## FIXME: specifying x length (and allowing it to be in inches or
##  user parameters) would make putting text boxes on logarithmic
## scales much easier ...
## FIXME: resets par(adj)
textbox <- function(x,y,textlist,justify=TRUE,cex=1,
                    leading=0.5,
                    box=TRUE) {
  ## basic strategy: break textlist into words, add words until
  ## length is right
  par(adj=0)
  textstr <- paste(textlist,collapse=" ")
  words <- strsplit(textstr," ")[[1]]
  line.height <- strheight("hy",cex=cex)*(1+leading)
  x.len <- diff(x)
  y.pos <- y
  x.pos <- x[1]
  curword <- 1
  while (curword < length(words)) {
    curline <- ""
    ## add first word
    curline <- paste(curline,words[curword])
    curword <- curword+1
    while (x.pos+strwidth(paste(curline,words[curword]),cex=cex)<x[2]) {
      curline <- paste(curline,words[curword])
      curword <- curword+1
    }
    text(x.pos,y.pos,curline)
    y.pos <- y.pos - line.height
  }
  if (box) rect(x[1],y+line.height,x[2],y.pos)
}

On Thu, 21 Nov 2002, Wright, Kevin wrote:

> 
> Is there an easy way to "sink" text output to a pdf file using the pdf()
> device?
> 
> Here's an email that describes setting up empty graphics plots and then
> using the "text" command to put text on page:
> http://www.r-project.org/nocvs/mail/r-help/2002/5598.html
> 
> I'm just looking for something a little more polished -- automatic page
> breaks, line wrapping, etc.
> 
> 
> Kevin Wright, Research Scientist
> Pioneer Hi-Bred Int'l, x4054.
> 
> 
> 
> 
> This communication is for use by the intended recipient and contains 
> information that may be privileged, confidential or copyrighted under
> applicable law.  If you are not the intended recipient, you are hereby
> formally notified that any use, copying or distribution of this e-mail,
> in whole or in part, is strictly prohibited.  Please notify the sender
> by return e-mail and delete this e-mail from your system.  Unless
> explicitly and conspicuously designated as "E-Contract Intended",
> this e-mail does not constitute a contract offer, a contract amendment,
> or an acceptance of a contract offer.  This e-mail does not constitute
> a consent to the use of sender's contact information for direct marketing
> purposes or for transfers of data to third parties.
> 
>  Francais Deutsch Italiano  Espanol  Portugues  Japanese  Chinese  Korean
> 
>             http://www.DuPont.com/corp/email_disclaimer.html
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-- 
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list