## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>"
)

## -----------------------------------------------------------------------------
library(ggspec)
library(ggplot2)

## -----------------------------------------------------------------------------
ref <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  facet_wrap(~drv) +
  labs(title = "Reference plot")

obs_correct <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  facet_wrap(~drv) +
  labs(title = "Reference plot")

obs_wrong <- ggplot(mpg, aes(displ, hwy)) +
  geom_smooth() +            # wrong geom
  facet_wrap(~cyl) +         # wrong facet variable
  labs(title = "Student plot")

## -----------------------------------------------------------------------------
# Passing case
result_ok <- equiv_plot(ref, obs_correct)
result_ok
as.logical(result_ok)

## -----------------------------------------------------------------------------
# Failing case
result_fail <- equiv_plot(ref, obs_wrong)
result_fail

## -----------------------------------------------------------------------------
equiv_layers(ref, obs_wrong)
equiv_facets(ref, obs_wrong)
equiv_labels(ref, obs_wrong, aesthetics = "title")

## -----------------------------------------------------------------------------
obs_extra <- ref + geom_smooth()  # extra layer is fine by default
equiv_layers(ref, obs_extra)

equiv_layers(ref, obs_extra, exact = TRUE)  # fails: extra layer

## ----error = TRUE-------------------------------------------------------------
# Passes silently
check_plot(obs_correct, ref, check = c("layers", "aes", "facets"))

# Fails with an informative error
check_plot(obs_wrong, ref, check = c("layers", "facets"))

## -----------------------------------------------------------------------------
result <- equiv_aes(ref, obs_wrong)
result$detail

## -----------------------------------------------------------------------------
p_ref   <- ggplot(mpg, aes(displ, hwy)) + geom_smooth(method = "lm", se = FALSE)
p_wrong <- ggplot(mpg, aes(displ, hwy)) + geom_smooth(method = "lm", se = TRUE)

equiv_params(p_ref, p_wrong, layer = 1L, params = "se")

