From timhesterberg at gmail.com  Fri Nov  7 22:35:12 2008
From: timhesterberg at gmail.com (timhesterberg at gmail.com)
Date: Fri,  7 Nov 2008 22:35:12 +0100 (CET)
Subject: [Rd] barplot can put legend in wrong place,
	request option to override that (PR#13265)
Message-ID: <20081107213513.00677283417B@mail.pubhealth.ku.dk>

This in an enhancement requestion, to add the capability to pass
arguments through barplot() to legend().  I've created a version that
does this.

# Run this code; notice that the legend
# is superposed over one bar:
barplot(height =
        cbind(x = c(465,91) / 465 * 100,
              y = c(840,200) / 840 * 100,
              z = c(37,17) / 37 * 100),
        beside = FALSE,
        width = c(465, 840, 37),
        col = c(1, 2),
        legend.text = c("A", "B"))

# It would be nice to move the legend to
# the top left, where there is room.
# barplot() doesn't support this.
# Below are diffs for a patched
# version that adds an 'args.legend' argument, e.g.
barplot(height =
        cbind(x = c(465,91) / 465 * 100,
              y = c(840,200) / 840 * 100,
              z = c(37,17) / 37 * 100),
        beside = FALSE,
        width = c(465, 840, 37),
        col = c(1, 2),
        legend.text = c("A", "B"),
        args.legend = list(x = "topleft"))

# Check that some of the other arguments are passed without conflict:
barplot(height =
        cbind(x = c(465,91) / 465 * 100,
              y = c(840,200) / 840 * 100,
              z = c(37,17) / 37 * 100),
        beside = FALSE,
        width = c(465, 840, 37),
        col = c(1, 2),
        legend.text = c("A", "B"),
        args.legend = list(x = 35, y=140, xjust=0, yjust=.5, lty=2))


$ diff -c barplot.R barplot2.R
*** barplot.R    Mon Aug 11 02:51:52 2008
--- barplot2.R    Fri Nov  7 11:52:00 2008
***************
*** 25,31 ****
       xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
       axes = TRUE, axisnames = TRUE,
       cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
!      inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0, add = FALSE,
...)
  {
      if (!missing(inside)) .NotYetUsed("inside", error = FALSE)# -> help(.)

--- 25,32 ----
       xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
       axes = TRUE, axisnames = TRUE,
       cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
!      inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0, add = FALSE,
!          args.legend = NULL, ...)
  {
      if (!missing(inside)) .NotYetUsed("inside", error = FALSE)# -> help(.)

***************
*** 176,184 ****
          angle <- rev(angle)
          }
          xy <- par("usr")
!         legend(xy[2] - xinch(0.1), xy[4] - yinch(0.1),
!            legend = legend.text, angle = angle, density = density,
!            fill = legend.col, xjust = 1, yjust = 1)
      }
      title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...)
      if(axes) axis(if(horiz) 1 else 2, cex.axis = cex.axis, ...)
--- 177,197 ----
          angle <- rev(angle)
          }
          xy <- par("usr")
!             if(is.null(args.legend)) {
!               legend(xy[2] - xinch(0.1), xy[4] - yinch(0.1),
!                      legend = legend.text, angle = angle, density =
density,
!                      fill = legend.col, xjust = 1, yjust = 1)
!             } else {
!               args.legend1 <- list(x = xy[2] - xinch(0.1),
!                                    y = xy[4] - yinch(0.1),
!                                    legend = legend.text,
!                                    angle = angle,
!                                    density = density,
!                                    fill = legend.col,
!                                    xjust = 1, yjust = 1)
!               args.legend1[names(args.legend)] <- args.legend
!               do.call("legend", args.legend1)
!             }
      }
      title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...)
      if(axes) axis(if(horiz) 1 else 2, cex.axis = cex.axis, ...)
$ diff -c barplot.Rd barplot2.Rd
*** barplot.Rd    Mon Sep 24 19:05:10 2007
--- barplot2.Rd    Fri Nov  7 13:22:22 2008
***************
*** 22,28 ****
          axes = TRUE, axisnames = TRUE,
          cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
          inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
!         add = FALSE, \dots)
  }
  \arguments{
    \item{height}{either a vector or matrix of values describing the
--- 22,28 ----
          axes = TRUE, axisnames = TRUE,
          cex.axis = par("cex.axis"), cex.names = par("cex.axis"),
          inside = TRUE, plot = TRUE, axis.lty = 0, offset = 0,
!         add = FALSE, args.legend = NULL, \dots)
  }
  \arguments{
    \item{height}{either a vector or matrix of values describing the
***************
*** 105,110 ****
--- 105,113 ----
      relative to the x axis.}
    \item{add}{logical specifying if bars should be added to an already
      existing plot; defaults to \code{FALSE}.}
+   \item{args.legend}{list of additional arguments to pass to
+     \code{\link{legend()}}; names of the list are used as argument
+     names.  Only used if \code{legend.text} is supplied.}
    \item{\dots}{arguments to be passed to/from other methods.  For the
      default method these can include further arguments (such as
      \code{axes}, \code{asp} and \code{main}) and graphical
rocket at rocket1:~/R/bugs/barplot$



--please do not edit the information below--

Version:
 platform = i386-apple-darwin8.11.1
 arch = i386
 os = darwin8.11.1
 system = i386, darwin8.11.1
 status =
 major = 2
 minor = 8.0
 year = 2008
 month = 10
 day = 20
 svn rev = 46754
 language = R
 version.string = R version 2.8.0 (2008-10-20)

Locale:
C

Search Path:
 .GlobalEnv, package:stats, package:graphics, package:grDevices,
package:utils, package:datasets, package:methods, Autoloads, package:base

	[[alternative HTML version deleted]]



[Rd] barplot can put legend in wrong place, request option to override that (PR#13265)
timhesterberg at gmail.com