Skip to content

strip.text.y = element_blank() yields an error when there are multiple layers of strip labels #4050

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

Closed
yqgchen opened this issue Jun 7, 2020 · 3 comments · Fixed by #4384
Labels
bug an unexpected problem or unintended behavior facets 💎

Comments

@yqgchen
Copy link

yqgchen commented Jun 7, 2020

Hello,
I was trying to remove the strip labels. I found that setting strip.text.x = element_blank() works when there is only one layer of strip labels and yields an error when there are multiple layers of labels. Is there any way to remove the strip labels in the latter case? Many thanks!

# This works:
p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() +
 facet_grid(vs + am ~ gear)  + theme(strip.text.x = element_blank())

#This does not work:
p2 <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() +
 facet_grid(vs + am ~ gear)  + theme(strip.text.y = element_blank())
#Error in gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t,  : 
#  Not all inputs have either length 1 or same length same as 'grobs'
@yutannihilation
Copy link
Member

Thanks, confirmed. The error comes from here:

panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -2, clip = "on", name = paste0("strip-r-", seq_along(strips$y$right)), z = 2)

  # Check that inputs have the right length
  if (!all(vapply(
    list(t, r, b, l, z, clip, name), len_same_or_1,
    logical(1), n_grobs
  ))) {
    stop("Not all inputs have either length 1 or same length same as 'grobs'")
  }

https://github.com/r-lib/gtable/blob/4fb86c724c0555de9ce3fce867e2320fe9aeb9c2/R/add-grob.r#L57-L63

I have no idea what's happening here, sorry...

@teunbrand
Copy link
Collaborator

teunbrand commented Jun 9, 2020

I have an idea where this problem comes form. If the strips texts are rendered as blanks, you get strangely formatted grobs for the strips with incorrect lengths.

library(ggplot2)

# Create full theme and theme with blank strip.text.y.right
theme_full <- theme_gray()
theme_blank <- theme_full + theme(strip.text.y.right = element_blank())

# Dummy labels
dummy <- data.frame(c("a", "b"), "b")

# Render strips
strip_full  <- ggplot2:::render_strips(dummy, dummy, label_value, theme_full)
strip_blank <- ggplot2:::render_strips(dummy, dummy, label_value, theme_blank)

# The normal strips are a list of tablegrobs
strip_full$y$right
#> [[1]]
#> TableGrob (1 x 2) "strip": 2 grobs
#>   z     cells  name                  grob
#> 1 1 (1-1,1-1) strip gTree[strip.gTree.83]
#> 2 2 (1-1,2-2) strip gTree[strip.gTree.87]
#> 
#> [[2]]
#> TableGrob (1 x 2) "strip": 2 grobs
#>   z     cells  name                  grob
#> 1 1 (1-1,1-1) strip gTree[strip.gTree.85]
#> 2 2 (1-1,2-2) strip gTree[strip.gTree.89]

# The 'blank' themed strips are a matrix of lists?
strip_blank$y$right
#>      [,1]   [,2]  
#> [1,] List,3 List,3
#> [2,] List,3 List,3

# They have different lengths
length(strip_full$y$right)
#> [1] 2
length(strip_blank$y$right)
#> [1] 4

When you then naively try to add the blank strips to a gtable and not all arguments are length 1 you can get the error described. Below I use t = c(1, 2) because the surrounding code would assume render_strips() returns a length two list.

library(gtable)

gt <- gtable(unit(c(1, 1), "cm"), unit(c(1, 1), "cm"))

# Works fine
new_full <- gtable_add_grob(gt, strip_full$y$right, t = c(1, 2), l = 1)

# Returns the error
new_blank <- gtable_add_grob(gt, strip_blank$y$right, t=  c(1, 2), l = 1)
#> Error in gtable_add_grob(gt, strip_blank$y$right, t = c(1, 2), l = 1): Not all inputs have either length 1 or same length same as 'grobs'

Created on 2020-06-09 by the reprex package (v0.3.0)

I hope this could help narrow the scope of the problem a bit.

@yutannihilation
Copy link
Member

Thanks for your insight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior facets 💎
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants