From c2b7e0ca6cd8f762493d03dfc8ad76ce9f946f55 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Sun, 24 Jan 2016 22:08:25 +0100 Subject: [PATCH 1/6] Resolve data upon extraction within ggplot_build --- R/plot-build.r | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/plot-build.r b/R/plot-build.r index d4ef2a2fda..c8b3bfa724 100644 --- a/R/plot-build.r +++ b/R/plot-build.r @@ -22,7 +22,13 @@ ggplot_build <- function(plot) { } layers <- plot$layers - layer_data <- lapply(layers, function(y) y$data) + layer_data <- lapply(layers, function(y) { + if (is.function(y$data)) { + fortify(y$data(plot$data)) + } else { + y$data + } + }) scales <- plot$scales # Apply function to layer and matching data From d10d2e86e3cac737f92d9168e99ba516f8fc7366 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Thu, 25 Oct 2018 15:45:34 +0200 Subject: [PATCH 2/6] Memoize calls to descentDetails() --- R/margins.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/R/margins.R b/R/margins.R index 300e6a6bde..17b85c6c9c 100644 --- a/R/margins.R +++ b/R/margins.R @@ -63,8 +63,7 @@ title_spec <- function(label, x, y, hjust, vjust, angle, gp = gpar(), # has the common letters with descenders. This guarantees that the grob always has # the same height regardless of whether the text actually contains letters with # descenders or not. The same happens automatically with ascenders already. - temp <- editGrob(text_grob, label = "gjpqyQ") - descent <- descentDetails(temp) + descent <- font_descent(gp$fontfamily, gp$fontface, gp$fontsize, gp$cex) # Use trigonometry to calculate grobheight and width for rotated grobs. This is only # exactly correct when vjust = 1. We need to take the absolute value so we don't make @@ -329,3 +328,23 @@ rotate_just <- function(angle, hjust, vjust) { list(hjust = hnew, vjust = vnew) } +descent_cache <- new.env(parent = emptyenv()) +font_descent <- function(family = "", face = "plain", size = 12, cex = 1) { + key <- paste0(family, ':', face, ":", size, ":", cex) + + descent <- descent_cache[[key]] + + if (is.null(descent)) { + descent <- descentDetails(textGrob( + label = "gjpqyQ", + gp = gpar( + fontsize = size, + cex = cex, + fontfamily = family, + fontface = face + ) + )) + descent_cache[[key]] <- descent + } + descent +} From 47ef11da8af97c248949653ce163051952ecbff0 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Fri, 9 Nov 2018 11:45:51 +0100 Subject: [PATCH 3/6] memoise by the current device as well --- R/margins.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/margins.R b/R/margins.R index 17b85c6c9c..676685c6a5 100644 --- a/R/margins.R +++ b/R/margins.R @@ -330,7 +330,8 @@ rotate_just <- function(angle, hjust, vjust) { } descent_cache <- new.env(parent = emptyenv()) font_descent <- function(family = "", face = "plain", size = 12, cex = 1) { - key <- paste0(family, ':', face, ":", size, ":", cex) + cur_dev <- names(dev.cur()) + key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex) descent <- descent_cache[[key]] From 72c351f5e7efde7b8f72ed2d5d26fc5a5cb17ac6 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Fri, 9 Nov 2018 13:34:31 +0100 Subject: [PATCH 4/6] import dev.cur --- DESCRIPTION | 3 ++- R/margins.R | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 48df3a3a5f..9f0ab172dd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,7 +31,8 @@ Imports: stats, tibble, viridisLite, - withr (>= 2.0.0) + withr (>= 2.0.0), + grDevices Suggests: covr, dplyr, diff --git a/R/margins.R b/R/margins.R index 676685c6a5..4ddfe6135a 100644 --- a/R/margins.R +++ b/R/margins.R @@ -330,7 +330,7 @@ rotate_just <- function(angle, hjust, vjust) { } descent_cache <- new.env(parent = emptyenv()) font_descent <- function(family = "", face = "plain", size = 12, cex = 1) { - cur_dev <- names(dev.cur()) + cur_dev <- names(grDevices::dev.cur()) key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex) descent <- descent_cache[[key]] From 97fd57680c270096801e78e25adef6895b61b5ac Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Mon, 12 Nov 2018 11:37:49 +0100 Subject: [PATCH 5/6] Add description to vignette --- vignettes/profiling.Rmd | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vignettes/profiling.Rmd b/vignettes/profiling.Rmd index 6bc6e21576..4e2a31b79c 100644 --- a/vignettes/profiling.Rmd +++ b/vignettes/profiling.Rmd @@ -55,3 +55,9 @@ To keep track of changes focused on improving the performance of gtable they are summarised below: ### v`r packageVersion('ggplot2')` + +- **Caching of calls to `grid::descentDetails()`** The absolute biggest offender + was the construction of titles. In recent versions this has included calls to + `grid::descentDetails()` to ensure that they are aligned across plots, but + this is quite heavy. These calls are now cached so they only have to be + calculated once per font setting. From 9e6933b4a8c5a2743e461f1a49488fec8eed39d6 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Mon, 12 Nov 2018 15:07:34 +0100 Subject: [PATCH 6/6] Make dependency lists alphabetic --- DESCRIPTION | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6479a3229d..89fae1d9b3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,6 +20,7 @@ Depends: R (>= 3.1) Imports: digest, + grDevices, grid, gtable (>= 0.1.1), lazyeval, @@ -32,14 +33,14 @@ Imports: stats, tibble, viridisLite, - withr (>= 2.0.0), - grDevices + withr (>= 2.0.0) Suggests: covr, dplyr, ggplot2movies, hexbin, Hmisc, + knitr, lattice, mapproj, maps, @@ -47,16 +48,15 @@ Suggests: multcomp, munsell, nlme, - testthat (>= 0.11.0), - vdiffr, + profvis, quantreg, - knitr, rgeos, - rpart, rmarkdown, + rpart, sf (>= 0.3-4), svglite (>= 1.2.0.9001), - profvis + testthat (>= 0.11.0), + vdiffr Enhances: sp License: GPL-2 | file LICENSE URL: http://ggplot2.tidyverse.org, https://github.com/tidyverse/ggplot2