Adjust axis label margins to accommodate axis labels
Arguments
- x
charactervector of axis labels- margin
integervalue indicating which margin to adjust, using the order bygraphics::par("mar"), 1=bottom, 2=left, 3=top, 4=right.- maxFig
numericfraction less than 1, indicating the maximum size of margin relative to the figure size. Setting margins too large results in an error otherwise.- cex
numericor NULL, defaultgraphics::par("cex"), used as a convenience withcex * cex.axispassed tographics::strwidth(). However,graphics::axis()itself should usecex.axiswhen adjusting axis label font size.- cex.axis
numeric, defaultgraphics::par("cex.axis")to define the axis label font size.- prefix
characterstring to add whitespace around the axis label in order to add a "buffer" of whitespace.- ...
additional parameters are ignored.
Value
list named "mai" suitable for use in graphics::par()
to adjust margin size using in inches.
Details
This function takes a vector of axis labels, and the margin where they
will be used, and adjusts the relevant axis margin to accomodate the
label size, up to a maximum fraction of the figure size as defined by
maxFig.
Labels are assumed to be perpendicular to the axis, for example
argument las=2 when using graphics::text().
Note this function does not render labels in the figure, and therefore does not revert axis margins to their original size. That process should be performed separately.
Examples
xlabs <- paste0("item_", (1:20));
ylabs <- paste0("rownum_", (1:20));
# proper adjustment should be done using withr, for example
x_cex <- 0.8;
y_cex <- 1.2;
withr::with_par(adjustAxisLabelMargins(xlabs, 1, cex.axis=x_cex), {
withr::local_par(adjustAxisLabelMargins(ylabs, 2, cex.axis=y_cex))
nullPlot(xlim=c(1,20), ylim=c(1,20), doMargins=FALSE);
graphics::axis(1, at=1:20, labels=xlabs, las=2, cex.axis=x_cex);
graphics::axis(2, at=1:20, labels=ylabs, las=2, cex.axis=y_cex);
})
withr::with_par(adjustAxisLabelMargins(xlabs, 3, cex.axis=x_cex), {
withr::local_par(adjustAxisLabelMargins(ylabs, 4, cex.axis=y_cex))
nullPlot(xlim=c(1,20), ylim=c(1,20), doMargins=FALSE);
graphics::axis(3, at=1:20, labels=xlabs, las=2);
graphics::axis(4, at=1:20, labels=ylabs, las=2);
})
par("mar")
#> [1] 4.809679 4.100000 4.809679 2.100000