Skip to content

Feature request: New argument axis.titles for facet plots #6420

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
jbengler opened this issue Apr 11, 2025 · 9 comments
Closed

Feature request: New argument axis.titles for facet plots #6420

jbengler opened this issue Apr 11, 2025 · 9 comments

Comments

@jbengler
Copy link

Hi Teun,
I would like to suggest a new argument axis.titles to control the display of axis titles in facet plots. In a way this would complement the existing arguments axes and axes.labels.

Image

Best
Broder

@teunbrand
Copy link
Collaborator

Hi Broder, would you suggest that all axis titles along a dimension have the same title? If not, how would one control the set the titles?

@jbengler
Copy link
Author

jbengler commented Apr 11, 2025

The same title. I am aware that this will basically duplicate information that is already present. However, one would be able to give the facets the look and feel of individual plots.

@teunbrand
Copy link
Collaborator

Thanks for elaborating! I agree that this can be convenient to have, but at this point I'd just be inclined to use patchwork to get the layout you're after. Alternatively, you (or anyone who builds extensions) could make a facet extension where you override the Facet$draw_labels() method. The extension system was build so that ggplot2 doesn't have to implement every kind of option :)

@jbengler
Copy link
Author

Thank you for thinking about it! I will follow your suggestion and have a look into Facet$draw_labels().

@jbengler
Copy link
Author

I gave it a shot with Facet$draw_labels(). However, the gaps between panels can vary in width and therefore I cannot just make three equally sized cols to draw the labels.

Image

Maybe I can get the exact positioning of the panels from somewhere?

Or I might even need to dive into the code that draws the panels itself. In this case I would probably go for FacetWrap$attach_axes() and include the labels there. However, this code is a little intimidating 😂

@teunbrand
Copy link
Collaborator

You can use panel_cols() and panel_rows() to find the correct cells in the gtable structure. However, it looks like these rectangles were drawn using freehand? If that is the case, then it might just be easier to work with the gtable structure rather than parallel to it.

However, this code is a little intimidating

It used to be part of a larger, even more intimidating chunk of code, so now it is just a smaller chunk of intimidating code.

@jbengler
Copy link
Author

However, it looks like these rectangles were drawn using freehand?

Nope, they were done with code:

  xlab_height_bottom <- grobHeight(labels$x[[2]])
  tab <- gtable(unit(rep(1, 3), "null"), xlab_height_bottom)
  tab <- gtable_add_grob(tab, rectGrob(gp = gpar(fill = "#56B4E9")), t = 1, l = 1)
  tab <- gtable_add_grob(tab, rectGrob(gp = gpar(fill = "#F5C710")), t = 1, l = 2)
  tab <- gtable_add_grob(tab, rectGrob(gp = gpar(fill = "#D55E00")), t = 1, l = 3)
  panels <- gtable_add_rows(panels, xlab_height_bottom, pos = -1)
  panels <- gtable_add_grob(panels, tab, name = "xlab-b",
                            l = panel_dim$l, r = panel_dim$r, t = -1, clip = "off")

You can use panel_cols() and panel_rows() to find the correct cells in the gtable structure.

Thanks for this great tip!

Image

Slowly getting closer...

It used to be part of a larger, even more intimidating chunk of code, so now it is just a smaller chunk of intimidating code.

Thankfully, I did not have to go in there!

@jbengler
Copy link
Author

How would you check for empty panels that do not need axis titles?
Like to bottom right one in the plot above.

@teunbrand
Copy link
Collaborator

Ah it seems like you're getting to the juicy bits of why the faceting code is so intimidating 😅 The layout data frame should have ROW and COL variables that should allow you to deduce which panels are empty.

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