Skip to content

Commit

Permalink
Set file background from plot theme (#4164)
Browse files Browse the repository at this point in the history
* Add bg argument that defaults to plot.background fill colour

* Add NEWS bullet

* Add test for background color of saved file
  • Loading branch information
karawoo committed Aug 20, 2020
1 parent 3be0acc commit 813d0bd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Suggests:
sf (>= 0.7-3),
svglite (>= 1.2.0.9001),
testthat (>= 2.1.0),
vdiffr (>= 0.3.0)
vdiffr (>= 0.3.0),
xml2
Enhances: sp
License: GPL-2 | file LICENSE
URL: http://ggplot2.tidyverse.org, https://github.com/tidyverse/ggplot2
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `ggsave()` now sets the default background to match the fill value of the
`plot.background` theme element (@karawoo, #4057)

* Extended `stat_ecdf()` to calculate the cdf from either x or y instead from y only (@jgjl, #4005).

* Fixed a bug in `labeller()` so that `.default` is passed to `as_labeller()`
Expand Down
9 changes: 7 additions & 2 deletions R/save.r
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#' @param limitsize When `TRUE` (the default), `ggsave()` will not
#' save images larger than 50x50 inches, to prevent the common error of
#' specifying dimensions in pixels.
#' @param bg Background colour. If `NULL`, uses the `plot.background` fill value
#' from the plot theme.
#' @param ... Other arguments passed on to the graphics device function,
#' as specified by `device`.
#' @export
Expand Down Expand Up @@ -74,7 +76,7 @@
ggsave <- function(filename, plot = last_plot(),
device = NULL, path = NULL, scale = 1,
width = NA, height = NA, units = c("in", "cm", "mm"),
dpi = 300, limitsize = TRUE, ...) {
dpi = 300, limitsize = TRUE, bg = NULL, ...) {

dpi <- parse_dpi(dpi)
dev <- plot_dev(device, filename, dpi = dpi)
Expand All @@ -84,8 +86,11 @@ ggsave <- function(filename, plot = last_plot(),
if (!is.null(path)) {
filename <- file.path(path, filename)
}
if (is_null(bg)) {
bg <- calc_element("plot.background", plot_theme(plot))$fill
}
old_dev <- grDevices::dev.cur()
dev(filename = filename, width = dim[1], height = dim[2], ...)
dev(filename = filename, width = dim[1], height = dim[2], bg = bg, ...)
on.exit(utils::capture.output({
grDevices::dev.off()
if (old_dev > 1) grDevices::dev.set(old_dev) # restore old device unless null device
Expand Down
4 changes: 4 additions & 0 deletions man/ggsave.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-ggsave.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ test_that("ggsave restores previous graphics device", {
expect_identical(old_dev, dev.cur())
})

test_that("ggsave uses theme background as image background", {
path <- tempfile()
on.exit(unlink(path))
p <- ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
coord_fixed() +
theme(plot.background = element_rect(fill = "#00CCCC"))
ggsave(path, p, device = "svg", width = 5, height = 5)
img <- xml2::read_xml(path)
# Find background rect in svg
bg <- as.character(xml2::xml_find_first(img, xpath = "d1:rect/@style"))
expect_true(grepl("fill: #00CCCC", bg))
})

# plot_dim ---------------------------------------------------------------

test_that("guesses and informs if dim not specified", {
Expand Down

0 comments on commit 813d0bd

Please sign in to comment.