Skip to content

Adding NULL to a theme #6291

Closed
Closed
@teunbrand

Description

@teunbrand

This was identified in #6287 and affects 16 packages.

You can add NULL to a regular theme and it will be a no-op.
However, some packages don't also have the "gg" class for their themes, which causes problems when adding NULL.

library(ggplot2)

class(theme())
#> [1] "theme" "gg"
class(survminer::theme_survminer())
#> [1] "theme"

theme() + NULL
#>  Named list()
#>  - attr(*, "class")= chr [1:2] "theme" "gg"
#>  - attr(*, "complete")= logi FALSE
#>  - attr(*, "validate")= logi TRUE
survminer::theme_survminer() + NULL
#> Error in survminer::theme_survminer() + NULL: non-numeric argument to binary operator

Created on 2025-01-22 with reprex v2.1.1

This can become a problem in the chunk of code below:

ggplot2/R/guides-.R

Lines 541 to 549 in 6a61201

adjust <- NULL
position <- groups$key$position[i]
if (position == "inside") {
adjust <- theme(
legend.position.inside = groups$key$coord[[i]],
legend.justification.inside = groups$key$justs[[i]]
)
}
grobs[[i]] <- self$package_box(grobs[[i]], position, theme + adjust)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions