Skip to content

Set file background from plot theme #4164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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