Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 32 additions & 5 deletions R/tinylabel.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,27 @@
#' ultimately get passed to.
#' @param x a numeric or character vector
#' @param labeller a formatting function to be applied to `x`, e.g. `abs`,
#' `toupper`, etc. Can also be one of the following convenience strings, for
#' which common formatting transformations are provided: `"percent"`,
#' `"comma"`, `"dollar"`, `"euro"`, or `"sterling"`.
#'
#' `toupper`, `format`, etc. Can also be one of the following convenience
#' strings (symbols), for which common formatting transformations are
#' provided: `"percent"` (`"%"`), `"comma"` (`","`), `"dollar"` (`"$"`),
#' `"euro"` (`"€"`), or `"sterling"` (`"£"`).
#' @examples
#' \dontrun{
#' x = 1e4
#' tinylabel(x, "comma")
#' tinylabel(x, ",") # same
#' tinylabel(x, "$") # or "dollar"
#'
#' # pass to xaxl/yaxl for adjusting axes tick labels in a tinyplot call
#' tinyplot(I(mpg/hp) ~ hp, data = mtcars, yaxl = "%")
#'
#' # function example (here: date formatting)
#' dat = data.frame(
#' date = seq(as.Date("2000/1/1"), by = "month", length.out = 12),
#' trend = 1:12 + rnorm(12, sd = 1)
#' )
#' tinyplot(trend ~ date, data = dat, xaxl = function(x) format(x, "%b, %Y"))
#' }
#' @keywords internal
tinylabel = function(x, labeller = NULL) {
if (is.null(labeller)) return(x)
Expand All @@ -18,8 +35,18 @@ tinylabel = function(x, labeller = NULL) {


labeller_fun = function(label = "percent") {

labels = c(
"%" = "percent",
"," = "comma",
"$" = "dollar",
"\u20ac" = "euro",
"\u00a3" = "sterling"
)
if (label %in% names(labels)) label = labels[label]

## all labels plus absolute value version
labels = c("percent", "comma", "dollar", "euro", "sterling")
# labels = c("percent", "comma", "dollar", "euro", "sterling")
labels = c(labels, paste0("abs_", labels))

## match full label first, then store abs_ info separately
Expand Down
5 changes: 3 additions & 2 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@
#' @param xaxl,yaxl A formatting function (or character string) to apply to the
#' x- or y-axis tick labels. This affects the _appearance_ of the labels only,
#' not the calculation or positioning of the tick marks. In addition to custom
#' functions, users can supply one of several convenience strings for common
#' formats: `"percent"`, `"comma"`, `"dollar"`, `"euro"`, or `"sterling"`.
#' functions, users can supply one of several convenience strings (symbols)
#' for common formats: `"percent"` (`"%"`), `"comma"` (`","`), `"dollar"`
#' (`"$"`), `"euro"` (`"€"`), or `"sterling"` (`"£"`).
#' @param log a character string which contains "x" if the x axis is to be
#' logarithmic, "y" if the y axis is to be logarithmic and "xy" or "yx" if
#' both axes are to be logarithmic.
Expand Down
25 changes: 22 additions & 3 deletions man/tinylabel.Rd

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

5 changes: 3 additions & 2 deletions man/tinyplot.Rd

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