[Rd] Wishlist: specify the border color of boxes in legend() (PR#13913)

Frederic.Schutz at isb-sib.ch Frederic.Schutz at isb-sib.ch
Thu Aug 27 13:25:16 CEST 2009


This is a multi-part message in MIME format.
--------------040007030503070307040509
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

I could not find a way to specify the border color of the boxes drawn in
a legend, so that the legend can match exactly the colors of the actual
plot (e.g. in the case of two superimposed histograms which have
different shading and different borders).

Indeed, the legend function seems to hard code the color "black" for the
borders in this call:

         rect2(left = xt, top = yt + ybox/2, dx = xbox, dy = ybox,
               col = fill, density = density, angle = angle,
                border = "black")

I worked around this by adding a "border" argument to the function
(which defaults to "black"), and changing the call above. It works for
me; I have looked quickly at the rest of the function to see if there
would be a side effect and did not see any (but I haven't checked
carefully).

This could be a useful improvement of the legend() function; in case the
change described above looks usable, I attach two simple diff files (for
the function itself and the manual page).

Cheers,

Frédéric

--------------040007030503070307040509
Content-Type: text/x-patch;
 name="legend.R.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="legend.R.diff"

--- legend.R~	2009-03-20 00:05:18.000000000 +0100
+++ legend.R	2009-08-27 11:52:02.000000000 +0200
@@ -15,8 +15,8 @@
 #  http://www.r-project.org/Licenses/
 
 legend <-
-function(x, y = NULL, legend, fill=NULL, col = par("col"), lty, lwd, pch,
-	 angle = 45, density = NULL, bty = "o", bg = par("bg"),
+function(x, y = NULL, legend, fill=NULL, col = par("col"), border="black",
+         lty, lwd, pch, angle = 45, density = NULL, bty = "o", bg = par("bg"),
          box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),
 	 pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,
 	 xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1, adj = c(0, 0.5),
@@ -217,7 +217,7 @@
 	    fill <- rep(fill, length.out = n.leg)
 	    rect2(left = xt, top=yt+ybox/2, dx = xbox, dy = ybox,
 		  col = fill,
-		  density = density, angle = angle, border = "black")
+		  density = density, angle = angle, border = border)
 	}
 	xt <- xt + dx.fill
     }

--------------040007030503070307040509
Content-Type: text/x-patch;
 name="legend.Rd.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="legend.Rd.diff"

--- legend.Rd~	2009-03-20 00:05:18.000000000 +0100
+++ legend.Rd	2009-08-27 11:57:28.000000000 +0200
@@ -8,7 +8,7 @@
 \title{Add Legends to Plots}
 \usage{
 legend(x, y = NULL, legend, fill = NULL, col = par("col"),
-       lty, lwd, pch,
+       border="black", lty, lwd, pch,
        angle = 45, density = NULL, bty = "o", bg = par("bg"),
        box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),
        pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,
@@ -34,6 +34,8 @@
     with the specified colors (or shaded in the specified colors)
     to appear beside the legend text.}
   \item{col}{the color of points or lines appearing in the legend.}
+  \item{border}{the border color for the boxes (used only if
+    \code{fill} is specified).}
   \item{lty, lwd}{the line types and widths for lines appearing in the
     legend.  One of these two \emph{must} be specified for line drawing.}
   \item{pch}{the plotting symbols appearing in the legend, either as

--------------040007030503070307040509--



More information about the R-devel mailing list