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

ggsave does not respect ggplot2::unit in width and height arguments, defaults to inches #5341

Closed
hlynurhallgrims opened this issue Jun 29, 2023 · 1 comment · Fixed by #5351
Labels
documentation messages requests for improvements to error, warning, or feedback messages

Comments

@hlynurhallgrims
Copy link

I believe I've found a problem with the ggsave function. When denoting width and height using the unit function imported from the grid package. ggsave accepts the arguments without problem but actually takes the numeric value to mean inches, instead of the unit denoted.

Using the units = argument fixes this, so perhaps the only changes are warnings and changing the documentation. Let me know if you agree, and I could file a PR if needed.

Here is the code to reproduce the bug:

library(ggplot2)

ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point()

ggsave("test.png", 
       width = unit(297, "mm"), 
       height = unit(210, "mm"))
#> Error in `ggsave()`:
#> ! Dimensions exceed 50 inches (`height` and `width` are specified in
#>   inches not pixels).
#> ℹ If you're sure you want a plot that big, use `limitsize = FALSE`.
#> Backtrace:
#>     ▆
#>  1. └─ggplot2::ggsave("test.png", width = unit(297, "mm"), height = unit(210, "mm"))
#>  2.   └─ggplot2:::plot_dim(...)
#>  3.     └─cli::cli_abort(...)
#>  4.       └─rlang::abort(...)

Created on 2023-06-29 with reprex v2.0.2

@teunbrand
Copy link
Collaborator

Thanks for spotting this. Yeah I'd agree that the documentation is a little bit on the ambiguous side; half-suggesting to use grid::unit() whereas one shouldn't. Throwing an error when is.unit(width) || is.unit(height) would be informative I think. A PR would we welcome!

@teunbrand teunbrand added documentation messages requests for improvements to error, warning, or feedback messages labels Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation messages requests for improvements to error, warning, or feedback messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants