Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added axis_text
  • Loading branch information
mfidino committed Dec 7, 2021
commit fe95e8014a8ae1aacdc22b34bd598d4a729a18dd
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(axis_blank)
export(axis_text)
export(blank)
export(default_nvalues)
export(default_violin_type)
Expand Down
72 changes: 72 additions & 0 deletions R/axis_text.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' @title Add labels to the axis labels
#'
#' @description A wrapper function for \code{\link[graphics]{mtext}}
#' with two changes.
#'
#' 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.
#' 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).

#' @param text a character or expression vector specifying the text to be written. See \code{\link[graphics]{mtext}}.
#'
#' @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.
#'
#' @param line on which MARgin line, starting at 0 counting outwards.
#'
#' @param outer use outer margins if available.
#'
#' @param at The location of each string in user coordinates (i.e., the text). See \code{\link[graphics]{mtext}}.
#'
#' @param labels Set to \code{FALSE}. See \code{\link[graphics]{axis}}.
#'
#' @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}}.
#'
#' @param padj adjustment for each string perpendicular to the reading direction (which is controlled by \code{adj}). See \code{\link[graphics]{mtext}}.
#'
#' @param cex character expansion factor. Can be a vector.
#'
#' @param col color to use. Can be a vector. \code{NA} values (the default) means use \code{par("col")}.
#'
#' @param font font for text. Can be a vector. \code{NA} values (the default) means use \code{par("font")}.


#' @param ... Other graphical parameters that may apply to \code{\link[graphics]{mtext}}.
#'
#'
#'
#' @examples
#' \dontrun{
#' blank(
#' xlim = c(0,50),
#' ylim = c(0,100),
#' bty ="l"
#' )
#'
#' axis_blank(1, at = seq(0,50,10))
#' axis_text(side = 1)
#' axis_blank(2, at = seq(0,100,20))
#' axis_text(side = 2)
#'
#' }
#'
#' @export
axis_text <- function(text = NULL, side = 3, line = 0, outer = FALSE, at = NULL,
adj = NA, padj = NA, cex = NA, col = NA, font = NA, ...){
if(!is.numeric(side)){
stop("side must be numeric. 1=below, 2=left, 3=above and 4=right.")
}
if(is.null(at) & is.null(text)){
is_logged <- ifelse(side %in% c(1,3), par("xlog"), par("ylog"))

at <- text <- axTicks(side = side, log = is_logged)
}
if(length(text == 1) & is.null(at)){
if(side %% 2 == 1){
at <- mean(par("usr")[1:2])
} else {
at <- mean(par("usr")[3:4])
}
}
mtext(text = text, side = side, line = line, outer = outer,
at = at, adj = adj, padj = padj, cex = cex, col = col, font = font,
...)
}
68 changes: 68 additions & 0 deletions man/axis_text.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-04-axis_text.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
context("Test axis_text")

test_that("axis_text", {

f <- function(side, at, minor, tck, text, line){
blank(xlim = c(0,50), ylim = c(0,100))
axis_blank(side=side,at=at,minor=minor,tck=tck)
axis_text(text = text, side = side, line = line, at = at)
}
expect_error(
f(2, NULL, TRUE, -0.025, text = "yo")
)
expect_silent(
f(1, NULL, TRUE, -0.025, text = "Hello", line = 2)
)
expect_silent(
f(2, NULL, TRUE, -0.025, text = "World", line = 2)
)
expect_silent(
f(2, NULL, FALSE, -0.025,text = NULL, line = 1)
)
})