Skip to content

Excessive position guide training/drawing in facets #5427

@teunbrand

Description

@teunbrand

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions