31
31
# ' are combined to create the fully qualified file name. Defaults to the
32
32
# ' working directory.
33
33
# ' @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 ").
35
35
# ' If not supplied, uses the size of current graphics device.
36
36
# ' @param dpi Plot resolution. Also accepts a string input: "retina" (320),
37
37
# ' "print" (300), or "screen" (72). Applies only to raster output types.
75
75
# ' }
76
76
ggsave <- function (filename , plot = last_plot(),
77
77
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 " ),
79
79
dpi = 300 , limitsize = TRUE , bg = NULL , ... ) {
80
80
81
81
dpi <- parse_dpi(dpi )
82
82
dev <- plot_dev(device , filename , dpi = dpi )
83
83
dim <- plot_dim(c(width , height ), scale = scale , units = units ,
84
- limitsize = limitsize )
84
+ limitsize = limitsize , dpi = dpi )
85
85
86
86
if (! is.null(path )) {
87
87
filename <- file.path(path , filename )
@@ -122,12 +122,12 @@ parse_dpi <- function(dpi) {
122
122
}
123
123
}
124
124
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 ) {
127
127
128
128
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 ]
131
131
132
132
dim <- to_inches(dim ) * scale
133
133
@@ -158,18 +158,34 @@ plot_dev <- function(device, filename = NULL, dpi = 300) {
158
158
force(dpi )
159
159
160
160
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'
166
171
}
172
+ dev <- function (... ) do.call(device , modify_list(list (... ), call_args ))
173
+ return (dev )
167
174
}
168
175
169
176
eps <- function (filename , ... ) {
170
177
grDevices :: postscript(file = filename , ... , onefile = FALSE , horizontal = FALSE ,
171
178
paper = " special" )
172
179
}
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
+ }
173
189
devices <- list (
174
190
eps = eps ,
175
191
ps = eps ,
@@ -178,11 +194,11 @@ plot_dev <- function(device, filename = NULL, dpi = 300) {
178
194
svg = function (filename , ... ) svglite :: svglite(file = filename , ... ),
179
195
emf = function (... ) grDevices :: win.metafile(... ),
180
196
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" ),
184
200
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" )
186
202
)
187
203
188
204
if (is.null(device )) {
0 commit comments