Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better breaks for scale_*_bench_time() #50

Open
paleolimbot opened this issue Jun 17, 2019 · 2 comments
Open

Better breaks for scale_*_bench_time() #50

paleolimbot opened this issue Jun 17, 2019 · 2 comments
Labels
feature a feature request or enhancement

Comments

@paleolimbot
Copy link

paleolimbot commented Jun 17, 2019

@hadley suggested that perhaps your vroom benchmarks could have nicer breaks for the values that aren't in seconds. This is a quick fix that uses the excellent rounding and parsing code for format.bench_time() and as_bench_time.default():

bench_time_breaks <- function(n = 5, base = 10, digits = 0) {
  force(digits)
  log_breaks <- scales::log_breaks(n = n, base = base)
  function(x) {
    log_breaks_secs <- scales::log_breaks(n = n, base = base)(x)
    bench_time_formatted <- bench:::format.bench_time(log_breaks_secs, digits = digits, trim = TRUE)
    bench:::as_bench_time.default(bench_time_formatted)
  }
}

library(ggplot2)
df <- tibble::tibble(x = 10^((-6):4))
p <- ggplot(df, aes(x, 1)) + geom_point()  

p + bench:::scale_x_bench_time(labels = bench:::format.bench_time)

p + bench:::scale_x_bench_time(breaks = bench_time_breaks(), labels = bench:::format.bench_time)

Created on 2019-06-17 by the reprex package (v0.2.1)

I imagine that benchmarking things that are longer than a minute are uncommon...we will probably use some version of this for a set of scale_*_difftime() functions in scales/ggplot2 at some point.

@jimhester
Copy link
Member

Agreed that would be nice, looks good!

The vroom benchmarks actually aren't using scale_x_bench_time, they are just using
scale_y_continuous(labels = function(x) format(bench::as_bench_time(x))), but nicer breaks would definitely be nice!

@paleolimbot
Copy link
Author

For posterity,

bench_time_breaks <- function(breaks = scales::extended_breaks(), digits = 0) {
  force(breaks)
  force(digits)
  function(x) {
    log_breaks_secs <- breaks(x)
    bench_time_formatted <- bench:::format.bench_time(log_breaks_secs, digits = digits, trim = TRUE)
    bench:::as_bench_time.default(bench_time_formatted)
  }
}

library(ggplot2)
df <- tibble::tibble(x = c(2, 10*60))
p <- ggplot(df, aes(x, 1)) + geom_point()  

p + scale_x_continuous(labels = bench:::format.bench_time, breaks = bench_time_breaks())

Created on 2019-06-18 by the reprex package (v0.2.1)

@jimhester jimhester added the feature a feature request or enhancement label Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants