[Rd] legend -- one more try

bolker@zoo.ufl.edu bolker@zoo.ufl.edu
Thu, 19 Oct 2000 16:34:53 -0400 (EDT)


  Dermot MacSweeney pointed out to me that after my "fix" of legend(),
points were no longer coming out placed in the middle of the lines, but at
the right-hand edge.  It turns out that naively swapping the order of
point-drawing and line-drawing also messes up the bookkeeping that
legend() does on the current x-location.  Here's my patch, which fixes
that bookkeeping (and incidentally substitutes "seg.len" for 2 where it
refers to the length of the drawn line segments in units of x.char; one
could make seg.len user-settable, I don't know if it's worth the bother).

  This time the patch is against the current legend() in R-devel.

*** new-legend.R	Thu Oct 19 16:25:36 2000
--- R-devel/src/library/base/R/legend.R	Wed Sep  6 04:11:16 2000
***************
*** 141,171 ****
      }
      if(has.pch || do.lines)
          col <- rep(col,length.out=n.leg)
-     
-     if (do.lines) {                     #- draw lines ---------------------
-         seg.len <- 2 # length of drawn segment, in xchar units
- 	ok.l <- if(missing(lty)) { lty <- 1; TRUE } else lty > 0
- 	if(missing(lwd)) lwd <- par("lwd")
- 	lty <- rep(lty, length.out = n.leg)
- 	lwd <- rep(lwd, length.out = n.leg)
- 	if(trace)
- 	    catn("  segments2(",xt[ok.l] + x.off*xchar ,",", yt[ok.l],
-                  ", dx=",seg.len*xchar,", dy=0, ...)", sep="")
- 	segments2(xt[ok.l] + x.off*xchar, yt[ok.l], dx= seg.len*xchar, dy=0,
- 		  lty = lty[ok.l], lwd = lwd[ok.l], col = col[ok.l])
- 	# if (!merge)
-         xt <- xt + (seg.len+x.off) * xchar
-     }
      if (has.pch) {                      #- draw points -------------------
  	pch   <- rep(pch, length.out=n.leg)
  	pt.bg <- rep(pt.bg, length.out=n.leg)
  	ok <- is.character(pch) | pch >= 0
! 	x1 <- (if(merge) xt-(seg.len/2)*xchar else xt)[ok]
  	y1 <- yt[ok]
  	if(trace)
  	    catn("  points2(", x1,",", y1,", pch=", pch[ok],"...)")
  	points2(x1, y1, pch=pch[ok], col=col[ok], cex=cex, bg = pt.bg[ok])
  	if (!merge) xt <- xt + dx.pch
      }
  
      xt <- xt + x.intersp * xchar
--- 141,169 ----
      }
      if(has.pch || do.lines)
          col <- rep(col,length.out=n.leg)
      if (has.pch) {                      #- draw points -------------------
  	pch   <- rep(pch, length.out=n.leg)
  	pt.bg <- rep(pt.bg, length.out=n.leg)
  	ok <- is.character(pch) | pch >= 0
! 	x1 <- (if(merge) xt + 0.2*xchar else xt)[ok]
  	y1 <- yt[ok]
  	if(trace)
  	    catn("  points2(", x1,",", y1,", pch=", pch[ok],"...)")
  	points2(x1, y1, pch=pch[ok], col=col[ok], cex=cex, bg = pt.bg[ok])
  	if (!merge) xt <- xt + dx.pch
+     }
+     if (do.lines) {                     #- draw lines ---------------------
+ 	ok.l <- if(missing(lty)) { lty <- 1; TRUE } else lty > 0
+ 	if(missing(lwd)) lwd <- par("lwd")
+ 	lty <- rep(lty, length.out = n.leg)
+ 	lwd <- rep(lwd, length.out = n.leg)
+ 	if(trace)
+ 	    catn("  segments2(",xt[ok.l] + x.off*xchar ,",", yt[ok.l],
+                  ", dx=",2*xchar,", dy=0, ...)", sep="")
+ 	segments2(xt[ok.l] + x.off*xchar, yt[ok.l], dx= 2*xchar, dy=0,
+ 		  lty = lty[ok.l], lwd = lwd[ok.l], col = col[ok.l])
+ 	## if (!merge)
+         xt <- xt + (2+x.off) * xchar
      }
  
      xt <- xt + x.intersp * xchar

-- 
318 Carr Hall                                bolker@zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._