Skip to content

Commit fe95e80

Browse files
committed
added axis_text
1 parent 5bca720 commit fe95e80

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(axis_blank)
4+
export(axis_text)
45
export(blank)
56
export(default_nvalues)
67
export(default_violin_type)

R/axis_text.R

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

man/axis_text.Rd

+68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-04-axis_text.R

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
context("Test axis_text")
2+
3+
test_that("axis_text", {
4+
5+
f <- function(side, at, minor, tck, text, line){
6+
blank(xlim = c(0,50), ylim = c(0,100))
7+
axis_blank(side=side,at=at,minor=minor,tck=tck)
8+
axis_text(text = text, side = side, line = line, at = at)
9+
}
10+
expect_error(
11+
f(2, NULL, TRUE, -0.025, text = "yo")
12+
)
13+
expect_silent(
14+
f(1, NULL, TRUE, -0.025, text = "Hello", line = 2)
15+
)
16+
expect_silent(
17+
f(2, NULL, TRUE, -0.025, text = "World", line = 2)
18+
)
19+
expect_silent(
20+
f(2, NULL, FALSE, -0.025,text = NULL, line = 1)
21+
)
22+
})

0 commit comments

Comments
 (0)