r-lib / vdiffr

Visual regression testing and graphical diffing with testthat
https://vdiffr.r-lib.org
Other
185 stars 31 forks source link

testing that two ggplots are the same #98

Closed jgabry closed 3 years ago

jgabry commented 3 years ago

This is a follow up to https://github.com/r-lib/waldo/issues/56, which made more sense to ask here than in the waldo repo.

Currently vdiffr::expect_doppelganger() accepts a single ggplot object as input and compares to an SVG, but is there a way (or would you consider adding a way) to use vdiffr to test that two ggplot objects correspond to the same looking plot? I guess that would require either comparing them both to the same saved SVG or comparing separate SVG files created for each plot to each other.

Here's one example of a use case: in the bayesplot package we want to test that two different plotting functions create the same ggplot in the special case where the input matrix has 1 row:

# with exactly 1 row in `yrep` matrix these should produce the same plot
plot1 <- ppc_scatter_avg(y, yrep[1,, drop=FALSE])
plot2 <- ppc_scatter(y, yrep[1,, drop = FALSE])

Is there a recommended way of using vdiffr to compare plot1 to plot2 or, if not, would you consider supporting this?

Thank you!

lionel- commented 3 years ago

I don't think this is in scope for vdiffr. The approach seems brittle because the graphics engine might take two plots that look exactly the same but draw them in different ways.

jairav commented 1 year ago

@jgabry did you come up with an alternate solution for this?

shug0131 commented 7 months ago

A possible way to achieve this is by using the same title argument to expect_doppelganger

fig1<- plot_function1()
vdiffr::expect_doppelganger("plot", fig1)
fig2<- plot_function2()
vdiffr::expect_doppelganger("plot", fig2)

This seems to have the desired behaviour when I run the test. It passes if fig1 and fig2 are the same, and fails if they are different.