Skip to content

Commit 8aadf30

Browse files
committed
helper to deal with background
1 parent 78e9cd3 commit 8aadf30

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

R/save.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ ggsave <- function(filename, plot = get_last_plot(),
101101
dev <- validate_device(device, filename, dpi = dpi)
102102
dim <- plot_dim(c(width, height), scale = scale, units = units,
103103
limitsize = limitsize, dpi = dpi)
104+
bg <- get_plot_background(plot, bg)
104105

105-
if (is_null(bg) && is.ggplot(plot)) {
106-
bg <- calc_element("plot.background", plot_theme(plot))$fill %||% "transparent"
107-
}
108106
old_dev <- grDevices::dev.cur()
109107
dev(filename = filename, width = dim[1], height = dim[2], bg = bg, ...)
110108
on.exit(utils::capture.output({
@@ -238,6 +236,17 @@ plot_dim <- function(dim = c(NA, NA), scale = 1, units = "in",
238236
dim
239237
}
240238

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+
241250
validate_device <- function(device, filename = NULL, dpi = 300, call = caller_env()) {
242251
force(filename)
243252
force(dpi)

0 commit comments

Comments
 (0)