Skip to contents

Arranges a main scatter plot with marginal histogram or density plots on the top (x-axis distribution) and right (y-axis distribution). The axes are shared so that the marginal bins align exactly with the scatter axes.

Usage

compose_marginal(
  main,
  top,
  right,
  widths = c(4, 1),
  heights = c(1, 4),
  guides = "collect"
)

Arguments

main

A plotit scatter plot (must have both x and y mapped).

top

A plotit histogram or density plot for the x variable. Typically built from the same data and x mapping as main, with the same fill/colour aesthetic to match.

right

A plotit histogram or density plot for the y variable. Same conventions as top. Call project_cartesian(flip = TRUE) on this plot before passing it so the y-axis aligns with the scatter.

widths

Relative column widths for the main and right-marginal panels. Default c(4, 1) = right marginal is 1/5 of total width.

heights

Relative row heights for the top-marginal and main panels. Default c(1, 4) = top marginal is 1/5 of total height.

guides

"collect" (default) to merge legends across all panels, "keep" to keep them separate, NULL for patchwork auto-detect.

Value

A plotit_composite object. Pipe to label_title(), style(), export() as usual.

Examples

main <- plotit(iris, encode(x = Sepal.Width, y = Sepal.Length, colour = Species)) |> mark_point()
top <- plotit(iris, encode(x = Sepal.Width, fill = Species)) |> mark_histogram(bins = 15, alpha = 0.5)
right <- plotit(iris, encode(x = Sepal.Length, fill = Species)) |>
  mark_histogram(bins = 15, alpha = 0.5) |>
  project_cartesian(flip = TRUE)
compose_marginal(main, top, right)