## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.alt = paste( "Asymmetric smoothed-association matrix drawn by janusplot:", "each off-diagonal cell shows the fitted mgcv spline of the row", "variable on the column variable, with a 95% confidence band", "over the raw points, a border coloured by non-linearity and a", "strip of annotations above the panel; the diagonal cells show", "the kernel density of each variable." ), fig.width = 7, fig.height = 7, dpi = 110, eval = TRUE ) library(ggplot2) set.seed(2026L) has_mass <- requireNamespace("MASS", quietly = TRUE) has_penguins <- requireNamespace("palmerpenguins", quietly = TRUE) has_kernr <- requireNamespace("kernR", quietly = TRUE) # One row of summary(fit): the cell `response ~ s(predictor)`. cell <- function(fit, response, predictor) { tab <- summary(fit) tab[tab$var_y == response & tab$var_x == predictor, ] } f1 <- function(v) sprintf("%.1f", v) f2 <- function(v) sprintf("%.2f", v) ## ----quickstart, fig.cap = "Asymmetric association matrix for four mtcars traits. Each cell shows the row variable as a smooth function of the column variable; the border colour is the cell's non-linearity and the background its Pearson correlation.", fig.alt = "Four-by-four asymmetric smoothed-association matrix for mpg, hp, wt and qsec, with coloured cell borders and blue to orange cell backgrounds."---- library(janusplot) fit <- janusplot_fit(mtcars[, c("mpg", "hp", "wt", "qsec")]) fit plot(fit) ## ----nonlinear, fig.height = 8.5, fig.cap = "Association matrix for a quadratic pair, a sinusoidal pair and an independent variable. The border colour picks out the non-linear cells; the strip carries the shape code, decoded by the legend below.", fig.alt = "Four-by-four smoothed-association matrix for x1, x2 (quadratic in x1), x3 (sinusoidal in x1) and x4 (independent noise), followed by a legend of shape categories."---- n <- 300 x1 <- runif(n, -3, 3) d <- data.frame( x1 = x1, x2 = x1^2 + rnorm(n, sd = 0.6), # quadratic in x1 x3 = sin(x1) + rnorm(n, sd = 0.4), # sinusoidal in x1 x4 = rnorm(n) # independent ) fit_nl <- janusplot_fit(d) plot(fit_nl, annotations = c("code", "signif")) ## ----asym, fig.cap = "Association matrix for a heteroscedastic x-y pair (noise variance grows with x) plus an independent z, coloured by the asymmetry index A of each pair.", fig.alt = "Three-by-three smoothed-association matrix for x, y (heteroscedastic in x) and z (independent noise); the two x-y cells carry visibly different fitted curves."---- n <- 400 x <- runif(n, 0, 5) het <- data.frame( x = x, y = 0.5 * x + rnorm(n, sd = 0.3 + 0.4 * x), # variance grows with x z = rnorm(n) ) fit_het <- janusplot_fit(het) plot(fit_het, colour_by = "asymmetry", annotations = c("A", "edf")) ## ----group, eval = has_penguins, fig.cap = "Palmer penguins trait matrix with the raw points coloured by species. The smooths are fitted across all three species.", fig.alt = "Four-by-four association matrix for bill length, bill depth, flipper length and body mass, with points in three colours by species."---- pen_vars <- c("bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g") fit_pen <- janusplot_fit(palmerpenguins::penguins, pen_vars) plot(fit_pen, group = "species") ## ----hclust, eval = has_mass, fig.cap = "Boston housing matrix with axes reordered by hierarchical clustering on |correlation|: medv, lstat and rm group together, and age, indus, nox and dis form a second block.", fig.alt = "Seven-by-seven Boston housing association matrix with rows and columns reordered by hierarchical clustering on absolute correlation."---- data(Boston, package = "MASS") fit_boston <- janusplot_fit(Boston[, c("medv", "lstat", "rm", "age", "indus", "nox", "dis")]) plot(fit_boston, order = "hclust") ## ----style, fig.cap = "The mtcars fit redrawn with the asymmetry index on a corner tile, the magma palette, and no background.", fig.alt = "Four-by-four association matrix for mtcars with a small coloured tile in the bottom-right corner of each cell and white backgrounds."---- st <- janusplot_style(colour_channel = "tile", palette = "magma") st plot(fit, colour_by = "asymmetry", background_by = "none", style = st) ## ----narrow, fig.width = 85 / 25.4, fig.height = 90 / 25.4, fig.cap = "The mtcars matrix at one journal column, 85 mm wide, with 6-point text.", fig.alt = "Small four-by-four association matrix for mtcars sized for a single journal column."---- narrow <- janusplot_style(text_size = 6, show_shape_legend = FALSE, show_glossary = FALSE) plot(fit, style = narrow) ## ----oneshot, eval = FALSE---------------------------------------------------- # janusplot(mtcars[, c("mpg", "hp", "wt", "qsec")], order = "hclust") # # is the same figure as # plot(janusplot_fit(mtcars[, c("mpg", "hp", "wt", "qsec")]), # order = "hclust") ## ----pair, eval = has_mass, fig.width = 7, fig.height = 3.6, fig.cap = "The two directions of the mcycle pair: acceleration as a smooth function of time on the left, time as a smooth function of acceleration on the right.", fig.alt = "Two scatterplot panels side by side for the mcycle data, each with a fitted spline and confidence band; the left curve dips and recovers over time, the right curve is a much smoother fit of time on acceleration."---- fit_mc <- janusplot_fit(MASS::mcycle) janusplot_pair(fit_mc, x = "times", y = "accel") ## ----pair-both, eval = has_mass, fig.width = 4.5, fig.height = 4.5, fig.cap = "Both directions of the mcycle pair in one standardised panel.", fig.alt = "One panel with standardised time on the horizontal axis and standardised acceleration on the vertical axis, with two fitted curves in different colours."---- janusplot_pair(fit_mc, x = "times", y = "accel", display = "both") ## ----pair-grid, fig.width = 7, fig.height = 5, fig.cap = "Four pairs of the mtcars fit, two blocks per stack in two stacks.", fig.alt = "Grid of four pair blocks, two per column, each block holding the two directional fits of one mtcars pair."---- janusplot_pair(fit, x = c("hp", "wt", "qsec", "qsec"), y = c("mpg", "mpg", "hp", "wt"), ncol = 2L) ## ----compare, eval = has_penguins, fig.cap = "Penguin traits fitted marginally (grey dashed curves) and adjusted for species (solid curves with bands), points coloured by species. A red border marks a cell whose trend changed: rising, falling or non-monotone.", fig.alt = "Four-by-four matrix for the penguin traits with two curves in every cell, a dashed grey marginal curve and a solid species-adjusted curve, and red borders on the cells whose trend changed."---- fit_pen_adj <- janusplot_fit(palmerpenguins::penguins, pen_vars, adjust = ~ species) cmp <- janusplot_compare(fit_pen, fit_pen_adj, labels = c("marginal", "species-adjusted")) cmp plot(cmp, group = "species") ## ----compare-numbers, include = FALSE, eval = has_penguins-------------------- n_changed <- sum(cmp$table$shape_changed, na.rm = TRUE) n_trend <- sum(cmp$table$trend_changed, na.rm = TRUE) pen_cor <- stats::cor(palmerpenguins::penguins[, pen_vars], use = "pairwise.complete.obs")["bill_depth_mm", ] ## ----direction, eval = has_kernr && has_mass---------------------------------- janusplot_direction(fit_mc, x = "times", y = "accel", seed = 2026L) ## ----table-------------------------------------------------------------------- tab <- summary(fit_het) tab[, c("var_y", "var_x", "edf", "association_p", "asymmetry_index", "shape_category")] ## ----missing, fig.cap = "airquality matrix under pairwise NA handling: each cell is fitted from the rows complete for that pair, so the cells not touching Ozone or Solar.R use all 153 rows.", fig.alt = "Four-by-four association matrix for airquality's Ozone, Solar.R, Wind and Temp, fitted with pairwise-complete rows."---- # airquality has missing values in Ozone and Solar.R fit_aq <- janusplot_fit(airquality[, c("Ozone", "Solar.R", "Wind", "Temp")], na_action = "pairwise") plot(fit_aq, annotations = c("n", "signif")) ## ----derivs-fit, fig.height = 4.5, fig.cap = "Fitted-smooth (level) view of x against a rippled-monotone y1 = x + sin(3x) and a convex y2 = 0.5x^2.", fig.alt = "Three-by-three fitted-smooth matrix for x, y1 (x plus a sine ripple) and y2 (a quadratic), all against x."---- 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) ) fit_df <- janusplot_fit(df) plot(fit_df, display = "fit") ## ----derivs-d1, fig.height = 4.5, fig.cap = "First-derivative view of the same three variables: the y1 panel oscillates around a positive baseline (a locally reversing gain despite the globally increasing fit above), while the y2 panel is the straight line f'(x) = x.", fig.alt = "First-derivative panels for x against y1 and y2, showing an oscillating curve for y1 and a straight increasing line for y2."---- plot(fit_df, display = "d1") ## ----derivs-d2, fig.height = 4.5, fig.cap = "Second-derivative view: y1's curvature oscillates through zero repeatedly (the sine term), while y2's curvature is flat and positive (a constant second derivative, as expected for a quadratic).", fig.alt = "Second-derivative panels for x against y1 and y2, showing an oscillating curve for y1 and a flat positive line for y2."---- plot(fit_df, display = "d2") ## ----derivs-sim, fig.height = 4.5, fig.cap = "First-derivative view with simultaneous 95% Monte Carlo confidence bands: the y1 panel's oscillation crosses zero with a band wide enough to show where a reversal is and is not distinguishable from noise.", fig.alt = "First-derivative panels with simultaneous confidence ribbons for x against y1 and y2."---- fit_sim <- janusplot_fit(df, derivative_ci = "simultaneous", derivative_ci_nsim = 2000L) plot(fit_sim, display = "d1") ## ----cite--------------------------------------------------------------------- citation("janusplot") .