Add legend in the outer margin

outer_legend(
  x = "bottom",
  y = NULL,
  doPar = TRUE,
  legend,
  bg = "white",
  box.col = "grey70",
  col,
  pt.bg = NULL,
  pch = 21,
  ncol = min(c(5, length(legend))),
  cex = 1.2,
  pt.cex = 1.4,
  ...
)

Arguments

x, y

position of the legend, using any method recognized by grDevices::xy.coords().

doPar

logical indicating whether to apply graphics::par() in order to place the legend in the outer margin. If doPar=FALSE and if no other method has been done to update the graphics::par() settings, the legend will be placed with the same mechanism used by graphics::legend().

legend

character vector, or expression, used to display each legend label.

bg

color used as the background of the legend box.

box.col

color used as the frame border color of the legend box.

col

character vector of colors used to color each entry in legend.

pt.bg

vector of colors used as the background color when pch is any value from 21 to 25. When pt.bg=NULL this value and col are adjusted so the outline is darker, via jamba::makeColorDarker(), with the fill color pt.bg using the original col value.

pch

integer or character vector of point shapes, as described in graphics::points().

ncol

integer number of columns to use for legend entries. By default ncol is the smaller of length(legend) and 5, which is intended for a legend placed at the bottom of the visualization.

cex

character expansion multiplier, used to size the legend text overall.

pt.cex

point expansion factor, used to size points when pch is not NULL.

...

additional arguments are passed to graphics::legend().

Details

This function is intended to place a legend outside the figure in the outer margin area. The motivating use case is to display a legend below multi-panel base R graphics plots, for example when using par("mfrow"=c(2,2)).

Examples

opar <- par(no.readonly=TRUE);
on.exit(par(opar));

par("mfrow"=c(2,2));
for (i in 1:4) {
   jamba::nullPlot(plotAreaTitle=paste("Panel", i));
}
require(jamba);
#> Loading required package: jamba
#> 
#> Attaching package: ‘jamba’
#> The following object is masked from ‘package:IRanges’:
#> 
#>     heads
outer_legend("bottom",
   legend=c("one", "two", "three"),
   col=colorjam::rainbowJam(3));