Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
devtools::load_all("~/repo/R/ggplot2")
#> Loading ggplot2

p <- ggplot(base::data.frame(x = 1:3)) +
  geom_point(aes(x, x, colour = x))

get_colourbar <- function(nbin) {
  p <- p + scale_color_gradient(
    paste0("nbin =\n", nbin),
    guide = guide_colorbar(barheight = grid::unit(1, "npc") - grid::unit(4, "line"),nbin = nbin)
  )
  data <- ggplot_build(p)
  g <- ggplot_gtable(data)
  g$grobs[[15]]
}

gridExtra::grid.arrange(
  get_colourbar(20),
  get_colourbar(40),
  get_colourbar(60),
  get_colourbar(80),
  get_colourbar(100),
  get_colourbar(150),
  get_colourbar(200),
  get_colourbar(250),
  get_colourbar(300),
  get_colourbar(400),
  get_colourbar(500),
  nrow = 1
)

res <- bench::mark(
  `nbin = 020` = grid.draw(get_colourbar(20)),
  `nbin = 100` = grid.draw(get_colourbar(100)),
  `nbin = 200` = grid.draw(get_colourbar(200)),
  `nbin = 300` = grid.draw(get_colourbar(300)),
  `nbin = 500` = grid.draw(get_colourbar(500)),
  check = FALSE,
  min_iterations = 30
)

autoplot(res)
#> Loading required namespace: tidyr

Created on 2019-09-08 by the reprex package (v0.3.0)

devtools::load_all("~/repo/R/ggplot2")
#> Loading ggplot2

p <- ggplot(base::data.frame(x = 1:3)) +
  geom_point(aes(x, x, colour = x))

get_colourbar <- function(nbin) {
  p <- p + scale_color_gradient(
    paste0("nbin =\n", nbin),
    guide = guide_colorbar(barheight = grid::unit(1, "npc") - grid::unit(4, "line"),nbin = nbin)
  )
  data <- ggplot_build(p)
  g <- ggplot_gtable(data)
  g$grobs[[15]]
}

gridExtra::grid.arrange(
  get_colourbar(20),
  get_colourbar(40),
  get_colourbar(60),
  get_colourbar(80),
  get_colourbar(100),
  get_colourbar(150),
  get_colourbar(200),
  get_colourbar(250),
  get_colourbar(300),
  get_colourbar(400),
  get_colourbar(500),
  nrow = 1
)

res <- bench::mark(
  `nbin = 020` = grid.draw(get_colourbar(20)),
  `nbin = 100` = grid.draw(get_colourbar(100)),
  `nbin = 200` = grid.draw(get_colourbar(200)),
  `nbin = 300` = grid.draw(get_colourbar(300)),
  `nbin = 500` = grid.draw(get_colourbar(500)),
  check = FALSE,
  min_iterations = 30
)

autoplot(res)
#> Loading required namespace: tidyr

Created on 2019-09-08 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.