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

plot as first argument of ggsave() #5874

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `plot` is now the first argument of `ggsave()`. `plot` and `filename` are
swapped when `plot` is a string and `filename` is a plot, to preserve some
backward compatibility (#3523).
* (Internal) Applying defaults in `geom_sf()` has moved from the internal
`sf_grob()` to `GeomSf$use_defaults()` (@teunbrand).
* `facet_wrap()` has new options for the `dir` argument to more precisely
Expand Down
20 changes: 13 additions & 7 deletions R/save.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@
#' geom_point()
#'
#' # here, the device is inferred from the filename extension
#' ggsave("mtcars.pdf")
#' ggsave("mtcars.png")
#' ggsave(filename = "mtcars.pdf")
#' ggsave(filename = "mtcars.png")
#'
#' # setting dimensions of the plot
#' ggsave("mtcars.pdf", width = 4, height = 4)
#' ggsave("mtcars.pdf", width = 20, height = 20, units = "cm")
#' ggsave(filename = "mtcars.pdf", width = 4, height = 4)
#' ggsave(filename = "mtcars.pdf", width = 20, height = 20, units = "cm")
#'
#' # passing device-specific arguments to '...'
#' ggsave("mtcars.pdf", colormodel = "cmyk")
#' ggsave(filename = "mtcars.pdf", colormodel = "cmyk")
#'
#' # delete files with base::unlink()
#' unlink("mtcars.pdf")
Expand All @@ -77,7 +77,7 @@
#' # specify device when saving to a file with unknown extension
#' # (for example a server supplied temporary file)
#' file <- tempfile()
#' ggsave(file, device = "pdf")
#' ggsave(filename = file, device = "pdf")
#' unlink(file)
#'
#' # save plot to file without using ggsave
Expand All @@ -89,12 +89,18 @@
#' dev.off()
#'
#' }
ggsave <- function(filename, plot = last_plot(),
ggsave <- function(plot = last_plot(), filename,
device = NULL, path = NULL, scale = 1,
width = NA, height = NA, units = c("in", "cm", "mm", "px"),
dpi = 300, limitsize = TRUE, bg = NULL,
create.dir = FALSE,
...) {
if (is.character(plot) && is.ggplot(filename)) {
tmp <- filename
filename <- plot
plot <- tmp
}

filename <- check_path(path, filename, create.dir)

dpi <- parse_dpi(dpi)
Expand Down
18 changes: 9 additions & 9 deletions man/ggsave.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.