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

Conversation

karawoo
Copy link
Member

@karawoo karawoo commented Aug 6, 2020

Closes #4057. @clauswilke I took your proposed approach and calculated the theme element to get the background color. I imagine people will still typically want to edit their theme to remove the plot border.

I'm opening this as a draft because I think it needs a test but I'm not sure how best to write one. Is there a way to have vdiffr compare two files rather than comparing a plot object against a file? I imagine there is and that vdiffr is doing this under the hood normally, but I tinkered with it a bit and couldn't figure it out.

library("ggplot2")

p <- ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  coord_fixed() + # using coord_fixed() just to emphasize what's the plot and what's the image background
  theme(plot.background = element_rect(fill = "forestgreen"))

ggsave("~/p_new.png", p)
#> Saving 7 x 5 in image
knitr::include_graphics("~/p_new.png")

ggsave("~/p_override.png", p, bg = "cyan")
#> Saving 7 x 5 in image
knitr::include_graphics("~/p_override.png")

@clauswilke
Copy link
Member

You could write the png to a temp file, read it back in, plot as a rasterGrob() with grid, and then vdiffr can compare that.

@karawoo
Copy link
Member Author

karawoo commented Aug 18, 2020

I couldn't get the rasterGrob() method to work, so instead of writing a visual test I'm saving to svg and finding the background rect with xpath. This involves adding xml2 to Suggests which maybe we don't want to do for just one test. If you all prefer I can write a regex instead, that may be ok for this purpose.

@karawoo karawoo marked this pull request as ready for review August 18, 2020 04:47
@karawoo karawoo requested a review from clauswilke August 18, 2020 04:47
@clauswilke
Copy link
Member

What was the problem with the rasterGrob()approach? Maybe it can be fixed? Could you post the code you tried?

Copy link
Member

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking some more about this, I think this is a reasonable approach.

@karawoo
Copy link
Member Author

karawoo commented Aug 20, 2020

Thanks @clauswilke, and you feel ok about adding xml2 to Suggests?

@clauswilke
Copy link
Member

clauswilke commented Aug 20, 2020

I realized that writing an image and reading it back in to plot would also require an additional dependency (e.g. png), so it's a wash. And in any case just writing to and reading from svg (rather than writing a png, reading it in, plotting to svg, and then comparing that svg to a reference) is much more efficient. And also, vdiffr imports xml2, so we have this dependency already, implicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ggsave should use plot theme for default background
2 participants