Skip to content
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

Add tag_facet function? #149

Open
nonsciencemark opened this issue Feb 29, 2024 · 5 comments
Open

Add tag_facet function? #149

nonsciencemark opened this issue Feb 29, 2024 · 5 comments

Comments

@nonsciencemark
Copy link

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
Copy link
Author

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

@teunbrand
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

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

@teunbrand
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants