@@ -101,17 +101,18 @@ ggsave <- function(filename, plot = get_last_plot(),
101
101
dev <- validate_device(device , filename , dpi = dpi )
102
102
dim <- plot_dim(c(width , height ), scale = scale , units = units ,
103
103
limitsize = limitsize , dpi = dpi )
104
+ bg <- get_plot_background(plot , bg )
104
105
105
- if (is_null(bg )) {
106
- bg <- calc_element(" plot.background" , plot_theme(plot ))$ fill %|| % " transparent"
107
- }
108
106
old_dev <- grDevices :: dev.cur()
109
107
dev(filename = filename , width = dim [1 ], height = dim [2 ], bg = bg , ... )
110
108
on.exit(utils :: capture.output({
111
109
grDevices :: dev.off()
112
110
if (old_dev > 1 ) grDevices :: dev.set(old_dev ) # restore old device unless null device
113
111
}))
114
- grid.draw(plot )
112
+ if (! is_bare_list(plot )) {
113
+ plot <- list (plot )
114
+ }
115
+ lapply(plot , grid.draw )
115
116
116
117
invisible (filename )
117
118
}
@@ -235,6 +236,17 @@ plot_dim <- function(dim = c(NA, NA), scale = 1, units = "in",
235
236
dim
236
237
}
237
238
239
+ get_plot_background <- function (plot , bg = NULL , default = " transparent" ) {
240
+ if (! is.null(bg )) {
241
+ return (bg )
242
+ }
243
+ plot <- if (is_bare_list(plot )) plot [[1 ]] else plot
244
+ if (! is.ggplot(plot )) {
245
+ return (default )
246
+ }
247
+ calc_element(" plot.background" , plot_theme(plot ))$ fill %|| % default
248
+ }
249
+
238
250
validate_device <- function (device , filename = NULL , dpi = 300 , call = caller_env()) {
239
251
force(filename )
240
252
force(dpi )
0 commit comments