Skip to content

Commit

Permalink
Fine control of axis types in axes/xaxt/yaxt (#190)
Browse files Browse the repository at this point in the history
* support character descriptions of different axis types in axes/xaxt/yaxt

* set frame.plot default to NULL, handle all defaults of axes/xaxt/yaxt/frame.plot early on in tinyplot.default, default of frame.plot is only TRUE if both axes are drawn with axis lines

* auxiliary Axis() interface with different parameter combinations based on type

* tinyAxis() in separate file, improved comments
  • Loading branch information
zeileis authored Aug 3, 2024
1 parent b0be6d7 commit 0df2aa4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
20 changes: 20 additions & 0 deletions R/tinyAxis.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## auxiliary Axis() interface with different parameter combinations based on type
tinyAxis = function(x = NULL, ..., type = "standard") {
type = match.arg(type, c("standard", "none", "labels", "ticks", "axis"))
if (type == "none") {
invisible(numeric(0L))
} else {
args = list(x = x, ...)
if (type == "labels") {
args$tick = FALSE
} else if (type == "ticks") {
args$lwd = 0
if (!("lwd.ticks" %in% names(args))) args$lwd.ticks = 1
} else if (type == "axis") {
args$lwd.ticks = 0
} else {
args$tick = TRUE
}
do.call("Axis", args)
}
}
49 changes: 37 additions & 12 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,17 @@
#' @param ylab a label for the y axis, defaults to a description of y.
#' @param ann a logical value indicating whether the default annotation (title
#' and x and y axis labels) should appear on the plot.
#' @param axes a logical value indicating whether both axes should be drawn on
#' the plot. Use `graphical parameter` "xaxt" or "yaxt" to suppress just one of
#' the axes.
#' @param axes logical or character. Should axes be drawn (`TRUE` or `FALSE`)?
#' Or alternatively what type of axes should be drawn: `"standard" (with
#' axis, ticks, and labels; equivalent to `TRUE`), "none" (no axes;
#' equivalent to `FALSE`), `"ticks"` (only ticks and labels without axis line),
#' `"labels"` (only labels without ticks and axis line), `"axis"` (only axis
#' line and labels but no ticks). To control this separately for the two
#' axes, use the character specifications for `xaxt` and/or `yaxt`.
#' @param frame.plot a logical indicating whether a box should be drawn around
#' the plot. Can also use `frame` as an acceptable argument alias.
#' The default is to draw a frame if both axis types (set via `axes`, `xaxt`,
#' or `yaxt`) include axis lines.
#' @param grid argument for plotting a background panel grid, one of either:
#' - a logical (i.e., `TRUE` to draw the grid), or
#' - a panel grid plotting function like `grid()`.
Expand Down Expand Up @@ -267,6 +273,8 @@
#' @param height numeric giving the plot height in inches. Same considerations as
#' `width` (above) apply, e.g. will default to `tpar("file.height")` if not
#' specified.
#' @param xaxt,yaxt character specifying the type of x-axis and y-axis, respectively.
#' See `axes` for the possible values.
#' @param ... other graphical parameters. See \code{\link[graphics]{par}} or
#' the "Details" section of \code{\link[graphics]{plot}}.
#'
Expand Down Expand Up @@ -482,7 +490,7 @@ tinyplot.default = function(
ylab = NULL,
ann = par("ann"),
axes = TRUE,
frame.plot = axes,
frame.plot = NULL,
asp = NA,
grid = NULL,
palette = NULL,
Expand All @@ -506,6 +514,8 @@ tinyplot.default = function(
width = NULL,
height = NULL,
empty = FALSE,
xaxt = NULL,
yaxt = NULL,
...
) {

Expand All @@ -519,6 +529,21 @@ tinyplot.default = function(

xlabs = NULL

## handle defaults of axes, xaxt, yaxt, frame.plot
## - convert axes to character if necessary
## - set defaults of xaxt/yaxt (if these are NULL) based on axes
## - set logical axes based on xaxt/yaxt
## - set frame.plot default based on xaxt/yaxt
if (!is.character(axes)) axes = if (isFALSE(axes)) "none" else "standard"
axis_types = c("standard", "none", "labels", "ticks", "axis")
axes = match.arg(axes, axis_types)
if (is.null(xaxt)) xaxt = axes
if (is.null(yaxt)) yaxt = axes
xaxt = substr(match.arg(xaxt, axis_types), 1L, 1L)
yaxt = substr(match.arg(yaxt, axis_types), 1L, 1L)
axes = any(c(xaxt, yaxt) != "n")
if (is.null(frame.plot) || !is.logical(frame.plot)) frame.plot = all(c(xaxt, yaxt) %in% c("s", "a"))

# Write plot to output file or window with fixed dimensions
setup_device(file = file, width = width, height = height)
if (!is.null(file)) on.exit(dev.off(), add = TRUE)
Expand Down Expand Up @@ -1076,27 +1101,27 @@ tinyplot.default = function(
yside = 2
}

# axes, plot.frame and grid
# axes, frame.plot and grid
if (isTRUE(axes)) {
if (isTRUE(frame.plot)) {
# if plot frame is true then print axes per normal...
if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot", "p") && !is.null(xlabs)) {
Axis(x, side = xside, at = xlabs, labels = names(xlabs))
tinyAxis(x, side = xside, at = xlabs, labels = names(xlabs), type = xaxt)
} else {
Axis(x, side = xside)
tinyAxis(x, side = xside, type = xaxt)
}
Axis(y, side = yside)
tinyAxis(y, side = yside, type = yaxt)
} else {
# ... else only print the "outside" axes.
if (ii %in% oxaxis) {
if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot", "p") && !is.null(xlabs)) {
Axis(x, side = xside, at = xlabs, labels = names(xlabs))
tinyAxis(x, side = xside, at = xlabs, labels = names(xlabs), type = xaxt)
} else {
Axis(x, side = xside)
tinyAxis(x, side = xside, type = xaxt)
}
}
if (ii %in% oyaxis) {
Axis(y, side = yside)
tinyAxis(y, side = yside, type = yaxt)
}
}
}
Expand Down Expand Up @@ -1332,7 +1357,7 @@ tinyplot.formula = function(
ylab = NULL,
ann = par("ann"),
axes = TRUE,
frame.plot = axes,
frame.plot = NULL,
asp = NA,
grid = NULL,
pch = NULL,
Expand Down
18 changes: 12 additions & 6 deletions man/tinyplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0df2aa4

Please sign in to comment.