Skip to content

Add new parameter "preset" for ggsave() to specify width/height at once#2781

Closed
ilarischeinin wants to merge 3 commits intotidyverse:masterfrom
ilarischeinin:ggsave-presets
Closed

Add new parameter "preset" for ggsave() to specify width/height at once#2781
ilarischeinin wants to merge 3 commits intotidyverse:masterfrom
ilarischeinin:ggsave-presets

Conversation

@ilarischeinin
Copy link

This PR adds a new parameter preset for ggsave() that allows plot width and height (and units) to be specified at once. It recognizes common paper sizes (such as "a4" and "letter"), common screen resolutions ("4k"), and resolution specifications such as "1920x1080".

All presets default to landscape orientation, and portrait can be specified by appending an "r" (for rotated) to the end of the string. This is similar to the paper option for grDevices::pdf(), but with landscape being the default as it's probably a more common choice for plots.

(Naturally this new parameter could also be called paper instead of preset, but 1) that felt a bit too old-fashioned, and 2) might seem odd when making a plot that is to be placed on a sheet of paper together with other elements like text. size is another name I considered, but as the function already has a parameter called limitsize, I think they would be an odd pair. Anyways, preset is just one suggestion and I can rename it as wished.)

For now, some selected paper sizes and screen resolutions are included, but I'm happy to make the list more comprehensive if that seems reasonable.

Example Usage

library(ggplot2)
p <- ggplot(data.frame(x = rnorm(100), y = rnorm(100)), aes(x, y)) + geom_point()

ggsave("letter.pdf", p, preset = "letter")
ggsave("a4r.pdf", p, preset = "a4r")
ggsave("hd.png", p, preset = "HD")
ggsave("1920x1080.png", p, preset = "1920x1080")

Presets can be used to specify plot width and height (and units) at
once. It recognizes common paper sizes (such as "a4" and "letter"),
common screen resolutions ("4k"), or a resolution specification such
as "1920x1080". All presets default to landscape orientation, and
portrait can be specified by appending an "r" (for rotated) to the end
of the string. This is similar to the "paper" option for
grDevices::pdf(), but with landscape being the default as it's
probably a more common choice for plots.
@thomasp85
Copy link
Member

@hadley, can you comment on whether you'd want this in ggplot2? If you do, I'll give it a proper review

@thomasp85 thomasp85 added this to the ggplot2 3.2.0 milestone Apr 11, 2019
@hadley
Copy link
Member

hadley commented Apr 11, 2019

This doesn't feel quite right for ggplot2 to me. It's a bit too big of a change to a very old function, and updating the preset list over time seems likely to add to the maintenance burden.

@thomasp85
Copy link
Member

Agreed.

@thomasp85 thomasp85 closed this Apr 11, 2019
@lock
Copy link

lock bot commented Oct 8, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants