## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse   = TRUE,
  comment    = "#>",
  fig.alt    = paste(
    "Asymmetric smoothed-association matrix produced by",
    "janusplot(); diagonal cells hold variable labels,",
    "off-diagonal cells show the fitted mgcv::gam spline",
    "with a 95% confidence envelope, raw-data scatter,",
    "and per-cell annotations for n, EDF, and smooth",
    "significance glyph."
  ),
  fig.width  = 7,
  fig.height = 7,
  dpi        = 110,
  eval       = TRUE
)
library(ggplot2)
set.seed(2026L)

## ----quickstart---------------------------------------------------------------
library(janusplot)

# Four numeric columns from mtcars (32 rows: small but illustrative)
janusplot(mtcars[, c("mpg", "hp", "wt", "qsec")])

## ----nonlinear----------------------------------------------------------------
n <- 300
x1 <- runif(n, -3, 3)
x2 <- x1^2 + rnorm(n, sd = 0.6)   # quadratic on x1
x3 <- sin(x1) + rnorm(n, sd = 0.4) # sinusoidal on x1
x4 <- rnorm(n)                     # independent
d  <- data.frame(x1 = x1, x2 = x2, x3 = x3, x4 = x4)

janusplot(d)

## ----asym---------------------------------------------------------------------
n <- 400
x <- runif(n, 0, 5)
y <- 0.5 * x + rnorm(n, sd = 0.3 + 0.4 * x)   # variance grows with x
d <- data.frame(x = x, y = y, z = rnorm(n))

janusplot(d)

## ----adjust, eval = requireNamespace("palmerpenguins", quietly = TRUE)--------
library(palmerpenguins)
pp <- na.omit(penguins)

# Without covariate
janusplot(pp[, c("bill_length_mm", "bill_depth_mm",
                 "flipper_length_mm", "body_mass_g")])

# With species as a fixed effect — resolves Simpson's-paradox geometry
janusplot(pp, vars = c("bill_length_mm", "bill_depth_mm",
                       "flipper_length_mm", "body_mass_g"),
         adjust = ~ species)

## ----palette-viridis, fig.height = 5, fig.width = 6---------------------------
d <- data.frame(
  x1 = runif(200, -3, 3),
  x2 = rnorm(200),
  x3 = rnorm(200)
)
d$x2 <- d$x1^2 + rnorm(200, sd = 0.8)  # non-linear

janusplot(d, palette = "viridis")  # default, colourblind-safe

## ----palette-brewer, fig.height = 5, fig.width = 6----------------------------
janusplot(d, palette = "RdYlBu")   # diverging, colourblind-safe

## ----palette-turbo, fig.height = 5, fig.width = 6-----------------------------
janusplot(d, palette = "turbo")    # high-contrast, NOT colourblind-safe

## ----missing------------------------------------------------------------------
# airquality has genuine NAs in Ozone and Solar.R
janusplot(airquality[, c("Ozone", "Solar.R", "Wind", "Temp")],
          na_action = "pairwise")

## ----hclust, eval = requireNamespace("MASS", quietly = TRUE)------------------
data(Boston, package = "MASS")
janusplot(Boston[, c("medv", "lstat", "rm", "age",
                     "indus", "nox", "dis")],
          order = "hclust")

## ----data---------------------------------------------------------------------
# Re-create the heteroscedastic example
n <- 400
het <- data.frame(
  x = runif(n, 0, 5),
  y = NA_real_
)
het$y <- 0.5 * het$x + rnorm(n, sd = 0.3 + 0.4 * het$x)

out <- janusplot_data(het, vars = c("x", "y"))
out$pairs[[1L]]$edf_yx
out$pairs[[1L]]$edf_xy
out$pairs[[1L]]$asymmetry_index

## ----derivs-fit, fig.height = 4.5---------------------------------------------
set.seed(2026L)
n  <- 300L
xs <- runif(n, -pi, pi)
df <- data.frame(
  x  = xs,
  y1 = xs + sin(3 * xs) + rnorm(n, sd = 0.15),
  y2 = 0.5 * xs^2       + rnorm(n, sd = 0.8)
)
janusplot(df, display = "fit", show_shape_legend = FALSE)

## ----derivs-d1, fig.height = 4.5----------------------------------------------
janusplot(df, display = "d1", show_shape_legend = FALSE)

## ----derivs-d2, fig.height = 4.5----------------------------------------------
janusplot(df, display = "d2", show_shape_legend = FALSE)

## ----derivs-sim, fig.height = 4.5---------------------------------------------
janusplot(df, display = "d1",
          derivative_ci = "simultaneous",
          derivative_ci_nsim = 2000L,
          show_shape_legend = FALSE)

## ----cite---------------------------------------------------------------------
citation("janusplot")

