teunbrand / ggh4x

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

Add tag_facet function? #149

Open nonsciencemark opened 7 months ago

nonsciencemark commented 7 months ago

Would it be possible to swipe the tag_facet function from egg and incorporate it such that it plays nicely with facet_nested?

I think the only thing that's needed is to take the current version:

tag_facet <- function(p, open = "(", close = ")", tag_pool = letters, x = -Inf, y = Inf, 
                      hjust = -0.5, vjust = 1.5, fontface = 2, family = "", ...) {
    gb <- ggplot_build(p)
    lay <- gb$layout$layout
    tags <- cbind(lay, label = paste0(open, tag_pool[lay$PANEL], close), x = x, y = y)
    p + geom_text(
        data = tags, aes_string(x = "x", y = "y", label = "label"), ..., hjust = hjust, vjust = vjust,
            fontface = fontface, family = family, inherit.aes = FALSE) +
        theme(strip.text = element_blank(), strip.background = element_blank())
}

and remove that last theme line and then we get beautiful, automatically-labelled facets. I would consider this to be h4x and egg hasn't been updated in 4 years

nonsciencemark commented 7 months ago

Hmm actually it'd need to be more complicated than that to allow for empty plots which currently get counted

teunbrand commented 7 months ago

It is not too terrible to do it manually, see this tip for example. It is just a geom_label() call. These days you can even use size.unit = "pt" to coordinate sizes with the theme better.

teunbrand commented 5 months ago

Thinking about this some more, this could probably be implemented as a type of strip. So that you could then do facet_grid2(..., strip = strip_tag()) or something.

nonsciencemark commented 5 months ago

Sorry - last couple of months have been hectic. I shall try this out, thanks !

teunbrand commented 5 months ago

This won't work at the moment, it was more of a note to self on why this issue shouldn't be closed.