## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = FALSE, comment = "",
                      fig.width = 7, fig.height = 4.5, dpi = 96,
                      dev.args = list(bg = "transparent"))
# Console colour carries no meaning on a rendered page. pkgdown turns it on for
# its own build, and the escape sequences then reach the reader as literal text,
# so colour is switched off here for a plain vignette render and a site build
# alike. The fixed width keeps printed output inside the documentation column.
options(cli.num_colors = 1, cli.hyperlink = FALSE, crayon.enabled = FALSE,
        width = 80)

# Figures on the package website sit on a warm off-white page in light mode and
# are inverted by pkgdown in dark mode, so an opaque background would read as a
# pale slab one way and a black plate the other. Two layers paint that
# background, and both have to go. The device canvas is made transparent by
# `dev.args` above, and theme_depictr() then inherits theme_minimal()'s white
# plot.background, which is drawn over that canvas, so it is cleared as each
# figure is printed. This is deliberately a vignette-level choice: theme_depictr()
# keeps its opaque background, which is what a figure saved for a paper wants.
transparent_bg <- ggplot2::theme(
  plot.background  = ggplot2::element_rect(fill = NA, colour = NA),
  panel.background = ggplot2::element_rect(fill = NA, colour = NA)
)
knit_print.ggplot <- function(x, ...) knitr::normal_print(x + transparent_bg)
knit_print.patchwork <- function(x, ...) knitr::normal_print(x & transparent_bg)


## -----------------------------------------------------------------------------
library(depictr)

## -----------------------------------------------------------------------------
explore_bivariate(crop_yield, fertiliser, yield)

## -----------------------------------------------------------------------------
fit <- lm(yield ~ rainfall + fertiliser + soil_ph + treatment,
          data = crop_yield)
coefficient_plot(fit, order = "descending", title = "Drivers of crop yield")

## -----------------------------------------------------------------------------
effects_plot(fit, "fertiliser")

## ----fig.height = 6-----------------------------------------------------------
residual_diagnostics_plot(fit)

## -----------------------------------------------------------------------------
draws <- readRDS(system.file("extdata", "lexdec_draws.rds",
                             package = "depictr"))
posterior_plot(draws[c("conditionunrelated", "modalityauditory",
                       "word_frequency")],
               labels = c(conditionunrelated = "condition",
                          modalityauditory = "modality",
                          word_frequency = "word frequency"),
               title = "Lexical-decision fixed effects (ms)")

## -----------------------------------------------------------------------------
tidy_estimates(fit)

## -----------------------------------------------------------------------------
library(ggplot2)
ggplot(crop_yield, aes(fertiliser, yield, colour = treatment)) +
  geom_point(alpha = 0.7) +
  scale_colour_depictr() +
  theme_depictr()

## -----------------------------------------------------------------------------
depictr_palette(4)

## ----fig.height = 5-----------------------------------------------------------
palette_preview(type = "all")

## ----fig.height = 2.6---------------------------------------------------------
palette_preview(cvd = "deutan")

## -----------------------------------------------------------------------------
palette_safety()

## -----------------------------------------------------------------------------
grouped <- ggplot(crop_yield, aes(fertiliser, yield, colour = treatment)) +
  geom_point(alpha = 0.7) +
  scale_colour_depictr() +
  theme_depictr()

check_figure(grouped)

## -----------------------------------------------------------------------------
mended <- ggplot(crop_yield, aes(fertiliser, yield, colour = treatment,
                                 shape = treatment)) +
  geom_point(alpha = 0.7) +
  scale_colour_manual(values = c("#005b96", "#d55e00")) +
  theme_depictr()

check_figure(mended)[, c("check", "measured", "threshold", "verdict")]

## -----------------------------------------------------------------------------
subset(check_figure(grouped, width_cm = 8.9), check == "text_size")

## -----------------------------------------------------------------------------
eight <- data.frame(g = factor(letters[1:8]), x = 1:8, y = 1:8)
p8 <- ggplot(eight, aes(x, y, colour = g)) +
  geom_point() +
  scale_colour_depictr() +
  theme_depictr()

subset(check_figure(p8), check == "greyscale_separability")

## -----------------------------------------------------------------------------
depictr_options()

## ----fig.height = 4-----------------------------------------------------------
old <- depictr_options(base_size = 13, accent = "#b3589a")
coefficient_plot(fit, title = "Set once, applied everywhere")
do.call(depictr_options, old)   # restore the previous settings

