R/autoplot.R

Defines functions autoplot.microbenchmark

Documented in autoplot.microbenchmark

#' Autoplot method for microbenchmark objects: Prettier graphs for
#' microbenchmark using ggplot2
#'
#' Uses ggplot2 to produce a more legible graph of microbenchmark timings
#'
#' @param object A microbenchmark object
#' @param \dots Ignored
#' @param log If \code{TRUE} the time axis will be on log scale.
#' @param y_max The upper limit of the y axis, in the unit automatically
#'   chosen for the time axis (defaults to the maximum value)
#' @return A ggplot2 plot
#'
#' @examples
#' if (requireNamespace("ggplot2")) {
#'
#' tm <- microbenchmark(rchisq(100, 0),
#'                      rchisq(100, 1),
#'                      rchisq(100, 2),
#'                      rchisq(100, 3),
#'                      rchisq(100, 5), times=1000L)
#' ggplot2::autoplot(tm)
#' }
#' @author Ari Friedman, Olaf Mersmann
autoplot.microbenchmark <- function(object, ...,
                                    log=TRUE,
                                    y_max=NULL) {
  if (!requireNamespace("ggplot2"))
    stop("Missing package 'ggplot2'.")
  y_min <- 0
  object$ntime <- convert_to_unit(object$time, "t")
  if (is.null(y_max)) {
    y_max <- max(object$ntime)
  }
  plt <- ggplot2::ggplot(object, ggplot2::aes_string(x="expr", y="ntime"))
  plt <- plt + ggplot2::stat_ydensity()
  plt <- plt + ggplot2::scale_x_discrete(name="")
  y_label <- sprintf("Time [%s]", attr(object$ntime, "unit"))
  if (log) {
    y_min <- if (min(object$time) == 0) 1 else min(object$ntime)
    plt <- plt + ggplot2::scale_y_log10(name=y_label)
  } else {
    plt <- plt + ggplot2::scale_y_continuous(name=y_label)
  }
  plt <- plt + ggplot2::coord_flip(ylim=c(y_min , y_max))
  plt
}

Try the microbenchmark package in your browser

Any scripts or data that you put into this service are public.

microbenchmark documentation built on April 29, 2023, 1:12 a.m.