Skip to content

Commit a9b3b17

Browse files
authored
Add full support for ragg (#4388)
1 parent 0990114 commit a9b3b17

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Imports:
4242
withr (>= 2.0.0)
4343
Suggests:
4444
covr,
45+
ragg,
4546
dplyr,
4647
ggplot2movies,
4748
hexbin,

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* `ggsave()` now uses ragg to render raster output if ragg is available
4+
(@thomasp85, #4388)
5+
36
* `coord_sf()` now has an argument `default_crs` that specifies the coordinate
47
reference system (CRS) for non-sf layers and scale/coord limits. This argument
58
defaults to the World Geodetic System 1984 (WGS84), which means x and y positions

R/save.r

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' are combined to create the fully qualified file name. Defaults to the
3232
#' working directory.
3333
#' @param scale Multiplicative scaling factor.
34-
#' @param width,height,units Plot size in `units` ("in", "cm", or "mm").
34+
#' @param width,height,units Plot size in `units` ("in", "cm", "mm", or "px").
3535
#' If not supplied, uses the size of current graphics device.
3636
#' @param dpi Plot resolution. Also accepts a string input: "retina" (320),
3737
#' "print" (300), or "screen" (72). Applies only to raster output types.
@@ -75,13 +75,13 @@
7575
#' }
7676
ggsave <- function(filename, plot = last_plot(),
7777
device = NULL, path = NULL, scale = 1,
78-
width = NA, height = NA, units = c("in", "cm", "mm"),
78+
width = NA, height = NA, units = c("in", "cm", "mm", "px"),
7979
dpi = 300, limitsize = TRUE, bg = NULL, ...) {
8080

8181
dpi <- parse_dpi(dpi)
8282
dev <- plot_dev(device, filename, dpi = dpi)
8383
dim <- plot_dim(c(width, height), scale = scale, units = units,
84-
limitsize = limitsize)
84+
limitsize = limitsize, dpi = dpi)
8585

8686
if (!is.null(path)) {
8787
filename <- file.path(path, filename)
@@ -122,12 +122,12 @@ parse_dpi <- function(dpi) {
122122
}
123123
}
124124

125-
plot_dim <- function(dim = c(NA, NA), scale = 1, units = c("in", "cm", "mm"),
126-
limitsize = TRUE) {
125+
plot_dim <- function(dim = c(NA, NA), scale = 1, units = c("in", "cm", "mm", "px"),
126+
limitsize = TRUE, dpi = 300) {
127127

128128
units <- match.arg(units)
129-
to_inches <- function(x) x / c(`in` = 1, cm = 2.54, mm = 2.54 * 10)[units]
130-
from_inches <- function(x) x * c(`in` = 1, cm = 2.54, mm = 2.54 * 10)[units]
129+
to_inches <- function(x) x / c(`in` = 1, cm = 2.54, mm = 2.54 * 10, px = dpi)[units]
130+
from_inches <- function(x) x * c(`in` = 1, cm = 2.54, mm = 2.54 * 10, px = dpi)[units]
131131

132132
dim <- to_inches(dim) * scale
133133

@@ -158,18 +158,34 @@ plot_dev <- function(device, filename = NULL, dpi = 300) {
158158
force(dpi)
159159

160160
if (is.function(device)) {
161-
if ("file" %in% names(formals(device))) {
162-
dev <- function(filename, ...) device(file = filename, ...)
163-
return(dev)
164-
} else {
165-
return(device)
161+
args <- formals(device)
162+
call_args <- list()
163+
if ("file" %in% names(args)) {
164+
call_args$file <- filename
165+
}
166+
if ("res" %in% names(args)) {
167+
call_args$res <- dpi
168+
}
169+
if ("units" %in% names(args)) {
170+
call_args$units <- 'in'
166171
}
172+
dev <- function(...) do.call(device, modify_list(list(...), call_args))
173+
return(dev)
167174
}
168175

169176
eps <- function(filename, ...) {
170177
grDevices::postscript(file = filename, ..., onefile = FALSE, horizontal = FALSE,
171178
paper = "special")
172179
}
180+
if (requireNamespace('ragg', quietly = TRUE)) {
181+
png_dev <- ragg::agg_png
182+
jpeg_dev <- ragg::agg_jpeg
183+
tiff_dev <- ragg::agg_tiff
184+
} else {
185+
png_dev <- grDevices::png
186+
jpeg_dev <- grDevices::jpeg
187+
tiff_dev <- grDevices::tiff
188+
}
173189
devices <- list(
174190
eps = eps,
175191
ps = eps,
@@ -178,11 +194,11 @@ plot_dev <- function(device, filename = NULL, dpi = 300) {
178194
svg = function(filename, ...) svglite::svglite(file = filename, ...),
179195
emf = function(...) grDevices::win.metafile(...),
180196
wmf = function(...) grDevices::win.metafile(...),
181-
png = function(...) grDevices::png(..., res = dpi, units = "in"),
182-
jpg = function(...) grDevices::jpeg(..., res = dpi, units = "in"),
183-
jpeg = function(...) grDevices::jpeg(..., res = dpi, units = "in"),
197+
png = function(...) png_dev(..., res = dpi, units = "in"),
198+
jpg = function(...) jpeg_dev(..., res = dpi, units = "in"),
199+
jpeg = function(...) jpeg_dev(..., res = dpi, units = "in"),
184200
bmp = function(...) grDevices::bmp(..., res = dpi, units = "in"),
185-
tiff = function(...) grDevices::tiff(..., res = dpi, units = "in")
201+
tiff = function(...) tiff_dev(..., res = dpi, units = "in")
186202
)
187203

188204
if (is.null(device)) {

man/ggsave.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ggsave.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,19 @@ test_that("scale multiplies height & width", {
8585

8686
# plot_dev ---------------------------------------------------------------------
8787

88-
test_that("function is passed back unchanged", {
89-
expect_equal(plot_dev(png), png)
90-
})
91-
9288
test_that("unknown device triggers error", {
9389
expect_error(plot_dev("xyz"), "Unknown graphics device")
9490
expect_error(plot_dev(NULL, "test.xyz"), "Unknown graphics device")
9591
})
9692

9793

9894
test_that("text converted to function", {
99-
expect_identical(body(plot_dev("png"))[[1]], quote(grDevices::png))
95+
expect_identical(body(plot_dev("png"))[[1]], quote(png_dev))
10096
expect_identical(body(plot_dev("pdf"))[[1]], quote(grDevices::pdf))
10197
})
10298

10399
test_that("if device is NULL, guess from extension", {
104-
expect_identical(body(plot_dev(NULL, "test.png"))[[1]], quote(grDevices::png))
100+
expect_identical(body(plot_dev(NULL, "test.png"))[[1]], quote(png_dev))
105101
})
106102

107103
# parse_dpi ---------------------------------------------------------------

0 commit comments

Comments
 (0)