teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
542 stars 33 forks source link

Error when setting strip text to element_blank() #35

Closed pcollender closed 3 years ago

pcollender commented 3 years ago

Hi there, thanks for coding this very useful extension to ggplot!

I think I found a bug that occurs when panel text is set to element_blank(). Context: I'm trying to apply the ggh4x package to have nested facets for a multi-outcome figure, which I'm arranging using the gridExtra package, trying to only keep facet labels on the leftmost layer but using facet_nested() in each strip to maintain formatting. The error can be reproduced with the following code:

`df = data.frame(x = rnorm(100), y = rnorm(100), fac1 = factor(sample(4,100, replace = T)), fac2 = factor(sample(4,100,replace = T)))

this works

ggplot(df, aes(x=x,y=y)) + geom_point() +
facet_nested(.~fac1) + theme(strip.text = element_blank())

this works

ggplot(df, aes(x=x,y=y)) + geom_point() +
facet_nested(.~fac1+fac2)

this doesn't work

ggplot(df, aes(x=x,y=y)) + geom_point() + facet_nested(.~fac1+fac2) + theme(strip.text = element_blank()) `

teunbrand commented 3 years ago

Hey there,

Thanks for letting me know! I can indeed reproduce this error and I agree that it shouldn't happen. I'll fix it here on github when I can find some time to spend on this problem.

Best wishes

teunbrand commented 3 years ago

In a twist of unexpected events, I creepily felt that I have diagnosed this issue before and it turns out I have: https://github.com/tidyverse/ggplot2/issues/4050. This is not (necessarily) an issue with ggh4x, but also occurs in ggplot2, see the following example:

library(ggplot2)

df = data.frame(x = rnorm(100), y = rnorm(100),
                fac1 = factor(sample(4,100, replace = T)),
                fac2 = factor(sample(4,100,replace = T)))

ggplot(df, aes(x=x,y=y)) + geom_point() +
  facet_grid(.~fac1+fac2) +
  theme(strip.text = element_blank())
#> Error in gtable_add_grob(panel_table, strips$x$top, 2, panel_pos_col$l, : Not all inputs have either length 1 or same length same as 'grobs'

Created on 2021-03-19 by the reprex package (v0.3.0)

I'm afraid I can't fix this on the ggh4x side, this should probably get fixed in ggplot2.

pcollender commented 3 years ago

I see. Thanks for digging!

teunbrand commented 3 years ago

Small update: I helped fixing it in ggplot2 dev version but ggh4x has extra code that also throws an error, so I'd need to dig a bit deeper.

teunbrand commented 3 years ago

This should now work without errors with the dev version of ggh4x. I'll be closing this now.

pcollender commented 3 years ago

Excellent! It works now. Thanks so much for digging into this!