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

Allow saving PDF pages #6187

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

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

@@ -102,7 +102,7 @@ ggsave <- function(filename, plot = get_last_plot(),
dim <- plot_dim(c(width, height), scale = scale, units = units,
limitsize = limitsize, dpi = dpi)

if (is_null(bg)) {
if (is_null(bg) && is.ggplot(plot)) {
Copy link
Member

Choose a reason for hiding this comment

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

In the case a list is passed in, I think we should just grab the bg from the first plot in the list rather than completely ignore it

if (!is_bare_list(plot)) {
plot <- list(plot)
}
lapply(plot, grid.draw)
Copy link
Member

Choose a reason for hiding this comment

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

There should be a grid.newpage() in between every draw call

Copy link
Collaborator Author

@teunbrand teunbrand Jan 27, 2025

Choose a reason for hiding this comment

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

Yes I would have thought so as well. This would make sense, but grid.draw.ggplot() calls print.ggplot(), which has the following clause:

if (newpage) grid.newpage()

which gets activated by default. For this reason, grid.newpage() gets invoked every loop for a list of ggplots.

Copy link
Member

Choose a reason for hiding this comment

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

oh, yeah... forgot it was a ggplot object 🙈

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