[Rd] legend lwd - feature request (PR#7023)

ligges at statistik.uni-dortmund.de ligges at statistik.uni-dortmund.de
Fri Aug 20 15:17:11 CEST 2004


Lfo at psykiatri.aaa.dk wrote:

> (R1.9.1 Windows)
> 
> In legend( ) lwd have no effect on points - contrary to the behaviour
> in e.g. plot( ) and points( ). It would be nice to have an option pt.lwd
> affecting the line width of legend points (like pt.cex changes the
> magnification).
> 
> 
> An example (note the difference in line width between points in the
> plot and the legend):
> x <- 1:10 ; y <- rnorm(10,10,5) ; y2 <- rnorm(10,8,4)
> plot(x,y,bty="l",lwd=3,type="o",col=2,ylim=c(min(y,y2),max(y,y2)),cex=1.5)
> points(x,y2,lwd=3,lty=8,col=4,type="o",pch=2,cex=1.5)
> legend(10,max(y,y2),legend=c("Method 1","Method
> 2"),col=c(2,4),lty=c(1,8),pch=c(1,2),
>     xjust=1,yjust=1,pt.cex=1.5,lwd=3)
> 
> Workaround (thanks to Peter Dalgaard):
> x <- 1:10 ; y <- rnorm(10,10,5) ; y2 <- rnorm(10,8,4)
> plot(x,y,bty="l",lwd=3,type="o",col=2,ylim=c(min(y,y2),max(y,y2)),cex=1.5)
> points(x,y2,lwd=3,lty=8,col=4,type="o",pch=2,cex=1.5)
> legend(10,max(y,y2),legend=c("Method 1","Method
> 2"),col=c(0,0),lty=c(1,8),pch=c(1,2),
>     xjust=1,yjust=1,pt.cex=1.5,lwd=0,text.col=0)
> pp <- par(lwd=3)
> legend(10,max(y,y2),legend=c("Method 1","Method 2"),
>     col=c(2,4),lty=c(1,8),pch=c(1,2), 
>     xjust=1,yjust=1,pt.cex=1.5,lwd=3,bty="n")
> par(pp)


The quick patch at the end of this message will add an argument pt.lwd 
to legend(). Note that this argument only accepts one value for all 
point, because vectorized widths are not supported by points() and friends.

There is another point to consider for the long term:
In order to restrict the possibly huge amount of possible values for 
legend() and make it legend() more flexible (I know, it already is very 
flexible), one might want to invent new arguments like point.args, 
line.args, text.args that accept lists of arguments to be passed to the 
corresponding underlying functions. However, that would require a 
redesign of legend() - and would certainly break some code ...

Uwe Ligges


Patch, diff'ed against r-devel (rev. 30796):


--- y:\recent\R\src\library\graphics\R\legend.R
+++ legend.R    2004-08-20 15:08:13.000000000 +0200
@@ -1,7 +1,7 @@
  legend <-
  function(x, y = NULL, legend, fill=NULL, col = "black", lty, lwd, pch,
-     angle = NULL, density = NULL, bty = "o",
-     bg = par("bg"), pt.bg = NA, cex = 1, pt.cex = cex,
+     angle = NULL, density = NULL, bty = "o", bg = par("bg"),
+     pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = par("lwd"),
       xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1, adj = c(0, 0.5),
       text.width = NULL, text.col = par("col"),
           merge = do.lines && has.pch, trace = FALSE,
@@ -178,8 +178,8 @@
      if(trace)
          catn("  points2(", x1,",", y1,", pch=", pch[ok],", ...)")
      if(plot)
-        points2(x1, y1,
-                    pch = pch[ok], col= col[ok], cex= pt.cex[ok], bg= 
pt.bg[ok])
+        points2(x1, y1, pch = pch[ok], col = col[ok], cex = pt.cex[ok],
+                bg = pt.bg[ok], lwd = pt.lwd)
      if (!merge) xt <- xt + dx.pch
      }



--- y:\recent\R\src\library\graphics\man\legend.Rd
+++ legend.Rd   2004-08-20 15:13:41.000000000 +0200
@@ -4,8 +4,8 @@
  \usage{
  legend(x, y = NULL, legend, fill = NULL, col = "black", lty, lwd, pch,
         angle = NULL, density = NULL, bty = "o", bg = par("bg"),
-       pt.bg = NA, cex = 1, pt.cex = cex, xjust = 0, yjust = 1,
-       x.intersp = 1, y.intersp = 1, adj = c(0, 0.5),
+       pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = par("lwd"), xjust = 0,
+       yjust = 1, x.intersp = 1, y.intersp = 1, 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)
@@ -43,6 +43,7 @@
    \item{cex}{character expansion factor \bold{relative} to current
      \code{par("cex")}.}
    \item{pt.cex}{expansion factor(s) for the points.}
+  \item{pt.lwd}{line width for the points.}
    \item{xjust}{how the legend is to be justified relative to the legend
      x location.  A value of 0 means left justified, 0.5 means centered
      and 1 means right justified.}



More information about the R-devel mailing list