Skip to content

Commit 813d0bd

Browse files
authored
Set file background from plot theme (#4164)
* Add bg argument that defaults to plot.background fill colour * Add NEWS bullet * Add test for background color of saved file
1 parent 3be0acc commit 813d0bd

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ Suggests:
6363
sf (>= 0.7-3),
6464
svglite (>= 1.2.0.9001),
6565
testthat (>= 2.1.0),
66-
vdiffr (>= 0.3.0)
66+
vdiffr (>= 0.3.0),
67+
xml2
6768
Enhances: sp
6869
License: GPL-2 | file LICENSE
6970
URL: http://ggplot2.tidyverse.org, https://github.com/tidyverse/ggplot2

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* `ggsave()` now sets the default background to match the fill value of the
4+
`plot.background` theme element (@karawoo, #4057)
5+
36
* Extended `stat_ecdf()` to calculate the cdf from either x or y instead from y only (@jgjl, #4005).
47

58
* Fixed a bug in `labeller()` so that `.default` is passed to `as_labeller()`

R/save.r

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#' @param limitsize When `TRUE` (the default), `ggsave()` will not
3939
#' save images larger than 50x50 inches, to prevent the common error of
4040
#' specifying dimensions in pixels.
41+
#' @param bg Background colour. If `NULL`, uses the `plot.background` fill value
42+
#' from the plot theme.
4143
#' @param ... Other arguments passed on to the graphics device function,
4244
#' as specified by `device`.
4345
#' @export
@@ -74,7 +76,7 @@
7476
ggsave <- function(filename, plot = last_plot(),
7577
device = NULL, path = NULL, scale = 1,
7678
width = NA, height = NA, units = c("in", "cm", "mm"),
77-
dpi = 300, limitsize = TRUE, ...) {
79+
dpi = 300, limitsize = TRUE, bg = NULL, ...) {
7880

7981
dpi <- parse_dpi(dpi)
8082
dev <- plot_dev(device, filename, dpi = dpi)
@@ -84,8 +86,11 @@ ggsave <- function(filename, plot = last_plot(),
8486
if (!is.null(path)) {
8587
filename <- file.path(path, filename)
8688
}
89+
if (is_null(bg)) {
90+
bg <- calc_element("plot.background", plot_theme(plot))$fill
91+
}
8792
old_dev <- grDevices::dev.cur()
88-
dev(filename = filename, width = dim[1], height = dim[2], ...)
93+
dev(filename = filename, width = dim[1], height = dim[2], bg = bg, ...)
8994
on.exit(utils::capture.output({
9095
grDevices::dev.off()
9196
if (old_dev > 1) grDevices::dev.set(old_dev) # restore old device unless null device

man/ggsave.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-ggsave.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ test_that("ggsave restores previous graphics device", {
2828
expect_identical(old_dev, dev.cur())
2929
})
3030

31+
test_that("ggsave uses theme background as image background", {
32+
path <- tempfile()
33+
on.exit(unlink(path))
34+
p <- ggplot(mtcars, aes(disp, mpg)) +
35+
geom_point() +
36+
coord_fixed() +
37+
theme(plot.background = element_rect(fill = "#00CCCC"))
38+
ggsave(path, p, device = "svg", width = 5, height = 5)
39+
img <- xml2::read_xml(path)
40+
# Find background rect in svg
41+
bg <- as.character(xml2::xml_find_first(img, xpath = "d1:rect/@style"))
42+
expect_true(grepl("fill: #00CCCC", bg))
43+
})
44+
3145
# plot_dim ---------------------------------------------------------------
3246

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

0 commit comments

Comments
 (0)