Skip to content

Allow saving PDF pages #6187

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

Merged
merged 5 commits into from
Mar 25, 2025
Merged

Allow saving PDF pages #6187

merged 5 commits into from
Mar 25, 2025

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5093.

Briefly, it allows ggsave(plot) to be a list and draws every element in that list.

While this works, it has a few drawbacks:

  • No warning is thrown when device != "pdf". This is because it is hard to detect whether the current device is a PDF device that supports the onefile argument.
  • I don't think there is a way to set different bg for every page, so instead we don't use the trick where we set the background to the plot background colour.

Example usage:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()

pl <- list(
  p + ggtitle("First"),
  p + ggtitle("Second"),
  p + ggtitle("Last")
)

# `onefile = TRUE` is default, but clarifies intent
ggsave("pages.pdf", plot = pl, device = "pdf", onefile = TRUE)
#> Saving 7 x 5 in image

Created on 2024-11-15 with reprex v2.1.1

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit 5fe3c2a into tidyverse:main Mar 25, 2025
13 checks passed
@teunbrand teunbrand deleted the ggsave_pages branch March 25, 2025 09:22
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 this pull request may close these issues.

New feature to allow ggsave to save multiple plots to separate pages of pdf
2 participants