Skip to content

Commit

Permalink
Fix saving plots with no background (#4244)
Browse files Browse the repository at this point in the history
* If plot theme background is null, set to transparent when saving

* Add test for svg with no background

* Make checking for NULL background more concise with %||%
  • Loading branch information
karawoo committed Oct 28, 2020
1 parent 7e51849 commit a132727
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/save.r
Expand Up @@ -87,7 +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
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, ...)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-ggsave.R
Expand Up @@ -44,6 +44,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", {
Expand Down

0 comments on commit a132727

Please sign in to comment.