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

Excessive position guide training/drawing in facets #5427

Closed
teunbrand opened this issue Sep 18, 2023 · 1 comment · Fixed by #5431
Closed

Excessive position guide training/drawing in facets #5427

teunbrand opened this issue Sep 18, 2023 · 1 comment · Fixed by #5431

Comments

@teunbrand
Copy link
Collaborator

When using facets, position guides are trained for every panel and drawn for every row/column. This seems excessive to me.
In the code below, we can count how often a guide is trained and drawn by using the following modification. I hasten to note that this doesn't reflect the intended syntax of extending guides, but it is a quick and dirty hack.

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

train <- 0
draw  <- 0

# Setting up a counting axis
x <- guide_axis()
x$train <- function(self, params, scale, aesthetic = NULL, ...) {
  train <<- train + 1
  GuideAxis$train(params, scale, aesthetic, ...)
}
x$draw <- function(self, theme, params) {
  draw <<- draw + 1
  GuideAxis$draw(theme, params)
}

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  guides(x = x) +
  facet_grid(drv ~ cyl)

train
#> [1] 12
draw
#> [1] 4

Created on 2023-09-18 with reprex v2.0.2

Surely, we only need to train or draw* an x-axis just once in the plot above, since all panels share the same scales.
* build the grob once, then re-use for every column (not literally drawing just 1 axis).
Unless there is a very good reason to build more guides than needed, I suppose we could make this more efficient.

@teunbrand teunbrand changed the title Excessive position guide training/drawing Excessive position guide training/drawing in facets Sep 18, 2023
@teunbrand
Copy link
Collaborator Author

Similar issue upstream with viewscales:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
#> Warning: package 'testthat' was built under R version 4.3.1

viewscales <- 0

# Setting up a counting coord
coord <- coord_cartesian()
coord$setup_panel_params <- function(self, scale_x, scale_y, params) {
  viewscales <<- viewscales + 1
  ggproto_parent(CoordCartesian, self)$setup_panel_params(scale_x, scale_y, params)
}

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  coord +
  facet_grid(drv ~ cyl)

viewscales
#> [1] 12

Created on 2023-09-18 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant