| 1 |
#### Pretty Labels for "plotmath" axes -- Main function: eaxis() |
| 2 |
|
| 3 |
### --> these are from ~/R/MM/GRAPHICS/axis-prettylab.R |
| 4 |
|
| 5 |
### Help files: ../man/pretty10exp.Rd ../man/axTexpr.Rd ../man/eaxis.Rd |
| 6 |
### -------------- ---------- -------- |
| 7 |
|
| 8 |
pretty10exp <- function(x, drop.1 = FALSE, sub10 = FALSE, |
| 9 |
digits = 7, digits.fuzz, |
| 10 |
## default `off` corrects for subnormal |x| {on x86_64 ..}:; |
| 11 |
off = pmax(10^-digits, 2^-(l10x*log2(10)+1075)), |
| 12 |
lab.type = c("plotmath", "latex"), |
| 13 |
lab.sep = c("cdot", "times")) |
| 14 |
{ |
| 15 |
## Purpose: produce "a 10^k" label expressions instead of "a e<k>" |
| 16 |
## ---------------------------------------------------------------------- |
| 17 |
## Arguments: x: numeric vector (e.g. axis tick locations) |
| 18 |
## ---------------------------------------------------------------------- |
| 19 |
## Author: Martin Maechler, Date: 7 May 2004, Jan 2006, ... |
| 20 |
|
| 21 |
if(!missing(digits.fuzz)) { |
| 22 |
message("'digits.fuzz' is deprecated; use 'digits' instead") |
| 23 |
digits <- digits.fuzz |
| 24 |
} |
| 25 |
lab.type <- match.arg(lab.type) |
| 26 |
lab.sep <- match.arg(lab.sep) |
| 27 |
|
| 28 |
l10x <- log10(abs(x)) |
| 29 |
eT <- floor(l10x + off) # x == 0 case is dealt with below |
| 30 |
## ^^^^^ round() would be better, e.g., when x <- 10^-(323:300) <--> "subnormal" rounding |
| 31 |
mT <- signif(x / 10^eT, digits) # m[antissa] |
| 32 |
ss <- vector("list", length(x)) |
| 33 |
if(sub.10 <- !isFALSE(sub10)) { |
| 34 |
sub10 <- if(isTRUE(sub10)) |
| 35 |
c(0L,0L) |
| 36 |
else if(identical(sub10, "10")) c(-1L,1L) |
| 37 |
else if(identical(sub10, "100")) c(-1L,2L) |
| 38 |
else if(identical(sub10,"1000")) c(-2L,3L) |
| 39 |
else |
| 40 |
as.integer(sub10) |
| 41 |
noE <- |
| 42 |
if(length(sub10) == 1) { |
| 43 |
## *Not* really sensible -- was active till 2025-07-26 [sfsmisc version <= 1.1-21] |
| 44 |
## if(sub10 < 0) |
| 45 |
## stop("'sub10' must not be negative if a single number") |
| 46 |
eT <= sub10 |
| 47 |
} else if(length(sub10) == 2) { |
| 48 |
stopifnot(sub10[1] <= sub10[2]) |
| 49 |
sub10[1] <= eT & eT <= sub10[2] |
| 50 |
} else stop("invalid 'sub10'") |
| 51 |
## for noE's, mt := value (instead of mantissa): |
| 52 |
mT[noE] <- mT[noE] * 10^eT[noE] |
| 53 |
} |
| 54 |
if (lab.type == "plotmath") { |
| 55 |
for(i in seq_along(x)) |
| 56 |
ss[[i]] <- |
| 57 |
if(x[i] == 0) quote(0) |
| 58 |
else if(sub.10 && noE[i] ) substitute( A, list(A = mT[i])) |
| 59 |
else if(drop.1 && mT[i] == 1) substitute( 10^E, list(E = eT[i])) |
| 60 |
else if(drop.1 && mT[i] == -1) substitute(-10^E, list(E = eT[i])) |
| 61 |
else substitute(A %*% 10^E, list(A = mT[i], E = eT[i])) |
| 62 |
do.call("expression", ss) |
| 63 |
} else { ## lab.type=="latex" |
| 64 |
## TO DO: allow format specifier?? |
| 65 |
mTf <- formatC(mT, width=1) |
| 66 |
eTf <- formatC(eT, width=1) |
| 67 |
for(i in seq_along(x)) |
| 68 |
ss[[i]] <- |
| 69 |
if(x[i] == 0) "" |
| 70 |
else if(sub.10 && noE[i] ) mTf[i] |
| 71 |
else if(drop.1 && mT[i] == 1) sprintf("$10^{%s}$", eTf[i]) |
| 72 |
else if(drop.1 && mT[i] == -1) sprintf("$-10^{%s}$",eTf[i]) |
| 73 |
else sprintf("$%s \\%s 10^{%s}$", mTf[i], lab.sep, eTf[i]) |
| 74 |
unlist(ss) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
axTexpr <- function(side, at = axTicks(side, axp=axp, usr=usr, log=log), |
| 79 |
axp = NULL, usr = NULL, log = NULL, drop.1 = FALSE) |
| 80 |
{ |
| 81 |
## Purpose: Do "a 10^k" labeling instead of "a e<k>" |
| 82 |
## ------------------------------------------------- |
| 83 |
## Arguments: as for axTicks() |
| 84 |
pretty10exp(at, drop.1) |
| 85 |
} |
| 86 |
|
| 87 |
### TODO: |
| 88 |
### |
| 89 |
### My axis(.) function with at least two options ("engineering/not") |
| 90 |
### Really wanted: allow xaxt = "p" (pretty) or "P" (pretty, "Engineer") |
| 91 |
### FIXME(2): max.at is only needed because axTicks() is sometimes too large |
| 92 |
### FIXME(3): ?? axisTicks() instead of axTicks(): |
| 93 |
## set.seed(1);x <- runif(100,-0.18, 1.13) |
| 94 |
## par(mar=.1+c(5,4,2,4)); plot(x,axes=FALSE) |
| 95 |
## eaxis(4) # ugly |
| 96 |
## eaxis(2, at=axisTicks(par("usr")[3:4],log=FALSE)) # much better |
| 97 |
eaxis <- function(side, at = if(log) axTicks(side, axp=axp, log=log, nintLog=nintLog) |
| 98 |
else axTicks(side, axp=axp, log=log), |
| 99 |
labels = NULL, log = NULL, |
| 100 |
## use expression (plotmath/latex) if 'log' or exponential format: |
| 101 |
use.expr = log || format.info(as.numeric(at), digits=7)[3] > 0, |
| 102 |
f.smalltcl = 3/5, at.small = NULL, small.mult = NULL, equidist.at.tol = 0.002, |
| 103 |
small.args = list(), |
| 104 |
draw.between.ticks = TRUE, between.max = 4, |
| 105 |
outer.at = TRUE, drop.1 = TRUE, sub10 = FALSE, las = 1, |
| 106 |
nintLog = max(12L, par("lab")[2L - is.x]), axp = NULL, n.axp = NULL, |
| 107 |
max.at = Inf, lab.type="plotmath", lab.sep="cdot", ...) |
| 108 |
{ |
| 109 |
## Purpose: "E"xtended, "E"ngineer-like (log-)axis |
| 110 |
## ---------------------------------------------------------------------- |
| 111 |
## Author: Martin Maechler, Date: 13 Oct 2007 |
| 112 |
|
| 113 |
## first part: same as graphics::axTicks() [also by MM] : |
| 114 |
is.x <- side%%2 == 1 |
| 115 |
XY <- function(ch) paste0(if (is.x) "x" else "y", ch) |
| 116 |
if(is.null(log)) log <- par(XY("log")) |
| 117 |
if(is.null(axp)) { |
| 118 |
axp <- par(XY("axp")) |
| 119 |
if(!is.null(n.axp)) { |
| 120 |
if(is.numeric(n.axp) && length(n.axp) == 1 && n.axp == as.integer(n.axp)) |
| 121 |
axp[3] <- n.axp |
| 122 |
else stop(gettextf( |
| 123 |
"'n.axp' must be an integer to be used as '%s', see ?par and there 'xaxp'", |
| 124 |
XY("axp")), domain=NA) |
| 125 |
} |
| 126 |
} |
| 127 |
if(is.finite(max.at <- round(max.at))) { ## "thin the 'at' values |
| 128 |
if(max.at < 1) stop("'max.at' must be >= 1") |
| 129 |
at <- quantile(at, (0:max.at)/max.at, names = FALSE, |
| 130 |
type = 3) ## <-- ensure that order statistics are used |
| 131 |
if(!log && is.null(at.small) && { d <- diff(at) |
| 132 |
any(abs(diff(d)) > equidist.at.tol * mean(d)) }) # at is not equidistant : |
| 133 |
at.small <- FALSE |
| 134 |
} |
| 135 |
if(is.null(labels)) |
| 136 |
labels <- if(use.expr) { |
| 137 |
pretty10exp(at, drop.1=drop.1, sub10=sub10, |
| 138 |
lab.type=lab.type, lab.sep=lab.sep) |
| 139 |
} else if(lab.type == "latex") |
| 140 |
paste("$", at, "$", sep="") |
| 141 |
else TRUE |
| 142 |
else if(length(labels) == 1 && is.na(labels)) # no 'plotmath' |
| 143 |
labels <- TRUE |
| 144 |
if(getOption("digits") > 16 && |
| 145 |
(isTRUE(labels) || (is.expression(labels) && "numeric" %in% vapply(labels, mode, "")))) { |
| 146 |
## options(digits = 17) leads to ugly decimal formatting |
| 147 |
op <- options(digits = 15); on.exit(options(op)) |
| 148 |
message("temporarily lower \"digits\" option to 15 ..") |
| 149 |
} |
| 150 |
axis(side, at = at, labels = labels, las=las, ...) |
| 151 |
if(log) { |
| 152 |
if(any(at <= 0)) stop("invalid 'log=TRUE' for at <= 0: not a true log scale plot?") |
| 153 |
l1 <- (lat <- log10(at)) %% 1 ## the 10^k ones |
| 154 |
l.int <- l1 < 1e-5 | l1 > 1 - 1e-5 |
| 155 |
if(draw.between.ticks && all(l.int)) { ## all lat are integer |
| 156 |
## check if have "thinned" but still want to draw ticks |
| 157 |
if(any(diff(lat <- sort(round(lat, 5))) > 1)) { |
| 158 |
nl <- length(lat0 <- lat) |
| 159 |
## extend 'at' (new must contain the previous!) |
| 160 |
lat <- lat[1]:lat[nl] |
| 161 |
if(length(lat) > between.max*nl) { ## too many: thin them! |
| 162 |
lat <- unique(round(seqXtend(lat0, between.max*nl, |
| 163 |
"interpolate"))) |
| 164 |
if(is.null(at.small) && median(diff(lat)) > 1.5) |
| 165 |
## no small ticks, if large are mostly not 10^(k1..k2) |
| 166 |
at.small <- FALSE |
| 167 |
} |
| 168 |
at <- 10^lat |
| 169 |
axis(side, at = at, labels = FALSE, las=las, ...) |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
if(is.null(at.small)) { ## create smart default, using small.mult |
| 174 |
at.small <- |
| 175 |
if(log) { |
| 176 |
if(!all(l.int)) at <- at[l.int] |
| 177 |
if(is.null(small.mult)) small.mult <- 9 |
| 178 |
if(length(at)) |
| 179 |
outer(2:small.mult, c(if(outer.at) at[1]/10, at)) |
| 180 |
} else { |
| 181 |
## assumes that 'at' is equidistant |
| 182 |
d <- diff(at <- sort(at)) |
| 183 |
if(any(abs(diff(d)) > equidist.at.tol * (dd <- mean(d)))) |
| 184 |
FALSE ## 'at' is not equidistant" |
| 185 |
else { |
| 186 |
if(is.null(small.mult)) { |
| 187 |
## look at 'dd' , e.g. in {5, 50, 0.05, 0.02 ..} |
| 188 |
d. <- dd / 10^floor(log10(dd)) |
| 189 |
small.mult <- { |
| 190 |
if(d. %% 5 == 0) 5 |
| 191 |
else if(d. %% 4 == 0) 4 |
| 192 |
else if(d. %% 2 == 0) 2 |
| 193 |
else if(d. %% 3 == 0) 3 |
| 194 |
else if(d. %% 0.5 == 0) 5 |
| 195 |
else 2 } |
| 196 |
} |
| 197 |
outer(seq_len(small.mult-1)/small.mult * dd, |
| 198 |
c(if(outer.at) at[1]-dd, at), "+") |
| 199 |
} |
| 200 |
} |
| 201 |
## |
| 202 |
if(outer.at) { # make sure 'at.small' remain inside "usr" |
| 203 |
p.u <- sort(par("usr")[if(is.x) 1:2 else 3:4]) |
| 204 |
if(log) p.u <- 10^p.u |
| 205 |
at.small <- at.small[p.u[1] <= at.small & at.small <= p.u[2]] |
| 206 |
} |
| 207 |
} |
| 208 |
if(is.numeric(at.small) && any(is.finite(at.small))) ## can use NA or FALSE to suppress |
| 209 |
## axis(side, at = at.small, .....) |
| 210 |
do.call(axis, c(list(side, at = at.small, labels = FALSE, |
| 211 |
tcl = f.smalltcl * par("tcl")), |
| 212 |
small.args)) |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
## @author Alain Hauser <[email protected]> |
| 218 |
## @date 2014-02-12 originally |
| 219 |
|
| 220 |
toLatex.numeric <- function(object, |
| 221 |
digits = format.info(object)[2], |
| 222 |
scientific = format.info(object)[3] > 0, |
| 223 |
times = "\\cdot", ...) |
| 224 |
{ |
| 225 |
sround <- function(x, digits) sprintf("%0.*f", digits, x) |
| 226 |
if(scientific) { |
| 227 |
## Strings in scientific format -- isn't regex a funny thing? ;-) |
| 228 |
# res <- as.character(pretty10exp(object, digits = digits + 1)) |
| 229 |
# res <- sub("%\\*%", gsub("\\\\", "\\\\\\\\", times), res) |
| 230 |
# sub("10\\^(.*)$", "10^{\\1}", res) |
| 231 |
## Original version without pretty10exp and regex: |
| 232 |
eT <- floor(log10(abs(object)) + 10^(-digits-1)) |
| 233 |
sprintf("%s %s 10^{%d}", |
| 234 |
sround(object/10^eT, digits), times, eT) |
| 235 |
} else { |
| 236 |
## Actual output strings |
| 237 |
sround(object, digits) |
| 238 |
} |
| 239 |
} |