From a1718132971b73d8a86d33c3867daecc0f0584bf Mon Sep 17 00:00:00 2001 From: Kara Woo Date: Tue, 6 Oct 2020 17:27:37 -0700 Subject: [PATCH 1/3] If plot theme background is null, set to transparent when saving --- R/save.r | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/save.r b/R/save.r index 1bdd11381c..a97b35ebbb 100644 --- a/R/save.r +++ b/R/save.r @@ -88,6 +88,9 @@ ggsave <- function(filename, plot = last_plot(), } if (is_null(bg)) { bg <- calc_element("plot.background", plot_theme(plot))$fill + if (is_null(bg)) { + bg <- "transparent" + } } old_dev <- grDevices::dev.cur() dev(filename = filename, width = dim[1], height = dim[2], bg = bg, ...) From dbcf3f14ea33a3ac93076018ce77832a7b4cb03c Mon Sep 17 00:00:00 2001 From: Kara Woo Date: Mon, 19 Oct 2020 17:53:29 -0700 Subject: [PATCH 2/3] Add test for svg with no background --- tests/testthat/test-ggsave.R | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testthat/test-ggsave.R b/tests/testthat/test-ggsave.R index 6d60439686..135a112da3 100644 --- a/tests/testthat/test-ggsave.R +++ b/tests/testthat/test-ggsave.R @@ -46,6 +46,21 @@ test_that("ggsave uses theme background as image background", { expect_true(grepl("fill: #00CCCC", bg)) }) +test_that("ggsave can handle blank background", { + skip_if_not_installed("xml2") + + path <- tempfile() + on.exit(unlink(path)) + p <- ggplot(mtcars, aes(disp, mpg)) + + geom_point() + + theme(plot.background = element_blank()) + ggsave(path, p, device = "svg", width = 5, height = 5) + img <- xml2::read_xml(path) + bg <- as.character(xml2::xml_find_first(img, xpath = "d1:rect/@style")) + expect_true(grepl("fill: none", bg)) +}) + + # plot_dim --------------------------------------------------------------- test_that("guesses and informs if dim not specified", { From 0f3cd8a13fb0aa76c5e790ed8dcc773517d3f95c Mon Sep 17 00:00:00 2001 From: Kara Woo Date: Wed, 21 Oct 2020 16:58:46 -0700 Subject: [PATCH 3/3] Make checking for NULL background more concise with %||% --- R/save.r | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/R/save.r b/R/save.r index a97b35ebbb..98bd10e99a 100644 --- a/R/save.r +++ b/R/save.r @@ -87,10 +87,7 @@ ggsave <- function(filename, plot = last_plot(), filename <- file.path(path, filename) } if (is_null(bg)) { - bg <- calc_element("plot.background", plot_theme(plot))$fill - if (is_null(bg)) { - bg <- "transparent" - } + bg <- calc_element("plot.background", plot_theme(plot))$fill %||% "transparent" } old_dev <- grDevices::dev.cur() dev(filename = filename, width = dim[1], height = dim[2], bg = bg, ...)