Debian Codegraph @

R/plot.R

raw
1 #' Plot \code{units} objects
2 #'
3 #' Create axis label with appropriate labels.
4 #'
5 #' @param lab length one character; name of the variable to plot
6 #' @param u vector of class \code{units}
7 #' @param sep length two character vector, defaulting to \code{c("~","~")}, with
8 #' the white space between unit name and unit symbols, and between subsequent
9 #' symbols.
10 #' @param group length two character vector with grouping symbols, e.g.
11 #' \code{c("(",")")} for parenthesis, or \code{c("","")} for no group symbols
12 #' @param parse logical; indicates whether a parseable expression should be
13 #' returned (typically needed for super scripts), or a simple character string
14 #' without special formatting.
15 #'
16 #' @details \link{units_options} can be used to set and change the defaults for
17 #' \code{sep}, \code{group} and \code{doParse}.
18 #'
19 #' @name plot.units
20 #' @export
21 make_unit_label = function(lab, u,
22 sep = units_options("sep"),
23 group = units_options("group"),
24 parse = units_options("parse")) {
25
26 if (parse) {
27 str = paste0("group('", group[1], "',",
28 as.character(units(u), escape_units = TRUE, plot_sep = sep[2]),
29 ",'", group[2], "')")
30
31 if (length(grep("[^\t ]", lab)) > 0) {
32 if (is.character(lab))
33 lab <- gsub("[[:space:]]+", "~", lab)
34 str = paste0(lab, "*", sep[1], str)
35 }
36
37 parse(text = str)
38
39 } else {
40 paste0(lab, " ", group[1], as.character(units(u)), group[2])
41 }
42 }
43
44 .make_unit_label <- function(label, unit, default) {
45 if (is.null(label))
46 label <- default
47 if (inherits(unit, "units"))
48 label <- make_unit_label(label, unit)
49 label
50 }
51
52 #' @description Plot method for \code{units} objects.
53 #'
54 #' @param x object of class units, to plot along the x axis, or, if y is missing, along the y axis
55 #' @param y object to plot along the y axis, or missing
56 #' @param xlab character; x axis label
57 #' @param ylab character; y axis label
58 #' @param ... other parameters, passed on to \link{plot.default}
59 #'
60 #' @examples
61 #' displacement = mtcars$disp * as_units("in")^3
62 #' units(displacement) = make_units(cm^3)
63 #' weight = mtcars$wt * 1000 * make_units(lb)
64 #' units(weight) = make_units(kg)
65 #' plot(weight, displacement)
66 #'
67 #' units_options(group = c("(", ")") ) # parenthesis instead of square brackets
68 #' plot(weight, displacement)
69 #'
70 #' units_options(sep = c("~~~", "~"), group = c("", "")) # no brackets; extra space
71 #' plot(weight, displacement)
72 #'
73 #' units_options(sep = c("~", "~~"), group = c("[", "]"))
74 #' gallon = as_units("gallon")
75 #' consumption = mtcars$mpg * make_units(mi/gallon)
76 #' units(consumption) = make_units(km/l)
77 #' plot(displacement, consumption) # division in consumption
78 #'
79 #' units_options(negative_power = TRUE) # division becomes ^-1
80 #' plot(displacement, consumption)
81 #'
82 #' plot(1/displacement, 1/consumption)
83 #'
84 #' @export
85 plot.units <- function(x, y, xlab = NULL, ylab = NULL, ...) {
86 # We define the axis labels if they are not already provided and then let
87 # the default plotting function take over...
88 xlab0 <- deparse1(substitute(x))
89 ylab0 <- deparse1(substitute(y))
90
91 if (missing(y)) { # from xy.coords:
92 ylab <- .make_unit_label(ylab, x, xlab0)
93 if (is.null(xlab)) xlab <- "Index"
94
95 y <- x
96 x <- seq_along(x)
97
98 return(NextMethod("plot", x, y, xlab=xlab, ylab=ylab))
99 }
100
101 xlab <- .make_unit_label(xlab, x, xlab0)
102 ylab <- .make_unit_label(ylab, y, ylab0)
103
104 NextMethod("plot", xlab=xlab, ylab=ylab)
105 }
106
107 #' histogram for unit objects
108 #'
109 #' histogram for unit objects
110 #' @param x object of class units, for which we want to plot the histogram
111 #' @param xlab character; x axis label
112 #' @param main character; title of histogram
113 #' @param ... parameters passed on to \link{hist.default}
114 #' @export
115 #' @examples
116 #' units_options(parse = FALSE) # otherwise we break on the funny symbol!
117 #' u = set_units(rnorm(100), degree_C)
118 #' hist(u)
119 hist.units <- function(x, xlab = NULL, main = paste("Histogram of", xname), ...) {
120 # We define the axis labels if they are not already provided and then let
121 # the default plotting function take over...
122 xname <- deparse1(substitute(x))
123 xlab <- .make_unit_label(xlab, x, xname)
124 x = drop_units(x)
125 NextMethod("hist", xlab=xlab, main=main)
126 }
127
128 #' boxplot for unit objects
129 #'
130 #' boxplot for unit objects
131 #' @param x object of class units, for which we want to plot the boxplot
132 #' @param ... parameters passed on to \link{boxplot.default}
133 #' @param horizontal logical indicating if the boxplots should be horizontal;
134 #' default FALSE means vertical boxes.
135 #' @export
136 #' @examples
137 #' units_options(parse = FALSE) # otherwise we break on the funny symbol!
138 #' u = set_units(rnorm(100), degree_C)
139 #' boxplot(u)
140 boxplot.units <- function(x, ..., horizontal = FALSE) {
141 xlab <- ylab <- NULL
142 lab <- make_unit_label(deparse1(substitute(x)), x)
143 if (horizontal)
144 xlab <- lab
145 else ylab <- lab
146 x <- drop_units(x)
147 NextMethod("boxplot", xlab=xlab, ylab=ylab)
148 }