tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.49k stars 2.02k forks source link

Feature request: Add `gg` class in ggplot2::labs() #5553

Closed Yunuuuu closed 1 month ago

Yunuuuu commented 10 months ago

If both functions perform the same tasks for ggplot2 elements, we can use a single function like ggbuild_fn to re-dispatch methods. However, the issue arises when the value of ggplot2::labs() does not have a gg class.

# If both function do same things for ggplot2 elements
# we can re-dispatch methods with a single function like `ggbuild_fn`
first_fn <- function(x) {
    UseMethod("first_fn")
}
first_fn.gg <- function(x) {
    ggbuild_fn(x)
}
second_fn <- function(x) {
    UseMethod("second_fn")
}
second_fn.gg <- function(x) {
    ggbuild_fn(x)
}
# re-dispatch methods for exact ggplot2 elements
ggbuild_fn <- function(x) {
    UseMethod("ggbuild_fn")
}
ggbuild_fn.theme <- function(x) {
    # do something ...
}

ggbuild_fn.Scale <- function(x) {
    # do something ...
}

ggbuild_fn.labels <- function(x) {
    # do something ...
}

ggbuild_fn.Layer <- function(x) {
    # do something ...
}
class(ggplot2::scale_alpha_binned())
#> [1] "ScaleBinned" "Scale"       "ggproto"     "gg"
class(ggplot2::geom_point())
#> [1] "LayerInstance" "Layer"         "ggproto"       "gg"
class(ggplot2::theme())
#> [1] "theme" "gg"
class(ggplot2::labs())
#> [1] "labels"

Created on 2023-12-01 with reprex v2.0.2

teunbrand commented 10 months ago

I'm not quite sure what the scope of the example is, but I can see some merit in attaching a "gg" class to the labels.