Skip to content

Commit

Permalink
Allow setting a non-logarithmic scales
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhester committed Oct 26, 2018
1 parent 937be22 commit 3e5d63f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
2 changes: 2 additions & 0 deletions 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
Expand Down
3 changes: 3 additions & 0 deletions R/autoplot.R
Expand Up @@ -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)

Expand Down
17 changes: 12 additions & 5 deletions R/bytes.R
Expand Up @@ -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))

Expand All @@ -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))
}
18 changes: 14 additions & 4 deletions R/time.R
Expand Up @@ -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))

Expand All @@ -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))
}
12 changes: 8 additions & 4 deletions man/scale_bench_time.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e5d63f

Please sign in to comment.