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

Could functionality be provided to easily retrieve plot height and plot width neatly? #6367

Closed
davidhodge931 opened this issue Mar 19, 2025 · 5 comments

Comments

@davidhodge931
Copy link

Awesome new feature re theme panel.widths and panel.heights!

Could ggplot2 provide get_plot_height and get_plot_width functions to support saving plots where the panel.widths and panel.heights have been set.

Basically, it'd be super useful, if I could do something like this:

library(tidyverse)
library(palmerpenguins)

p <- penguins |>
  drop_na(sex) |>
  ggplot() +
  geom_point(
    aes(x = bill_depth_mm,
        y = body_mass_g,
        colour = species),
  ) +
  theme(panel.widths = rep(unit(50, "mm"), times = 100)) +
  theme(panel.heights = rep(unit(50, "mm"), times = 100))

ggsave(
  plot = p, 
  filename = "temp.png",
  height = get_plot_height(p), 
  width = get_plot_width(p), 
)
@davidhodge931
Copy link
Author

The get_plot_height and get_plot_width could also have a units argument that could match that of ggsave

@teunbrand
Copy link
Collaborator

I like this idea, but I think we should stick with the interface that {grid} already provides; e.g. make a widthDetails and heightDetails methods for ggplot2 objects.

Secondly, we could consider letting ggsave() interpret grid units and/or make it easy to use native plot dimensions.

@davidhodge931 davidhodge931 changed the title Could a get_plot_height and get_plot_width function be provided? Could functionality be provided to retrieve plot height and plot width easily? Mar 19, 2025
@davidhodge931
Copy link
Author

davidhodge931 commented Mar 19, 2025

I basically just want an easy way to save plots with specified panel heights/widths neatly. Open to any way you think is best to achieve this. But prefer not to deal with grobs, if I can. The status quo method seems a bit complex for a relatively common use-case. Renamed the issue to make it less prescriptive

@davidhodge931 davidhodge931 changed the title Could functionality be provided to retrieve plot height and plot width easily? Could functionality be provided to easily retrieve plot height and plot width neatly? Mar 19, 2025
@davidhodge931
Copy link
Author

One other thought: ideally the functionality could work with either a ggplot or patchwork object input

@thomasp85
Copy link
Member

We have been here before (though I can't find the other issues so you are forgiven for not knowing 🙂).

There is a circular dependency between the graphics device and getting the dimensions of the plot, because the size of text is device dependent in a myriad of ways. Because of this you cannot reliably know apriori how big a plot is before opening the device and trying to plot it. This is usually not an issue because the flexible panel area expands or contracts to fill the space but once that has been fixed in dimensions you can at best make an educated guess as to how much space it takes up.

Due to this uncertainty I have declined such functionality in ggplot2 as I don't want it to make promises it can't keep. There is probably room in an extension package for such functions though

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

Successfully merging a pull request may close this issue.

3 participants