Skip to content

Cannot set legend.box.background with theme_void() #2723

Closed

Description

@clauswilke

It doesn't seem possible to set the legend.box.background theme setting under theme_void(). Other legend settings work.

Reprex:

library(ggplot2)
df <- data.frame(x = 1:3)
ggplot(df, aes(x, x, color = factor(x))) +
  geom_point() +
  theme_void() +
  theme(legend.background = element_rect(fill = "gray80"))

df <- data.frame(x = 1:3)
ggplot(df, aes(x, x, color = factor(x))) +
  geom_point() +
  theme_void() +
  theme(legend.box.background = element_rect(fill = "gray80"))
#> Warning in if (theme$legend.box == "horizontal") {: the condition has
#> length > 1 and only the first element will be used
#> Warning in if (theme$legend.box == "vertical") {: the condition has length
#> > 1 and only the first element will be used
#> Error in gtable_add_cols(guides, theme$legend.box.margin[4], pos = 0): is.gtable(x) is not TRUE

Created on 2018-06-28 by the reprex package (v0.2.0).

Activity

  1. clauswilke commented on Jun 28, 2018

    @clauswilke
    MemberAuthor

    Also, this works without theme_void(), of course:

    library(ggplot2)
    df <- data.frame(x = 1:3)
    ggplot(df, aes(x, x, color = factor(x))) +
      geom_point() +
      #theme_void() +
      theme(legend.box.background = element_rect(fill = "gray80"))

    Created on 2018-06-28 by the reprex package (v0.2.0).

  2. ptoche commented on Jun 28, 2018

    @ptoche

    works for me:

    sessionInfo()
    R version 3.5.0 (2018-04-23)
    Platform: x86_64-apple-darwin15.6.0 (64-bit)
    Running under: macOS High Sierra 10.13.4

    Matrix products: default
    BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
    LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

    attached base packages:
    [1] stats graphics grDevices utils
    [5] datasets methods base

    other attached packages:
    [1] ggplot2_2.2.1

    loaded via a namespace (and not attached):
    [1] labeling_0.3 colorspace_1.3-2
    [3] scales_0.5.0 compiler_3.5.0
    [5] lazyeval_0.2.1 plyr_1.8.4
    [7] tools_3.5.0 pillar_1.2.3
    [9] gtable_0.2.0 tibble_1.4.2
    [11] yaml_2.1.19 Rcpp_0.12.17
    [13] grid_3.5.0 digest_0.6.15
    [15] rlang_0.2.1 munsell_0.5.0

  3. batpigandme commented on Jun 28, 2018

    @batpigandme
    Member

    I'm able to reproduce this with the latest dev. build.

    Traceback

    9: stop(msg, call. = FALSE, domain = NA)
    8: stopifnot(is.gtable(x))
    7: gtable_add_cols(guides, theme$legend.box.margin[4], pos = 0)
    6: guides_build(ggrobs, theme)
    5: build_guides(plot$scales, plot$layers, plot$mapping, position, 
           theme, plot$guides, plot$labels)
    4: ggplot_gtable.ggplot_built(data)
    3: ggplot_gtable(data)
    2: print.ggplot(x)
    1: function (x, ...) 
       UseMethod("print")(x)
    
  4. clauswilke commented on Jun 28, 2018

    @clauswilke
    MemberAuthor

    It's probably related to the changes in the theme inheritance code in the latest version. Some edge case that wasn't considered.

  5. clauswilke commented on Jun 28, 2018

    @clauswilke
    MemberAuthor

    I've figured it out. The immediate cause is a missing legend.box element in the theme:

    library(ggplot2)
    df <- data.frame(x = 1:3)
    ggplot(df, aes(x, x, color = factor(x))) +
      geom_point() +
      theme_void() +
      theme(
        legend.box = "horizontal",
        legend.box.background = element_rect(fill = "gray80")
      )

    Created on 2018-06-28 by the reprex package (v0.2.0).

    The ulterior reason is this if--else sequence:

    ggplot2/R/guides-.r

    Lines 251 to 280 in 348b26f

    if (theme$legend.box == "horizontal") {
    # Set justification for each legend
    for (i in seq_along(ggrobs)) {
    ggrobs[[i]] <- editGrob(ggrobs[[i]],
    vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust),
    height = heightDetails(ggrobs[[i]])))
    }
    guides <- gtable_row(name = "guides",
    grobs = ggrobs,
    widths = widths, height = max(heights))
    # add space between the guide-boxes
    guides <- gtable_add_col_space(guides, theme$legend.spacing.x)
    } else if (theme$legend.box == "vertical") {
    # Set justification for each legend
    for (i in seq_along(ggrobs)) {
    ggrobs[[i]] <- editGrob(ggrobs[[i]],
    vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust),
    width = widthDetails(ggrobs[[i]])))
    }
    guides <- gtable_col(name = "guides",
    grobs = ggrobs,
    width = max(widths), heights = heights)
    # add space between the guide-boxes
    guides <- gtable_add_row_space(guides, theme$legend.spacing.y)
    }

    It contains no default if legend.box is neither "horizontal" nor "vertical". I think this line:

    } else if (theme$legend.box == "vertical") {

    should be turned into an unconditional else.

  6. karawoo commented on Jun 28, 2018

    @karawoo
    Member

    I think the fix should actually occur earlier in this section of build_guides():

    ggplot2/R/guides-.r

    Lines 102 to 114 in 348b26f

    if (position == "inside") {
    theme$legend.box <- theme$legend.box %||% "vertical"
    theme$legend.direction <- theme$legend.direction %||% "vertical"
    theme$legend.box.just <- theme$legend.box.just %||% c("center", "center")
    } else if (position == "vertical") {
    theme$legend.box <- theme$legend.box %||% "vertical"
    theme$legend.direction <- theme$legend.direction %||% "vertical"
    theme$legend.box.just <- theme$legend.box.just %||% c("left", "top")
    } else if (position == "horizontal") {
    theme$legend.box <- theme$legend.box %||% "horizontal"
    theme$legend.direction <- theme$legend.direction %||% "horizontal"
    theme$legend.box.just <- theme$legend.box.just %||% c("center", "top")
    }

    theme$legend.box is NULL but theme$legend.box.background is not, and thus theme$legend.box %||% "vertical" is partially matching on theme$legend.box.background and inserting the element_rect() into theme$legend.box.

  7. clauswilke commented on Jun 29, 2018

    @clauswilke
    MemberAuthor

    @karawoo Can you think of a clean and simple fix at line 102?

    In any case, I think it's always a good idea to have a default option in an if--else chain, so I would change line 266 regardless. And theme_void() needs to define legend.box.

  8. karawoo commented on Jun 29, 2018

    @karawoo
    Member

    The easiest I guess would be to change theme$legend.box %||% "vertical" to theme[["legend.box"]] %||% "vertical". Agreed on the other points though.

  9. added a commit that references this issue on Jun 29, 2018
    f68ecad
  10. clauswilke commented on Jun 29, 2018

    @clauswilke
    MemberAuthor

    @karawoo I'm moving that discussion to separate issue #2724.

  11. lock commented on Jan 5, 2019

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  12. locked and limited conversation to collaborators on Jan 5, 2019
  13. added a commit that references this issue on Apr 1, 2026
    3129bf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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