Based on the answers here and here I created my own function.
As zeros are likely to occur in hist
I think this must be a new function.
Here we go (if I replace the last line by plot(h)
, the warnings disappear, but I am not sure, whether the histogram is really displayed with the correct parameters from ...
):
order_of_magnitude_hist <- function(x, ylog = FALSE, ...) {
params <- list(...)
optionalParamNames <- c(
"breaks", "freq", "probability", "include.lowest", "right", "density",
"angle", "col", "border", "main", "xlim", "ylim", "xlab", "ylab", "axes",
"plot", "labels", "nclass", "warn.unused")
unusedParams <- setdiff(names(params),optionalParamNames)
if (length(unusedParams)) {
stop('unused parameters ',paste(unusedParams,collapse = ', '))
}
if ("breaks" %in% names(params)) {
h <- hist(x, breaks = params$breaks, plot = FALSE)
} else {
h <- hist(x, plot = FALSE, ...)
}
assertthat::assert_that(all(h$counts >= 0))
if (!ylog) {
h$counts[h$counts == 0] <- 0
h$counts[h$counts %in% c(1:9)] <- 1
h$counts[h$counts %in% c(10:99)] <- 2
h$counts[h$counts %in% c(100:999)] <- 3
h$counts[h$counts %in% c(1000:9999)] <- 4
h$counts[h$counts %in% c(10000:99999)] <- 5
} else {
h$counts[h$counts == 0] <- 0
h$counts[h$counts == 1] <- 1
h$counts[h$counts > 1] <- log10(h$counts[h$counts > 1])
}
plot(h, ...)
}
set.seed(1)
dt <- 1/runif(n = 1000, min = 0.0001, max = 10)
order_of_magnitude_hist(dt,
breaks = seq(min(dt), max(dt), length.out = 101),
main = "Title 1")
order_of_magnitude_hist(dt,
breaks = seq(min(dt), max(dt), length.out = 101),
ylog = TRUE,
main = "title 2")
In principle it look ok, but the following warnings occur:
Warning messages:
1: In plot.window(xlim, ylim, "", ...) : "breaks" ist kein Grafikparameter
2: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) :
"breaks" ist kein Grafikparameter
3: In axis(1, ...) : "breaks" ist kein Grafikparameter
4: In axis(2, ...) : "breaks" ist kein Grafikparameter
...
1: In doTryCatch(return(expr), name, parentenv, handler) :
"breaks" ist kein Grafikparameter
The warning makes sense to me, but I have no clue, how to solve the issue.
Any ideas?