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
ggsave should use plot theme for default background #4057
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Hi, i have been using a customised ggsave for that purpose, although it might not be the best approach to handle that (edit: not exactly for NA though, but for consistency with the plot background at least which is related). ggsave <- function(
filename,
plot = ggplot2::last_plot(),
device = NULL,
path = NULL,
scale = 1,
width = NA,
height = NA,
units = c("in", "cm", "mm"),
dpi = 300,
limitsize = TRUE,
...
) {
if (is.null(plot$theme$plot.background$colour)) {
bc <- ggplot2::theme_get()$plot.background$colour
} else {
bc <- plot$theme$plot.background$colour
}
ggplot2::ggsave(
filename = filename,
plot = plot,
device = device,
path = path,
scale = scale,
width = width,
height = height,
units = units,
dpi = dpi,
limitsize = limitsize,
bg = bc,
...
)
} I did the same tweak for the print method https://github.com/mcanouil/mctemplates/blob/8a42d07300841ab2e3936ab63fe92a25a0781bff/R/theme_black.R#L231 I'll probably make my first PR to ggplot2 in the next few days. |
@mcanouil I'm not sure this is the right approach, as it short-circuits the regular ggplot theme calculations. In general, this is a tricky problem that I expect will require us to separately call |
@clauswilke which other places could the plot background be calculated? I think it is fair to expect it to either be set on the plot theme or get it from the default theme in lieu of having to implement yet another rendering routine |
Most importantly, I think it's a matter of keeping the code clean. We just spent a lot of effort removing one-off interactions with the theme system and trying to boil all interactions down to a small set of shared functions that are reliably called. This proposed patch would go in the opposite direction. To your specific question, this is the code the calculates the final theme: Lines 420 to 447 in 112f960
One issue that the current proposed patch doesn't handle is if the global theme does not have a properly defined background element. |
The other issue is that theme elements really should be calculated with Line 509 in 112f960
|
Those are fair points. I think the path forward should be to provide a utility function that can properly calculate a given theme element and then use that. Splitting up the build process for this seems like the wrong direction to take as well |
If you don't mind that bg <- calc_element("plot.background", plot_theme(plot))
bg_colour <- bg$colour |
If that is all it takes then I think we should do that (if background is set to |
Yeah, I think this would work. Note we need library(ggplot2)
theme_set(theme_gray() + theme(plot.background = element_rect(fill = "cornsilk")))
p1 <- ggplot(mtcars, aes(disp, mpg)) + geom_point()
p2 <- p1 + theme(plot.background = element_rect(fill = "skyblue"))
p3 <- p1 + theme_gray()
calc_element("plot.background", ggplot2:::plot_theme(p1))$fill
#> [1] "cornsilk"
calc_element("plot.background", ggplot2:::plot_theme(p2))$fill
#> [1] "skyblue"
calc_element("plot.background", ggplot2:::plot_theme(p3))$fill
#> [1] "white" Created on 2020-06-22 by the reprex package (v0.3.0) |
At least, now I am aware of those internals (thanks!). I wonder, if there is a computational "burden" in ggsave because of the double calculation with the proposed path using |
I think @djnavarro tweeted about this recently, and I hit it too. It seems like in a situation like this:
ggsave()
could automatically figure out whatbg
should be and pass it along to the graphics device.The text was updated successfully, but these errors were encountered: