From Gregor.Gorjanc at bfro.uni-lj.si  Mon Dec 22 15:50:02 2008
From: Gregor.Gorjanc at bfro.uni-lj.si (Gorjanc Gregor)
Date: Mon, 22 Dec 2008 15:50:02 +0100
Subject: [Rd] Align legend title
Message-ID: <F189E18BBAA8B6479F618B9044CF4E9C68D7BCC5B8@REGULUS.bfro.uni-lj.si>

Dear R developers,

I needed to align the title of legend for some of my plots. If there is interest to include
this into base R, bellow are the examples and the relevent diffs.

Regards, Gregor

x <- 1:10
y1 <- 1.5 * x
y2 <- 2.0 * x
y <- cbind(y1, y2)

source(file=url("http://gregor.gorjanc.googlepages.com/legend.R"))

matplot(x, y, type="l", lty=c(1, 2))

## Center
legend("topleft", legend=c("a long text, a long text", "y = 2.0 * x"), lty=c(1, 2), title="Function")
legend("topright", legend=c("a shor text", ""), lty=c(1, 2), title="Function + some more text")

## Left
legend("left", legend=c("a long text, a long text, a long text", "y = 2.0 * x"), lty=c(1, 2), title="Function", title.adj=0)
legend("right", legend=c("a shor text", ""), lty=c(1, 2), title="Function + some more long text", title.adj=0)

## Right
legend("bottomleft", legend=c("a long text, a long text, a long text", "y = 2.0 * x"), lty=c(1, 2), title="Function", title.adj=1)
legend("bottomright", legend=c("a shor text", ""), lty=c(1, 2), title="Function + some more long text", title.adj=1)

http://gregor.gorjanc.googlepages.com/legend.R
http://gregor.gorjanc.googlepages.com/legend.Rd
http://gregor.gorjanc.googlepages.com/legend.R.diff
http://gregor.gorjanc.googlepages.com/legend.Rd.diff

Index: legend.R
===================================================================
--- legend.R    (revision 47286)
+++ legend.R    (working copy)
@@ -23,7 +23,7 @@
         text.width = NULL, text.col = par("col"),
         merge = do.lines && has.pch, trace = FALSE,
         plot = TRUE, ncol = 1, horiz = FALSE, title = NULL,
-        inset = 0, xpd, title.col = text.col)
+        inset = 0, xpd, title.col = text.col, title.adj = 0.5)
 {
     ## the 2nd arg may really be `legend'
     if(missing(legend) && !missing(y) &&
@@ -259,11 +259,11 @@

     xt <- xt + x.intersp * xchar
     if(plot) {
-       if (!is.null(title))
-            text2(left + w/2, top - ymax, labels = title,
-                  adj = c(0.5, 0), cex = cex, col = title.col)
+        if(!is.null(title))
+            text2(left + w/2 + ((w - tw) * (title.adj - 0.5)), top - ymax,
+                  labels = title, adj = c(0.5, 0), cex = cex, col = title.col)

-       text2(xt, yt, labels = legend, adj = adj, cex = cex, col = text.col)
+        text2(xt, yt, labels = legend, adj = adj, cex = cex, col = text.col)
     }
     invisible(list(rect = list(w = w, h = h, left = left, top = top),
                   text = list(x = xt, y = yt)))

Index: legend.Rd
===================================================================
--- legend.Rd   (revision 47286)
+++ legend.Rd   (working copy)
@@ -16,7 +16,7 @@
        adj = c(0, 0.5), text.width = NULL, text.col = par("col"),
        merge = do.lines && has.pch, trace = FALSE,
        plot = TRUE, ncol = 1, horiz = FALSE, title = NULL,
-       inset = 0, xpd, title.col = text.col)
+       inset = 0, xpd, title.col = text.col, title.adj = 0.5)
 }
 \description{
   This function can be used to add legends to plots.  Note that a call
@@ -89,6 +89,8 @@
   \item{xpd}{if supplied, a value of the graphical parameter \code{xpd}
     to be used while the legend is being drawn.}
   \item{title.col}{color for \code{title}.}
+  \item{title.adj}{justify the title, a value of 0 means left justified,
+    0.5 means centered and 1 means right justified.}
 }
 \details{
   Arguments \code{x, y, legend} are interpreted in a non-standard way to



[Rd] Align legend title
Gorjanc Gregor