|
26 | 26 | #' @param plot Plot to save, defaults to last plot displayed.
|
27 | 27 | #' @param device Device to use. Can either be a device function
|
28 | 28 | #' (e.g. [png]), or one of "eps", "ps", "tex" (pictex),
|
29 |
| -#' "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). |
| 29 | +#' "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only). If |
| 30 | +#' `NULL` (default), the device is guessed based on the `filename` extension. |
30 | 31 | #' @param path Path of the directory to save plot to: `path` and `filename`
|
31 | 32 | #' are combined to create the fully qualified file name. Defaults to the
|
32 | 33 | #' working directory.
|
33 | 34 | #' @param scale Multiplicative scaling factor.
|
34 |
| -#' @param width,height,units Plot size in `units` ("in", "cm", "mm", or "px"). |
35 |
| -#' If not supplied, uses the size of current graphics device. |
| 35 | +#' @param width,height Plot size in units expressed by the `units` argument. |
| 36 | +#' If not supplied, uses the size of the current graphics device. |
| 37 | +#' @param units One of the following units in which the `width` and `height` |
| 38 | +#' arguments are expressed: `"in"`, `"cm"`, `"mm"` or `"px"`. |
36 | 39 | #' @param dpi Plot resolution. Also accepts a string input: "retina" (320),
|
37 | 40 | #' "print" (300), or "screen" (72). Applies only to raster output types.
|
38 | 41 | #' @param limitsize When `TRUE` (the default), `ggsave()` will not
|
|
48 | 51 | #' ggplot(mtcars, aes(mpg, wt)) +
|
49 | 52 | #' geom_point()
|
50 | 53 | #'
|
| 54 | +#' # here, the device is inferred from the filename extension |
51 | 55 | #' ggsave("mtcars.pdf")
|
52 | 56 | #' ggsave("mtcars.png")
|
53 | 57 | #'
|
| 58 | +#' # setting dimensions of the plot |
54 | 59 | #' ggsave("mtcars.pdf", width = 4, height = 4)
|
55 | 60 | #' ggsave("mtcars.pdf", width = 20, height = 20, units = "cm")
|
56 | 61 | #'
|
| 62 | +#' # passing device-specific arguments to '...' |
| 63 | +#' ggsave("mtcars.pdf", colormodel = "cmyk") |
| 64 | +#' |
57 | 65 | #' # delete files with base::unlink()
|
58 | 66 | #' unlink("mtcars.pdf")
|
59 | 67 | #' unlink("mtcars.png")
|
@@ -138,7 +146,6 @@ parse_dpi <- function(dpi, call = caller_env()) {
|
138 | 146 |
|
139 | 147 | plot_dim <- function(dim = c(NA, NA), scale = 1, units = "in",
|
140 | 148 | limitsize = TRUE, dpi = 300, call = caller_env()) {
|
141 |
| - |
142 | 149 | units <- arg_match0(units, c("in", "cm", "mm", "px"))
|
143 | 150 | to_inches <- function(x) x / c(`in` = 1, cm = 2.54, mm = 2.54 * 10, px = dpi)[units]
|
144 | 151 | from_inches <- function(x) x * c(`in` = 1, cm = 2.54, mm = 2.54 * 10, px = dpi)[units]
|
|
0 commit comments