diff --git a/R/save.R b/R/save.R index 6db3e9fe00..41df886653 100644 --- a/R/save.R +++ b/R/save.R @@ -26,13 +26,16 @@ #' @param plot Plot to save, defaults to last plot displayed. #' @param device Device to use. Can either be a device function #' (e.g. [png]), or one of "eps", "ps", "tex" (pictex), -#' "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). +#' "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). If +#' `NULL` (default), the device is guessed based on the `filename` extension. #' @param path Path of the directory to save plot to: `path` and `filename` #' are combined to create the fully qualified file name. Defaults to the #' working directory. #' @param scale Multiplicative scaling factor. -#' @param width,height,units Plot size in `units` ("in", "cm", "mm", or "px"). -#' If not supplied, uses the size of current graphics device. +#' @param width,height Plot size in units expressed by the `units` argument. +#' If not supplied, uses the size of the current graphics device. +#' @param units One of the following units in which the `width` and `height` +#' arguments are expressed: `"in"`, `"cm"`, `"mm"` or `"px"`. #' @param dpi Plot resolution. Also accepts a string input: "retina" (320), #' "print" (300), or "screen" (72). Applies only to raster output types. #' @param limitsize When `TRUE` (the default), `ggsave()` will not @@ -48,12 +51,17 @@ #' ggplot(mtcars, aes(mpg, wt)) + #' geom_point() #' +#' # here, the device is inferred from the filename extension #' ggsave("mtcars.pdf") #' ggsave("mtcars.png") #' +#' # setting dimensions of the plot #' ggsave("mtcars.pdf", width = 4, height = 4) #' ggsave("mtcars.pdf", width = 20, height = 20, units = "cm") #' +#' # passing device-specific arguments to '...' +#' ggsave("mtcars.pdf", colormodel = "cmyk") +#' #' # delete files with base::unlink() #' unlink("mtcars.pdf") #' unlink("mtcars.png") @@ -138,7 +146,6 @@ parse_dpi <- function(dpi, call = caller_env()) { plot_dim <- function(dim = c(NA, NA), scale = 1, units = "in", limitsize = TRUE, dpi = 300, call = caller_env()) { - units <- arg_match0(units, c("in", "cm", "mm", "px")) to_inches <- function(x) x / c(`in` = 1, cm = 2.54, mm = 2.54 * 10, px = dpi)[units] from_inches <- function(x) x * c(`in` = 1, cm = 2.54, mm = 2.54 * 10, px = dpi)[units] diff --git a/man/ggsave.Rd b/man/ggsave.Rd index 4787c774d1..cfa68b688b 100644 --- a/man/ggsave.Rd +++ b/man/ggsave.Rd @@ -26,7 +26,8 @@ ggsave( \item{device}{Device to use. Can either be a device function (e.g. \link{png}), or one of "eps", "ps", "tex" (pictex), -"pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only).} +"pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). If +\code{NULL} (default), the device is guessed based on the \code{filename} extension.} \item{path}{Path of the directory to save plot to: \code{path} and \code{filename} are combined to create the fully qualified file name. Defaults to the @@ -34,8 +35,11 @@ working directory.} \item{scale}{Multiplicative scaling factor.} -\item{width, height, units}{Plot size in \code{units} ("in", "cm", "mm", or "px"). -If not supplied, uses the size of current graphics device.} +\item{width, height}{Plot size in units expressed by the \code{units} argument. +If not supplied, uses the size of the current graphics device.} + +\item{units}{One of the following units in which the \code{width} and \code{height} +arguments are expressed: \code{"in"}, \code{"cm"}, \code{"mm"} or \code{"px"}.} \item{dpi}{Plot resolution. Also accepts a string input: "retina" (320), "print" (300), or "screen" (72). Applies only to raster output types.} @@ -81,12 +85,17 @@ examples section. ggplot(mtcars, aes(mpg, wt)) + geom_point() +# here, the device is inferred from the filename extension ggsave("mtcars.pdf") ggsave("mtcars.png") +# setting dimensions of the plot ggsave("mtcars.pdf", width = 4, height = 4) ggsave("mtcars.pdf", width = 20, height = 20, units = "cm") +# passing device-specific arguments to '...' +ggsave("mtcars.pdf", colormodel = "cmyk") + # delete files with base::unlink() unlink("mtcars.pdf") unlink("mtcars.png") diff --git a/tests/testthat/helper-density.R b/tests/testthat/helper-density.R new file mode 100644 index 0000000000..1896437b8f --- /dev/null +++ b/tests/testthat/helper-density.R @@ -0,0 +1,21 @@ + +# In R devel from 4.3.0 onwards, the density calculation has slightly changed, +# which affects visual snapshots that use a density calculation, like +# `geom_violin()` and `geom_density()`. +# See https://developer.r-project.org/blosxom.cgi/R-devel/NEWS/2023/05/03#n2023-05-03 +# +# It has a backwards compatibility argument called 'old.coords' that can be used +# to perform the classic density calculation, which means we can stably use +# visual tests in R devel. +# +# Since that argument is not available in older versions, we have to use the +# following workaround. Here, we conditionally override the default +# density method to use `old.coords = TRUE`. +if ("old.coords" %in% names(formals(stats::density.default))) { + registerS3method( + "density", "default", + function(..., old.coords = TRUE) { + stats::density.default(..., old.coords = old.coords) + } + ) +}