Skip to content

Allow saving PDF pages - #6187

Merged
teunbrand merged 5 commits into
tidyverse:mainfrom
teunbrand:ggsave_pages
Mar 25, 2025
Merged

teunbrand merged 5 commits into
tidyverse:mainfrom
teunbrand:ggsave_pages

Conversation

@teunbrand

Copy link
Copy Markdown
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

Comment thread R/save.R Outdated
Comment thread R/save.R

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

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
@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