grantmcdermott · GitHub

microbenchmark:::autoplot.microbenchmark does not appear to do more than offer a ymax arg. Maybe that is all. I don't know.

It has several additional arguments. But I don't want to push this PR further off-track from the narrow goal of providing basic type_violin support. FWIW, under the fold is a basic tinyplot.microbenchmark method. I'll submit something like to Josh's repo once we get this PR merged and the next version of tinyplot on CRAN.

Example: tinyplot.microbenchmark method
library(microbenchmark)
pkgload::load_all("~/Documents/Projects/tinyplot/")
#> ℹ Loading tinyplot
tinyplot.microbenchmark = function(x,
                                   order=NULL,
                                   log=TRUE,
                                   unit=NULL,
                                   y_max = NULL,
                                   flip = TRUE,
                                   trim = TRUE,
                                   joint.bw = FALSE,
                                   ...) {
  y_min <- 0
  unit <- microbenchmark:::determine_unit(x, unit)
  x$ntime <- microbenchmark:::convert_to_unit(x, unit)
  if (is.null(y_max)) {
    y_max <- max(x$ntime)
  }
  if (!is.null(order)) {
    s <- summary(x)
    x_colnames <- colnames(s)
    order <- match.arg(order, x_colnames, several.ok=TRUE)
    new_order <- do.call("order", c(s[, order, drop=FALSE], decreasing=TRUE))
    x$expr <- factor(x$expr, levels = levels(x$expr)[new_order])
  }
  y_label <- sprintf("Time (%s) for neval = %d",
                     attr(x$ntime, "unit"),
                     nrow(x) / length(levels(x$expr)))
  if (log) {
    y_min <- if (min(x$time) == 0) 1 else min(x$ntime)
    log = "y"
  } else {
    log = NULL
  }
  tinyplot(
    ntime ~ expr, data = x, type = "violin",
    ylab = y_label, main = "microbenchmark timings",
    log = log,
    trim = trim,
    flip = TRUE,
    joint.bw = joint.bw,
    ylim = c(y_min, y_max),
    ...
  )
}
tm <- microbenchmark(rchisq(100, 0),
                     rchisq(100, 1),
                     rchisq(100, 2),
                     rchisq(100, 3),
                     rchisq(100, 5), times=1000L)
ggplot2::autoplot(tm) + ggplot2::theme_classic()

tinytheme('classic')
plt(tm, fill = "white")

tinytheme()

Created on 2025-03-31 with reprex v2.1.1

Read the original on github.com ↗