teunbrand / ggh4x

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

Error when combining facet_nested() with patchwork #4

Closed eliocamp closed 4 years ago

eliocamp commented 4 years ago

I'm trying to combine two plots which use facet_nested() and it's not working. I don't know if is an issue in ggh4x or patchwork but there's at least another package using facets that is having problems. I reported the issue in this comment: https://github.com/thomasp85/patchwork/issues/151#issuecomment-599729039

teunbrand commented 4 years ago

Hi Elio,

Thank you for bringing this to my attention. I'm not entirely sure with how patchwork handles facets, but if I recall correctly from patchwork's author's tweets it has been quite an effort to make patchwork work for grobs that span multiple gtable cells. The way facet_nested() nests the facet strips is to have strips spanning multiple cells in the gtable. I'd have to delve a bit deeper into how patchwork handles the facet bits and see if I could come up with a strategy that would make the two compatible and where a bugfix might be most appropriate.

P.S.: love the ggnewscale package

eliocamp commented 4 years ago

Thomas commented on that issue.

Yes. Patchwork makes some assumptions about facets in order to be able to align them properly. I have yet to formalise a description of these assumptions to guide facet extension developers but this should of course be done

It seems like any fix made now could be broken in the future, but maybe some temporary workaround could be useful.

PS: Thanks! And I love facet_nested()!

teunbrand commented 4 years ago

I think this might be fixed now. I haven't exhaustively tested all interactions with patchwork but the following should work:

library(ggh4x)
#> Loading required package: ggplot2
library(patchwork)

df <- iris
df$nester <- ifelse(df$Species == "setosa",
                    "Short Leaves",
                    "Long Leaves")

g <- ggplot(df, aes(Sepal.Length, Petal.Length)) +
  geom_point()
g1 <- g + facet_nested(nester + Species ~ .)
g2 <- g + facet_nested(~ nester + Species)

g1 + g2

Created on 2020-03-17 by the reprex package (v0.3.0)

Please let me know if you stumble upon any bugs with this fix!

teunbrand commented 4 years ago

Found out that it now has a weird interaction with force_panelsizes(), might need to fix some more. Posted as seperate issue in #6

eliocamp commented 4 years ago

I'm still getting an error (Error in grid.Call.graphics(C_setviewport, vp, TRUE) : invalid 'layout.pos.col') in my plots, but not in the minimal example I posted in the patchwork issue. I'll try to get a mini reprex working.

teunbrand commented 4 years ago

Hey @eliocamp,

I've messed with the strip code while trying to fix issue #6. I had hoped that it would also fix your error, but since I don't know how to reproduce it, it would be nice if either you could provide a reprex, or check if your error is fixed now.

Thanks in advance!

teunbrand commented 4 years ago

I'm going to consider this issue closed for now, but will reopen if a reprex can be found.

eliocamp commented 4 years ago

Sorry. I've been busy with other code. As soon as I come back to it, I'll try to reproduce it.

teunbrand commented 4 years ago

No worries, don't feel obligated or pressured. I'm not in a hurry