如何正确编写显示 "order of magnitude" 直方图的函数?

How to properly write a function showing an "order of magnitude" histogram?

根据答案here and here,我创建了自己的函数。 由于零很可能出现在 hist 我认为这一定是一个新函数。

开始(如果我用 plot(h) 替换最后一行,警告消失,但我不确定直方图是否真的使用 ... 中的正确参数显示):

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")

原则上看起来没问题,但会出现以下警告:

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

这个警告对我来说很有意义,但我不知道如何解决这个问题。

有什么想法吗?

调用plot(h,...)时出现警告信息。由于 h 属于 class "histogram"plot() 调用没有参数 breaksgraphics:::plot.histogram()。预计中断将位于 class "histogram".

的 x 参数内

如果您想摆脱这些警告,我建议只传递对函数 graphics:::plot.histogram() 有效的参数。请参阅下面的 valid_plotting_parametersmy_plotting_parameters

但是,使用这种方法,您可能会丢失 order_of_magnitude_hist()... 参数中包含的其他图形参数。您当然可以手动将 valid_plotting_parameters 放大到您希望能够传递给 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")