R/Adding letters

From QERM Wiki
Jump to: navigation, search

Adding letters to identify sub-figures

Various journals want little letters in the corner. Here's some code to do it:

makeletter <- function(letter='a',position='topleft',xfrac=0.05,yfrac=0.05,...){
  # puts a letter into the corner of a panel to match CJFAS requirements
  # very similar to something Trevor Branch (and who knows how many others)
  # created independently
  if(letter%in%1:9) letter <- letters[letter]
  p <- par('usr')
  if(position=='topleft') x <- p[1] + xfrac*diff(p[1:2])
  if(position=='topright') x <- p[2] - xfrac*diff(p[1:2])
  y <- p[4] - yfrac*diff(p[3:4])
  text(x=x,y=y,paste('(',letter,')',sep=''),...)
}

Apply using something like:

par(mfrow=c(2,2))
for(i in 1:4){plot(0); makeletter(i)}

Another Version

Here's another, one line version - a shortcut based on the legend function:

 MakeLetter <- function(a, where="topleft", cex=2)
     legend(where, pt.cex=0, bty="n", title=a, cex=cex, legend=NA)

The "where" options are "topleft", "top", "topright", "left", "center", "right", "bottomleft", "bottom", "bottomright" ... i.e. all of the "legend" options. "a" can be any character string.

Personal tools