microbenchmark icon indicating copy to clipboard operation
microbenchmark copied to clipboard

Reorder the y axis in the plot generated by `autoplot()`

Open albertotb opened this issue 6 years ago • 6 comments
trafficstars

Is there any way to reorder the y axis in the plot generated by autoplot()?

Possible implementation, mostly borrowed from print:

autoplot.microbenchmark <- function(object, order, ...,
                                    log=TRUE,
                                    y_max=1.05 * max(object$time)) {
  if (!requireNamespace("ggplot2"))
    stop("Missing package 'ggplot2'.")
  y_min <- 0
  object$ntime <- convert_to_unit(object$time, "t")
  %% -----------------------------------------------------------------------
  if (!missing(order)) {
      s <- summary(object)
      if (order %in% colnames(s)) {
        object$expr <- factor(object$expr, levels = levels(object$expr)[order(s[[order]])])
      }
      else {
        warning("Cannot order results by", order, ".")
      }
  }
  %% -----------------------------------------------------------------------
  plt <- ggplot2::ggplot(object, ggplot2::aes_string(x="expr", y="ntime"))
  plt <- plt + ggplot2::coord_cartesian(ylim=c(y_min , y_max))
  plt <- plt + ggplot2::stat_ydensity()
  plt <- plt + ggplot2::scale_x_discrete(name="")
  y_label <- sprintf("Time [%s]", attr(object$ntime, "unit"))
  plt <- if (log) {
    plt + ggplot2::scale_y_log10(name=y_label)
  } else {
    plt + ggplot2::scale_y_continuous(name=y_label)
  }
  plt <- plt + ggplot2::coord_flip()
  plt
}

albertotb avatar Feb 12 '19 14:02 albertotb

Giving a thumbs up -- I am looking at the exact same issue. Would be nice to make that option available.

(The line plt <- plt + ggplot2::coord_cartesian(ylim=c(y_min , y_max)) can now be removed it seems.)

eddelbuettel avatar Jan 07 '24 14:01 eddelbuettel

As reordering is also helpful in print() or the boxplot, I pulled the essential two lines out (for now at least) and made them a helper function in my helper functions script, see the file now committed.

eddelbuettel avatar Jan 07 '24 15:01 eddelbuettel

@albertotb thanks for the report and patch! This seems like a useful enhancement. Your code throws this annoying warning from ggplot2. Do you know how to fix it?

Warning message: aes_string() was deprecated in ggplot2 3.0.0. ℹ Please use tidy evaluation idioms with aes(). ℹ See also vignette("ggplot2-in-packages") for more information. This warning is displayed once every 8 hours. Call lifecycle::last_lifecycle_warnings() to see where this warning was generated.


@eddelbuettel regarding "As reordering is also helpful in print()", there's an order argument to print.microbenchmark(). So you can print(mb, order = "median"). Maybe we create a global option for the print order so it will work with auto-printing?

joshuaulrich avatar Feb 14 '24 23:02 joshuaulrich

@joshuaulrich It's been a few days/weeks since I played with that. I think I concluded at the time that it is beneficial to reorder the returned object to drive both print() and autoplot() (and alike) at once. So yes a new global option would achieve that that too.

Of course a day or two after I committed the helper function I reference I found yet another example of mine where already did the same, but simpler...

eddelbuettel avatar Feb 14 '24 23:02 eddelbuettel

@joshuaulrich It's been a while, so its probably a version thing. According to this resource soemthing like this should work, but I have not tested it.

plt <- ggplot2::ggplot(object, ggplot2::aes(x=.data[["expr"]], y=.data[["ntime"]]))

albertotb avatar Feb 15 '24 14:02 albertotb

Function stats::reorder() may be helpful too.

eddelbuettel avatar Mar 09 '24 23:03 eddelbuettel