1

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?

Christoph
  • 6,841
  • 4
  • 37
  • 89

1 Answers1

1

The warning message occurs when calling plot(h,...). Since h is of class "histogram", plot() calls graphics:::plot.histogram() which does not have an argument breaks. The breaks are already expected to be inside the x argument which is of class "histogram".

If you want to get rid of these warnings, I would recommend to only pass parameters that are valid for function graphics:::plot.histogram(). See valid_plotting_parameters and my_plotting_parameters below.

However, with this approach, you might loose other graphical parameters that are contained in the ... argument of order_of_magnitude_hist(). You could of course manually enlarge valid_plotting_parameters to all parameters you want to be able to pass to graphics:::plot.histogram().

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, ...)
  valid_plotting_parameters <- c(
    "freq", "density", "angle", "col", "border", "lty", "main", "sub", "xlab",
    "ylab", "xlim", "ylim", "axes", "labels", "add", "ann")
  my_plotting_parameters <- params[names(params) %in% valid_plotting_parameters]
  do.call(what = "plot", 
          args = append(list(x = h), my_plotting_parameters))
}

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")
MichiSmith
  • 317
  • 1
  • 7