Daniel Sabanés Bové
2007-Aug-15 14:32 UTC
[R] shorthand for plotmath-annotation in certain font
Dear useRs, I wanted to have a function that allows me to get the font.lab-font, especially italic font, in plotmath-annotation. Consulting the R-Help-list did not reveal a solution, but other requests, so I wrote a little function, which I would like to share: ## code begin math <- function (..., font = par ("font.lab")) { ## unpack dot-dot-dot argument dots <- as.list (substitute (...[])) dots <- dots[2:(length (dots) - 1)] ## font extension font <- switch (font, "plain", "bold", "italic", "bolditalic") dots <- paste (font, "(", dots, ")") ## parse and build expression do.call ("expression", as.list (parse (text = dots))) } ## code end Using this function, you can abbreviate the annotation work, a small example: ## code begin r <- rexp (100) theta <- runif (100, 0, 2*pi) group <- sample (1:4, 100, replace = TRUE) par (font.lab = 4) plot (r * cos (theta), r * sin (theta), xlab = math (r %*% cos (theta)), ylab = math (r %*% sin (theta)), pch = group) legend ("topleft", title = "group", legend = math (15 * alpha, 37 * beta, gamma + v[1], delta %/% d), pch = 1:4) ## code end Note that cos and sin are no "built-in" functions that plotmath can recognize, they are thus bolditalic as well. Using font = 3 meaning "italic" and using the Computer Math Symbol Adobe-Symbol-encoded fonts (cmsyase) developed by Paul Murrell (see http://www.stat.auckland.ac.nz/~paul/R/CM/CMR.html), you can produce annotation that resembles that of the gold-standard LaTeX quite well. regards, Daniel - who is not subscribed to the list, please send direct answers to my email address as well as to the list, thank you.