From 3e5d63fbe263f3ced4b61715f21b951361bfef77 Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Fri, 26 Oct 2018 08:56:55 -0400 Subject: [PATCH] Allow setting a non-logarithmic scales --- NEWS.md | 2 ++ R/autoplot.R | 3 +++ R/bytes.R | 17 ++++++++++++----- R/time.R | 18 ++++++++++++++---- man/scale_bench_time.Rd | 12 ++++++++---- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1602d9e..753e5c4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # bench 1.0.1.9000 +* `scale_bench_time()` and `scale_bench_bytes()` now allow you to use a non-logarithmic scale. + # bench 1.0.1 * Add support for macOS versions prior to 10.12 diff --git a/R/autoplot.R b/R/autoplot.R index 1a3b840..5cc078e 100644 --- a/R/autoplot.R +++ b/R/autoplot.R @@ -48,6 +48,9 @@ autoplot.bench_mark <- function(object, type = c("beeswarm", "jitter", "ridge", "boxplot", "violin"),...) { + # Just convert everything to a character first + object$expression <- as.character(object$expression) + res <- tidyr::unnest(object) p <- ggplot2::ggplot(res) diff --git a/R/bytes.R b/R/bytes.R index e9dede7..d4e3712 100644 --- a/R/bytes.R +++ b/R/bytes.R @@ -160,10 +160,17 @@ type_sum.bench_bytes <- function(x) { #' #' This both log transforms the times and formats the labels as a `bench_time` #' object. -#' @inheritParams scales::log_trans +#' @inheritParams bench_time_trans #' @keywords internal #' @export bench_bytes_trans <- function(base = 2) { + if (is.null(base)) { + return( + scales::trans_new("bch:byt", as.numeric, as_bench_bytes, + scales::pretty_breaks(), domain = c(1e-100, Inf) + ) + ) + } trans <- function(x) log(as.numeric(x), base) inv <- function(x) as_bench_bytes(base ^ as.numeric(x)) @@ -180,13 +187,13 @@ scale_type.bench_bytes <- function(x) "bench_bytes" #' @name scale_bench_time #' @keywords internal #' @export -scale_x_bench_bytes <- function(...) { - ggplot2::scale_x_continuous(..., trans = bench_bytes_trans()) +scale_x_bench_bytes <- function(base = 10, ...) { + ggplot2::scale_x_continuous(..., trans = bench_bytes_trans(base = base)) } #' @rdname scale_bench_time #' @keywords internal #' @export -scale_y_bench_bytes <- function(...) { - ggplot2::scale_y_continuous(..., trans = bench_bytes_trans()) +scale_y_bench_bytes <- function(base = 10, ...) { + ggplot2::scale_y_continuous(..., trans = bench_bytes_trans(base = base)) } diff --git a/R/time.R b/R/time.R index f057aee..34cb0eb 100644 --- a/R/time.R +++ b/R/time.R @@ -197,6 +197,14 @@ type_sum.bench_time <- function(x) { #' @keywords internal #' @export bench_time_trans <- function(base = 10) { + if (is.null(base)) { + return( + scales::trans_new("bch:tm", as.numeric, as_bench_time, + scales::pretty_breaks(), domain = c(1e-100, Inf) + ) + ) + } + trans <- function(x) log(as.numeric(x), base) inv <- function(x) as_bench_time(base ^ as.numeric(x)) @@ -211,15 +219,17 @@ scale_type.bench_time <- function(x) "bench_time" #' Default scales for the [bench_time] class, these are added to plots using #' [bench_time] objects automatically. #' @name scale_bench_time +#' @param base The base of the logarithm, if `NULL` instead use a +#' non-logarithmic scale. #' @keywords internal #' @export -scale_x_bench_time <- function(...) { - ggplot2::scale_x_continuous(..., trans = bench_time_trans()) +scale_x_bench_time <- function(base = 10, ...) { + ggplot2::scale_x_continuous(..., trans = bench_time_trans(base = base)) } #' @rdname scale_bench_time #' @keywords internal #' @export -scale_y_bench_time <- function(...) { - ggplot2::scale_y_continuous(..., trans = bench_time_trans()) +scale_y_bench_time <- function(base = 10, ...) { + ggplot2::scale_y_continuous(..., trans = bench_time_trans(base = base)) } diff --git a/man/scale_bench_time.Rd b/man/scale_bench_time.Rd index 623475b..274d6c5 100644 --- a/man/scale_bench_time.Rd +++ b/man/scale_bench_time.Rd @@ -8,13 +8,17 @@ \alias{scale_y_bench_time} \title{Position scales for bench_time data} \usage{ -scale_x_bench_bytes(...) +scale_x_bench_bytes(base = 10, ...) -scale_y_bench_bytes(...) +scale_y_bench_bytes(base = 10, ...) -scale_x_bench_time(...) +scale_x_bench_time(base = 10, ...) -scale_y_bench_time(...) +scale_y_bench_time(base = 10, ...) +} +\arguments{ +\item{base}{The base of the logarithm, if \code{NULL} instead use a +non-logarithmic scale.} } \description{ Default scales for the \link{bench_time} class, these are added to plots using