|
| 1 | +#' @title Add labels to the axis labels |
| 2 | +#' |
| 3 | +#' @description A wrapper function for \code{\link[graphics]{mtext}} |
| 4 | +#' with two changes. |
| 5 | +#' |
| 6 | +#' 1. The default for \code{text} and \code{at} is now \code{NULL}. If both are \code{NULL}, \code{\link[graphics]{axTicks}} will be used to determine what values to include on the x (\code{side = 1}) or y (\code{side = 2}) axis. |
| 7 | +#' 2. If \code{length(text) == 1} and \code{at = NULL}, then the appropriate \code{side} will find the central point of the axis to put the text (e.g., for an axis title). |
| 8 | + |
| 9 | +#' @param text a character or expression vector specifying the text to be written. See \code{\link[graphics]{mtext}}. |
| 10 | +#' |
| 11 | +#' @param side an integer specifying which side of the plot the axis is to be drawn on. The axis is placed as follows: 1=below, 2=left, 3=above and 4=right. |
| 12 | +#' |
| 13 | +#' @param line on which MARgin line, starting at 0 counting outwards. |
| 14 | +#' |
| 15 | +#' @param outer use outer margins if available. |
| 16 | +#' |
| 17 | +#' @param at The location of each string in user coordinates (i.e., the text). See \code{\link[graphics]{mtext}}. |
| 18 | +#' |
| 19 | +#' @param labels Set to \code{FALSE}. See \code{\link[graphics]{axis}}. |
| 20 | +#' |
| 21 | +#' @param adj adjustment for each string in reading direction. For strings parallel to the axes, adj = 0 means left or bottom alignment, and adj = 1 means right or top alignment. See \code{\link[graphics]{mtext}}. |
| 22 | +#' |
| 23 | +#' @param padj adjustment for each string perpendicular to the reading direction (which is controlled by \code{adj}). See \code{\link[graphics]{mtext}}. |
| 24 | +#' |
| 25 | +#' @param cex character expansion factor. Can be a vector. |
| 26 | +#' |
| 27 | +#' @param col color to use. Can be a vector. \code{NA} values (the default) means use \code{par("col")}. |
| 28 | +#' |
| 29 | +#' @param font font for text. Can be a vector. \code{NA} values (the default) means use \code{par("font")}. |
| 30 | + |
| 31 | + |
| 32 | +#' @param ... Other graphical parameters that may apply to \code{\link[graphics]{mtext}}. |
| 33 | +#' |
| 34 | +#' |
| 35 | +#' |
| 36 | +#' @examples |
| 37 | +#' \dontrun{ |
| 38 | +#' blank( |
| 39 | +#' xlim = c(0,50), |
| 40 | +#' ylim = c(0,100), |
| 41 | +#' bty ="l" |
| 42 | +#' ) |
| 43 | +#' |
| 44 | +#' axis_blank(1, at = seq(0,50,10)) |
| 45 | +#' axis_text(side = 1) |
| 46 | +#' axis_blank(2, at = seq(0,100,20)) |
| 47 | +#' axis_text(side = 2) |
| 48 | +#' |
| 49 | +#' } |
| 50 | +#' |
| 51 | +#' @export |
| 52 | +axis_text <- function(text = NULL, side = 3, line = 0, outer = FALSE, at = NULL, |
| 53 | + adj = NA, padj = NA, cex = NA, col = NA, font = NA, ...){ |
| 54 | + if(!is.numeric(side)){ |
| 55 | + stop("side must be numeric. 1=below, 2=left, 3=above and 4=right.") |
| 56 | + } |
| 57 | + if(is.null(at) & is.null(text)){ |
| 58 | + is_logged <- ifelse(side %in% c(1,3), par("xlog"), par("ylog")) |
| 59 | + |
| 60 | + at <- text <- axTicks(side = side, log = is_logged) |
| 61 | + } |
| 62 | + if(length(text == 1) & is.null(at)){ |
| 63 | + if(side %% 2 == 1){ |
| 64 | + at <- mean(par("usr")[1:2]) |
| 65 | + } else { |
| 66 | + at <- mean(par("usr")[3:4]) |
| 67 | + } |
| 68 | + } |
| 69 | + mtext(text = text, side = side, line = line, outer = outer, |
| 70 | + at = at, adj = adj, padj = padj, cex = cex, col = col, font = font, |
| 71 | + ...) |
| 72 | +} |
0 commit comments