| 1 |
\name{pretty10exp} |
| 2 |
\alias{pretty10exp} |
| 3 |
\title{Nice 10 ** k Label Expressions} |
| 4 |
\description{ |
| 5 |
Produce nice \eqn{a \times 10^k}{a * 10^k} expressions to be used |
| 6 |
instead of the scientific notation \code{"a E<k>"}. |
| 7 |
} |
| 8 |
\usage{% ../R/prettylab.R |
| 9 |
pretty10exp(x, drop.1 = FALSE, sub10 = FALSE, digits = 7, digits.fuzz, |
| 10 |
off = pmax(10^-digits, 2^-(l10x*log2(10)+1075)), |
| 11 |
lab.type = c("plotmath","latex"), |
| 12 |
lab.sep = c("cdot", "times")) |
| 13 |
} |
| 14 |
\arguments{ |
| 15 |
\item{x}{numeric vector (e.g. axis tick locations)} |
| 16 |
\item{drop.1}{logical indicating if \eqn{1 \times}{1 *} should be |
| 17 |
dropped from the resulting expressions.} |
| 18 |
\item{sub10}{logical, \code{"10"}, \code{"100"}, an integer number or |
| 19 |
vector of length two, say \eqn{(k_1,k_2)}{(k1,k2)}, indicating if some |
| 20 |
\eqn{10^j} expressions for \eqn{j \in J}{j in J} should be formatted |
| 21 |
traditionally, notably e.g., \eqn{10^0 \equiv 1}{10^0 == 1}. |
| 22 |
\cr |
| 23 |
When a number, say \eqn{k}, \eqn{J = \{j; j \le k\}}{% |
| 24 |
J = {j; j \le k}} are all simplified, when a length--2 vector, |
| 25 |
\eqn{J = \{j; k_1 \le j \le k_2\}}{J = {j; k1 \le j \le k2}} are. |
| 26 |
|
| 27 |
Special cases: |
| 28 |
\describe{ |
| 29 |
\item{\code{sub10 = TRUE}:}{use \eqn{1} instead of \eqn{10^0},} |
| 30 |
\item{\code{sub10 = "10"}:}{short for \code{c(-1,1)}, i.e., uses |
| 31 |
\eqn{0.1} for \code{10^{-1}}, \eqn{1} for \eqn{10^0} and \eqn{10} for \eqn{10^1};} |
| 32 |
\item{\code{sub10 = "100"}:}{short for \code{c(-1,2)}, i.e., uses |
| 33 |
\eqn{0.1}, \eqn{1}, \eqn{10}, and \eqn{100} for \code{10^{-1},\dots, 10^2}.} |
| 34 |
\item{\code{sub10 = "1000"}:}{short for \code{c(-2,3)}, using |
| 35 |
\eqn{0.01}, \eqn{0.1}, \dots, \eqn{1000} for \code{10^{-2},\dots, 10^3}.} |
| 36 |
} |
| 37 |
If it would not break back compatibility, the author would nowadays |
| 38 |
choose the default as \code{sub10 = "1000"}. |
| 39 |
} |
| 40 |
\item{digits}{number of digits for mantissa (\eqn{a}) construction; |
| 41 |
the number of \emph{significant} digits, see \code{\link{signif}}.} |
| 42 |
\item{digits.fuzz}{the old deprecated name for \code{digits}.} |
| 43 |
\item{off}{a numeric offset in \code{eT <- floor(l10x + off)} where |
| 44 |
\code{l10x <- log10(abs(x))} and \code{eT} are the exponents \eqn{k} |
| 45 |
for label factors \eqn{10^k}. Previously hardcoded to |
| 46 |
\code{10^-digits}, the new default provides better results for |
| 47 |
subnormal \code{abs(x)} values.} |
| 48 |
\item{lab.type}{a string indicating how the result should look like. |
| 49 |
By default, (\code{\link{plotmath}}-compatible) |
| 50 |
\code{\link{expression}}s are returned. Alternatively, |
| 51 |
\code{lab.type = "plotmath"} returns LaTeX formatted strings for |
| 52 |
labels. (The latter is useful, e.g., when using the \CRANpkg{tikzDevice} |
| 53 |
package to generate LaTeX-processed figures.)} |
| 54 |
\item{lab.sep}{character separator between mantissa and exponent for |
| 55 |
LaTeX labels; it will be prepended with a backslash, |
| 56 |
i.e., \sQuote{"cdot"} will use \sQuote{"\\cdot"}} |
| 57 |
} |
| 58 |
\value{ |
| 59 |
For the default \code{lab.type = "plotmath"}, |
| 60 |
an expression of the same length as \code{x}, typically with elements |
| 61 |
of the form \code{a \%*\% 10 ^ k}. |
| 62 |
Exceptions are \code{0} which is kept simple, if \code{drop.1} is |
| 63 |
true and \eqn{a = 1}, \code{10 ^ k} is used, and if \code{sub10} |
| 64 |
is not false, \code{a \%*\% 10 ^ 0} as \code{a}, and \code{a \%*\% 10 ^ k} as |
| 65 |
as the corresponding formatted number \code{a * 10^k} independently of |
| 66 |
\code{drop.1}. |
| 67 |
|
| 68 |
Otherwise, a \code{\link{character}} vector of the same length as |
| 69 |
\code{x}. For \code{lab.type = "latex"}, currently the only |
| 70 |
alternative to the default, these strings are LaTeX (math mode) |
| 71 |
compatible strings. |
| 72 |
} |
| 73 |
\note{ |
| 74 |
If \code{sub10} is set, it will typically be a small number such as 0, |
| 75 |
1, or 2. Setting \code{sub10 = TRUE} will be interpreted as |
| 76 |
\code{sub10 =1} where resulting exponents \eqn{k} will either be |
| 77 |
negative or \eqn{k \ge 2}{k >= 2}. |
| 78 |
} |
| 79 |
\author{Martin Maechler; Ben Bolker contributed \code{lab.type = "latex"} |
| 80 |
and \code{lab.sep}.} |
| 81 |
\seealso{\code{\link{axTexpr}} and \code{\link{eaxis}()} which build on |
| 82 |
\code{pretty10exp()}, notably the \code{eaxis()} example plots. |
| 83 |
|
| 84 |
The new \code{\link{toLatex.numeric}} method which gives very similar |
| 85 |
results with option \code{scientific = TRUE}. |
| 86 |
\cr Further, \code{\link{axis}}, \code{\link{axTicks}}. |
| 87 |
} |
| 88 |
\examples{ |
| 89 |
pretty10exp(-1:3 * 1000) |
| 90 |
pretty10exp(-1:3 * 1000, drop.1 = TRUE) |
| 91 |
pretty10exp(c(1,2,5,10,20,50,100,200) * 1e3) |
| 92 |
pretty10exp(c(1,2,5,10,20,50,100,200) * 1e3, drop.1 = TRUE) |
| 93 |
|
| 94 |
set.seed(17); lx <- rlnorm(10, m=8, s=6) |
| 95 |
pretty10exp(lx, digits = 3) |
| 96 |
pretty10exp(lx, digits = 3, sub10 = 2) |
| 97 |
|
| 98 |
pretty10exp(lx, digits = 3, lab.type="latex") |
| 99 |
pretty10exp(lx, digits = 3, lab.type="latex", lab.sep="times", sub10=2) |
| 100 |
|
| 101 |
## use regular formatted numbers from 0.03 to 300 : |
| 102 |
pretty10exp(3*10^(-3:4), sub10 = c(-2,2)) |
| 103 |
pretty10exp(3*10^(-3:4), sub10 = c(-2,2), lab.type = "l") |
| 104 |
|
| 105 |
\dontshow{ |
| 106 |
stopifnot(identical(pretty10exp(numeric(0)), expression())) |
| 107 |
} |
| 108 |
ax <- 10^(-6:0) - 2e-16 |
| 109 |
pretty10exp(ax, drop.1=TRUE) # nice for plotting |
| 110 |
pretty10exp(ax, drop.1=TRUE, sub10=TRUE) |
| 111 |
pretty10exp(ax, drop.1=TRUE, sub10=c(-2,2)) |
| 112 |
|
| 113 |
## in sfsmisc version <= 1.0-16, no 'digits', |
| 114 |
## i.e., implicitly had digits := #{double precision digits} == |
| 115 |
(dig. <- .Machine$double.digits * log10(2)) # 15.95 |
| 116 |
pretty10exp(ax, drop.1=TRUE, digits= dig.) # ''ugly'' |
| 117 |
|
| 118 |
## Subnormal numbers |
| 119 |
x <- sort(c(outer(10^-(323:305), 1:9))); x <- c(x[1]/2, x) |
| 120 |
tail(x, 12) # nice |
| 121 |
head(x, 6) # "ugly" (they are multiple's of 2^-1074): |
| 122 |
head(x, 6) / 2^-1074 # nice |
| 123 |
|
| 124 |
head(p0 <- pretty10exp(x, off = 10^-7), 30) # previous behavior {before 'off' existed} |
| 125 |
str(head(pTen <- lapply(p0, `[[`, 3L))) |
| 126 |
str(exTen <- sapply(pTen, `[[`, 3L)) # -324 -324 .. |
| 127 |
head(f0 <- sapply(p0, `[[`, 2L), 17) |
| 128 |
|
| 129 |
head(p1 <- pretty10exp(x))# new default |
| 130 |
str(head(pTen1 <- lapply(p1, `[[`, 3L))) |
| 131 |
str(exTen1 <- sapply(pTen1, `[[`, 3L)) # -324 -324 .. |
| 132 |
head(f1 <- sapply(p1, `[[`, 2L), 17) # |
| 133 |
|
| 134 |
head(cbind(x, f0, f1, exTen, exTen1), 80) |
| 135 |
(nEQ <- which(sapply(1:length(p0), function(i) p0[[i]] != p1[[i]]))) |
| 136 |
cbind(x, f0, f1, exTen, exTen1)[nEQ,] |
| 137 |
stopifnot(is.finite(f1), 0.5 <= f1, f1 <= 9) |
| 138 |
} |
| 139 |
\keyword{dplot} |