You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ggsave should use plot theme for default background #4057
@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 ggplot_build() and ggplot_gtable() to be able to extract the theme without generating any grobs before the graphics device is set up.
@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:
The other issue is that theme elements really should be calculated with calc_element() is we want to keep the code maintainable and predictable going forward.
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
The other issue is that theme elements really should be calculated with calc_element() is we want to keep the code maintainable and predictable going forward.
At least, now I am aware of those internals (thanks!).
I wrote the tweak/patch with v2.2.0 at that time and did not update that part at all since then.
I wonder, if there is a computational "burden" in ggsave because of the double calculation with the proposed path using calc_element().
Is it worst or the same within the print/plot method, if we want the "interactive" device (x11/cairo/...) to get the background information as well?
Activity
thomasp85 commentedon Jun 10, 2020
hadley commentedon Jun 10, 2020
mcanouil commentedon Jun 22, 2020
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).
Here it is (initially from https://github.com/mcanouil/mctemplates/blob/8a42d07300841ab2e3936ab63fe92a25a0781bff/R/theme_black.R#L277)
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.
clauswilke commentedon Jun 22, 2020
@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
ggplot_build()andggplot_gtable()to be able to extract the theme without generating any grobs before the graphics device is set up.thomasp85 commentedon Jun 22, 2020
@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
clauswilke commentedon Jun 22, 2020
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:
ggplot2/R/theme.r
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.
clauswilke commentedon Jun 22, 2020
The other issue is that theme elements really should be calculated with
calc_element()is we want to keep the code maintainable and predictable going forward.ggplot2/R/theme.r
Line 509 in 112f960
thomasp85 commentedon Jun 22, 2020
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
clauswilke commentedon Jun 22, 2020
If you don't mind that
ggsave()will duplicate the theme calculation, you could simply do:thomasp85 commentedon Jun 22, 2020
If that is all it takes then I think we should do that (if background is set to
NULL)clauswilke commentedon Jun 22, 2020
Yeah, I think this would work. Note we need
fill, notcolour.Created on 2020-06-22 by the reprex package (v0.3.0)
mcanouil commentedon Jun 23, 2020
At least, now I am aware of those internals (thanks!).
I wrote the tweak/patch with v2.2.0 at that time and did not update that part at all since then.
I wonder, if there is a computational "burden" in ggsave because of the double calculation with the proposed path using
calc_element().Is it worst or the same within the print/plot method, if we want the "interactive" device (x11/cairo/...) to get the background information as well?