Package {glyph}


Title: A Grammar of Interactive Graphics
Version: 0.3.0
Description: Builds interactive graphics from a declarative specification. Marks, scales, tooltips, zoom, brushing, linked selections, animated transitions and multi-plot layouts are declared in one pipeline and stored as a plain R list that can be inspected, saved as JSON or converted to 'Vega-Lite'. Plots are drawn in the browser with 'D3.js' and work at the console, in R Markdown and 'Quarto' documents, and in 'Shiny' apps, where brushed selections are returned to R.
License: MIT + file LICENSE
Encoding: UTF-8
Depends: R (≥ 4.1.0)
Config/testthat/edition: 3
Imports: htmlwidgets (≥ 1.6.0), jsonlite (≥ 1.8.0), rlang (≥ 1.1.0), cli (≥ 3.6.0), grDevices, stats, utils, tools
Suggests: testthat (≥ 3.0.0), knitr, rmarkdown, chromote, htmltools, withr, shiny (≥ 1.5.0), callr, httpuv, pkgload
VignetteBuilder: knitr
URL: https://github.com/josh45-source/glyph, https://josh45-source.github.io/glyph/
BugReports: https://github.com/josh45-source/glyph/issues
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-11-30 14:08:36 UTC; Joshua
Author: Joash Joshua Ayo [aut, cre], Mike Bostock [ctb, cph] (Author of the bundled D3.js library)
Maintainer: Joash Joshua Ayo <joashjoshua789@gmail.com>
Repository: CRAN
Date/Publication: 2026-11-30 14:30:02 UTC

glyph: A Grammar of Interactive Graphics

Description

Builds interactive graphics from a declarative specification. Marks, scales, tooltips, hover and click events, zoom, brushing, linked selections, animated transitions and multi-plot layouts are declared in one pipeline and stored as a plain R list that can be inspected, saved as 'JSON' or converted to 'Vega-Lite'. Themes are set from a small set of design tokens. Plots are drawn in the browser as 'htmlwidgets' using 'D3.js'.

Acknowledgements

glyph's design draws directly on the ideas and prior art of others:

This package also bundles D3.js (Copyright Mike Bostock, ISC License); see inst/COPYRIGHTS for the full notice.

Author(s)

Maintainer: Joash Joshua Ayo joashjoshua789@gmail.com

Authors:

Other contributors:

See Also

Useful links:


Add animation/transition behavior

Description

Add animation/transition behavior

Usage

animate(
  spec,
  transition = "morph",
  duration = 500,
  stagger = 0,
  by = NULL,
  easing = "ease-in-out",
  loop = FALSE
)

Arguments

spec

A glyph_spec

transition

Transition type: "morph", "fade", "slide", "none"

duration

Duration in milliseconds

stagger

Delay between successive marks in ms (for entrance effects)

by

Column that defines animation keyframes. The plot shows one value of it at a time and moves between them, e.g. by = year plays through the years.

easing

Easing function: "linear", "ease-in", "ease-out", "ease-in-out", "bounce", "elastic"

loop

Repeat animation (TRUE, FALSE, or number of times)

Value

Modified glyph_spec

Examples

# Entrance animation: bars grow from baseline
glyph(mtcars, x = cyl, y = mpg) |>
  mark_bar() |>
  animate(transition = "slide", stagger = 50)

# Keyframe animation: morph between groups
glyph(mtcars, x = wt, y = mpg) |>
  mark_point(size = hp, color = cyl) |>
  animate(by = gear, transition = "morph", duration = 800)

Animation Grammar

Description

Animations in glyph are declarative state transitions, not frame-by-frame rendering. You describe what changes and how it should interpolate, and the renderer handles the rest.

Details

Two animation paradigms:


Colour palettes

Description

The palette names that scale_color() and set_scale("color", range =) accept, the d3 colour schemes:

A sequential or diverging palette is sampled at as many evenly spaced colours as there are categories. Names are matched without regard to case, so "viridis" works too.

Usage

color_palettes()

Value

A named list of character vectors: the palette names of each kind.

Examples

color_palettes()

glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = factor(gear)) |>
  scale_color("Dark2")

Compile a glyph spec to a resolved representation

Description

Compile a glyph spec to a resolved representation

Usage

compile(spec, engine = "auto", width = NULL, height = NULL)

## S3 method for class 'glyph_spec'
compile(spec, engine = "auto", width = NULL, height = NULL)

## S3 method for class 'glyph_layout'
compile(spec, engine = "auto", width = NULL, height = NULL)

Arguments

spec

A glyph_spec or glyph_layout

engine

Rendering backend. Only "html" (D3.js via htmlwidgets) is currently implemented; "auto" resolves to "html". Other values are rejected — earlier versions accepted "canvas"/"webgl" and silently ignored them (no such renderer ever existed), which was misleading.

width

Width in pixels (NULL for auto)

height

Height in pixels (NULL for auto)

Value

A glyph_compiled object (a list with resolved data + JSON)

A glyph_compiled object containing the resolved spec, JSON string, and engine selection

A glyph_compiled object containing the resolved layout spec and JSON string


Compose multiple glyph specs into a layout

Description

Compose multiple glyph specs into a layout

Usage

compose(
  ...,
  type = "hstack",
  widths = NULL,
  heights = NULL,
  gap = 10,
  shared_scales = FALSE,
  linked_selections = FALSE,
  title = NULL
)

Arguments

...

glyph_spec objects or layout objects (for nesting)

type

Layout type: "hstack", "vstack", "grid", "wrap"

widths

Relative widths for columns (e.g. c(2, 1) for 2:1 split)

heights

Relative heights for rows

gap

Gap between plots in px

shared_scales

Share axis scales across plots: TRUE, FALSE, "x", "y", or "both"

linked_selections

Share interaction selections across plots

title

Overall layout title

Value

A glyph_layout object

Examples

p1 <- glyph(mtcars, x = wt, y = mpg) |> mark_point()
p2 <- glyph(mtcars, x = wt, y = hp) |> mark_point()
p3 <- glyph(mtcars, x = hp, y = mpg) |> mark_line()

# Horizontal stack
compose(p1, p2, type = "hstack")

# Grid with linked brushing
compose(p1, p2, p3, type = "wrap",
        linked_selections = TRUE, shared_scales = "x")

Export to various formats

Description

Export to various formats

Usage

export(spec, file, width = 800, height = 600)

Arguments

spec

A glyph_spec or glyph_compiled

file

Output file path. Extension determines format. Currently implemented: .html (interactive widget) and .json (the compiled spec). Other extensions (.svg, .png, .pdf) are planned but not yet implemented, and currently just print a message.

width

Width in pixels

height

Height in pixels

Details

A single self-contained .html file needs pandoc. RStudio ships with pandoc, so this works out of the box there; elsewhere, install pandoc and make sure it is on your PATH. Without pandoc, export() still writes the .html file but puts the JavaScript it needs in a ⁠<name>_files⁠ folder next to it, and tells you so. Keep the two together when moving or sharing the plot.

Value

Invisibly returns the output file path


Facet a plot by one or two variables

Description

Splits a plot into small multiples, one panel for each value of rows, cols, or both. Panels share their axes unless free_scales lets them vary.

With both rows and cols, panels form a grid, as in ggplot2's facet_grid(): column labels go in a strip across the top and row labels in a strip down the right side. With rows alone, panels stack in one column with their labels on the right. With cols alone, or with wrap, panels wrap into rows with each label in a strip on top.

Shared axes are drawn once, on the outer panels: the y axis on the left column and the x axis on the bottom row. A strip shows the value alone, such as "4", unless labeller gives another label. Strip font size, weight and colours are theme tokens (see theme_tokens()).

Usage

facet(
  spec,
  rows = NULL,
  cols = NULL,
  free_scales = "none",
  wrap = NULL,
  labeller = NULL
)

Arguments

spec

A glyph_spec

rows

Row faceting variable: a bare column name, a computed expression (e.g. factor(cyl)), or NULL

cols

Column faceting variable: a bare column name, a computed expression, or NULL

free_scales

Which axes may vary between panels: "none" (the default), "x", "y" or "both". How they vary depends on the layout:

  • In a grid (both rows and cols), as in ggplot2's facet_grid(), a free y axis is shared along each row and a free x axis down each column. With "y", every row gets its own y scale and its tick labels appear on the left column only; with "x", every column gets its own x scale, labelled on the bottom row only; "both" does both. An empty panel uses its row's and column's scales.

  • With one faceting variable (rows or cols alone, or wrap), as in ggplot2's facet_wrap(), each panel's free axis is its own and every panel labels it.

wrap

If only one variable, wrap into a grid with this many columns

labeller

The labels shown in the strips, similar to ggplot2::labeller(). One of:

  • a named character vector, such as c("4" = "Four cylinders"), which looks up each value by its name. Values not in the vector keep their own label.

  • a function that takes the values as a character vector and returns their labels, one for each.

  • a list with one of the above per faceting variable, named as the variable is written in rows or cols, such as list(cyl = c("4" = "Four"), gear = function(x) paste(x, "gears")). Variables not in the list keep their own labels.

Value

Modified glyph_spec

Examples

p <- glyph(mtcars, x = wt, y = mpg) |> mark_point()

# Columns by gear in a strip on top, rows by cyl in a strip on the right.
p |> facet(rows = cyl, cols = gear)

# One variable: panels wrap, with labels on top.
p |> facet(cols = cyl, wrap = 3)

# Free y in a grid: one y scale per row, shared by that row's panels.
p |> facet(rows = cyl, cols = gear, free_scales = "y")

# Free y with one variable: every panel has its own y scale.
p |> facet(cols = cyl, free_scales = "y")

# Relabel some values with a named vector. 8 keeps its own label.
p |> facet(cols = cyl, labeller = c("4" = "Four cylinders", "6" = "Six cylinders"))

# A labeller per variable: a lookup for cyl, a function for gear.
p |> facet(rows = cyl, cols = gear, labeller = list(
  cyl = c("4" = "Four", "6" = "Six", "8" = "Eight"),
  gear = function(x) paste(x, "gears")
))

Create a Glyph Visualization

Description

Entry point for the glyph grammar. Creates a specification that describes a visualization declaratively. The spec is a plain nested list: it can be inspected, serialized to JSON, and compiled into an interactive HTML widget.

Usage

glyph(data = NULL, ...)

Arguments

data

A data frame or tibble, or NULL if each mark will supply its own data.

...

Global aesthetic mappings, written as bare column names or expressions, e.g. glyph(mtcars, x = wt, y = mpg). Every mark uses these unless it sets its own.

Value

A glyph_spec object

Examples

spec <- glyph(mtcars, x = wt, y = mpg) |>
  mark_point()
summary(spec)

Use glyph plots in Shiny

Description

glyphOutput() places a plot in a Shiny app's UI and renderGlyph() draws it from the server. renderGlyph() takes a plot from glyph(), a layout from compose(), or a widget from render().

What the user selects in the plot is sent back to the server as ⁠input$<outputId>_selected⁠, holding row numbers of the data frame given to glyph(). See the section below and vignette("shiny", package = "glyph").

shiny is not installed with glyph. Install it to use these functions.

Usage

glyphOutput(outputId, width = "100%", height = "400px")

renderGlyph(expr, env = parent.frame(), quoted = FALSE)

Arguments

outputId

Output variable to read the plot from.

width, height

Size of the plot, as a CSS size such as "100%" or "400px", or a number of pixels.

expr

An expression that returns a glyph_spec, a glyph_layout or a widget made by render(). It can also return NULL, which shows nothing and clears the selection.

env

The environment in which to evaluate expr.

quoted

Is expr a quoted expression (with quote())? This is useful if you want to save an expression in a variable.

Value

glyphOutput() returns the UI element for the plot. renderGlyph() returns a render function to assign to an element of output.

Selections

A selection is sent when the user ends a brush, clicks a mark with interact(click = "select"), or clicks a legend entry added with selection(type = "legend"). It is not sent while a brush is being dragged. Clearing a brush (with the "Clear selection" button, Escape, or a click on an empty part of the plot) sends NULL, as does clicking a selected mark again. So does redrawing the plot, which happens when renderGlyph() runs again or the output is resized.

For a single plot the value is an integer vector of row numbers, so data[input$plot_selected, ] gives the selected rows. Row numbers are positions in the data frame, not row names. They are correct when rows with missing values were dropped and when a mapping is computed (such as x = log(wt)). Selecting a box, violin, histogram bin or polygon selects every row it summarises. A mark drawn from its own data takes no part in the value. A brush that takes in no rows gives integer(0).

A legend selects the rows whose entries are still shown. With every entry shown there is no selection, and the value is NULL.

A facet() grid has one data frame, so its value is one vector: the rows selected in any of its panels.

A compose() layout gives a list with one entry per panel, in the order the panels were given, named after them when compose() was called with names. Each entry holds the rows selected in that panel, as row numbers of that panel's data, or NULL when nothing is selected there. A panel that is itself a layout gives a list of the same shape. When no panel has a selection the whole value is NULL.

output$plots <- renderGlyph(compose(cars = p1, iris = p2))
# input$plots_selected is NULL, or for example
# list(cars = c(3L, 18L), iris = NULL)

Examples

if (interactive() && requireNamespace("shiny", quietly = TRUE)) {
  library(shiny)

  ui <- fluidPage(
    glyphOutput("plot"),
    tableOutput("rows")
  )

  server <- function(input, output) {
    output$plot <- renderGlyph(
      glyph(mtcars, x = wt, y = mpg) |>
        mark_point() |>
        interact(brush = TRUE)
    )
    output$rows <- renderTable(mtcars[input$plot_selected, ], rownames = TRUE)
  }

  shinyApp(ui, server)
}

Add an inset plot

Description

Add an inset plot

Usage

inset(spec, inset, position = "top-right")

Arguments

spec

The main glyph_spec

inset

A glyph_spec to place as an inset

position

Where to place: "top-right", "top-left", "bottom-right", "bottom-left", or a list(x, y, width, height) with proportions 0-1

Value

Modified glyph_spec


Add interactive behaviors to a glyph spec

Description

Add interactive behaviors to a glyph spec

Usage

interact(
  spec,
  tooltip = FALSE,
  zoom = FALSE,
  brush = FALSE,
  hover = NULL,
  click = NULL,
  crossfilter = FALSE,
  nearest = FALSE
)

Arguments

spec

A glyph_spec

tooltip

Show values on hover. TRUE for auto-generated, or a glue-style template string like "{x}: {y} ({color})". On a line or area it shows the values of the data point nearest the pointer, on the nearest line when there are several.

zoom

Enable scroll-to-zoom and pan. The scroll wheel, a trackpad pinch, a two-finger pinch and a double-click/double-tap always zoom; dragging pans (see brush for what happens when both are enabled).

brush

Enable rectangular brush selection. When zoom is also enabled, dragging brushes by default and a small toggle next to the reset-zoom control switches dragging between selecting and panning, so one drag never does both at once.

hover

Highlight mark on hover ("enlarge", "brighten", "outline", or NULL)

click

Action on click: "select", "filter", "url", or a callback name

crossfilter

Link this plot's selections to other plots in a layout

nearest

Snap selection to nearest point (useful for line charts)

Value

Modified glyph_spec

Examples

glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = cyl) |>
  interact(
    tooltip = "{cyl} cylinders\n{mpg} mpg at {wt} tons",
    zoom = TRUE,
    hover = "enlarge",
    brush = TRUE,
    crossfilter = TRUE
  )

Interactivity

Description

Tooltips, hover effects, clicks, zoom, brushing and linked selections are part of the plot specification. They are declared with interact() and selection() in the same pipeline as the marks, stored in the spec, and run in the browser by the D3 renderer.

Details

Design philosophy: interactions are selections that filter or highlight marks. This follows Vega-Lite's insight that most interactions are really about defining subsets of the data. A tooltip is "select the nearest point, show its values." Brushing is "select points in a rectangle, highlight them."


Layout Composition

Description

Built-in multi-plot composition. No external packages needed. Layouts can express grids, stacks, insets, marginal plots, and arbitrary nesting. Composed plots can share selections for cross-filtering (linked brushing).


Add a marginal plot (histogram/density on axes)

Description

Draws the distribution of the x and y variables along the edges of the plot, as a histogram, density curve or boxplot.

Usage

marginals(spec, x = "histogram", y = "histogram", size = 0.15)

Arguments

spec

A glyph_spec

x

Marginal type for x-axis: "histogram", "density", "boxplot", NULL

y

Marginal type for y-axis: "histogram", "density", "boxplot", NULL

size

Proportion of plot area for marginals (0.0 to 0.4)

Value

Modified glyph_spec

Examples

glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = cyl) |>
  marginals(x = "histogram", y = "density")

Add an area mark

Description

Add an area mark

Usage

mark_area(spec, ..., data = NULL, style = list())

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

Value

Modified glyph_spec object with the area mark added


Add a bar mark

Description

Add a bar mark

Usage

mark_bar(spec, ..., data = NULL, style = list(), orient = "vertical")

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

orient

"vertical" or "horizontal"

Value

Modified glyph_spec object with the bar mark added


Add a boxplot mark

Description

Draws one box per group: the box spans the first to third quartile, the line inside it is the median, and the whiskers reach the most extreme values within coef times the interquartile range of the box. Values beyond the whiskers are drawn as outlier points.

Map y to the numeric variable and x to the grouping variable, which is treated as categorical even when it is numeric. Without x, all rows form one box. Mapping color to a different variable than x places one box per colour side by side within each x group.

The statistics are computed in R when the plot is compiled, using stats::quantile() with its default type = 7. Rows with a missing x or y are dropped first, with a warning.

Usage

mark_boxplot(
  spec,
  ...,
  data = NULL,
  style = list(),
  coef = 1.5,
  outliers = TRUE,
  width = 0.7
)

Arguments

spec

A glyph_spec

...

Aesthetic mappings: y (required, numeric), x and color.

data

Optional per-mark data override

style

Named list of fixed visual properties

coef

Length of the whiskers as a multiple of the interquartile range. Use Inf to extend them to the minimum and maximum.

outliers

Whether to draw values beyond the whiskers as points.

width

Width of each box as a fraction of the space for its group, between 0 and 1.

Value

Modified glyph_spec object with the boxplot mark added

Examples

glyph(mtcars, x = factor(cyl), y = mpg) |>
  mark_boxplot()

Add a histogram mark

Description

Counts how many rows fall into each bin of a numeric x variable and draws one bar per bin. Bins are closed on the right, like graphics::hist(), and the lowest bin also includes its left edge. Mapping color stacks one bar segment per colour within each bin.

With neither bins nor binwidth, the number of bins follows Sturges' rule and the edges are rounded to tidy values, as in graphics::hist(). All facet panels share the same bins.

The counts are computed in R when the plot is compiled. Rows with a missing x are dropped first, with a warning.

Usage

mark_histogram(
  spec,
  ...,
  data = NULL,
  style = list(),
  bins = NULL,
  binwidth = NULL
)

Arguments

spec

A glyph_spec

...

Aesthetic mappings: x (required, numeric) and color. There is no y mapping; the bar heights are the counts.

data

Optional per-mark data override

style

Named list of fixed visual properties

bins

Number of equal-width bins spanning the range of x.

binwidth

Width of each bin. Bin edges are multiples of binwidth. Takes precedence over bins.

Value

Modified glyph_spec object with the histogram mark added

Examples

glyph(mtcars, x = mpg) |>
  mark_histogram(binwidth = 2.5)

Add a line mark

Description

Add a line mark

Usage

mark_line(spec, ..., data = NULL, style = list(), interpolate = "monotone")

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

interpolate

Interpolation method: "linear", "monotone", "step", "basis"

Value

Modified glyph_spec object with the line mark added


Description

Add a link/edge mark (for networks, sankeys, slope graphs)

Usage

mark_link(spec, ..., data = NULL, style = list())

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

Value

Modified glyph_spec object with the link mark added


Add a point mark (scatterplot)

Description

Add a point mark (scatterplot)

Usage

mark_point(spec, ..., data = NULL, style = list())

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

Value

Modified glyph_spec

Examples

spec <- glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = cyl, size = hp, tooltip = "{cyl} cyl, {mpg} mpg")

Add a polygon mark

Description

Draws closed shapes from x/y vertices. Rows that share a value of group are the vertices of one polygon, joined in row order, and the last vertex is joined back to the first. Without a group mapping, all rows form one polygon. Mapping color fills each polygon by its value; a polygon whose rows have different colours is split into one polygon per colour.

Each polygon is one element: its tooltip shows the polygon rather than a single vertex, and brushing or clicking it selects all of its rows. A tooltip template can use any column that has the same value on every row of the polygon, such as a name or an id. Rows with a missing x or y are dropped first, with a warning, and polygons left with fewer than three vertices are not drawn.

Usage

mark_polygon(spec, ..., data = NULL, style = list(), radius = 0)

Arguments

spec

A glyph_spec

...

Aesthetic mappings: x and y (required, numeric), group and color.

data

Optional per-mark data override

style

Named list of fixed visual properties: fill (a colour used when color isn't mapped), stroke (outline colour, the theme's foreground colour by default), stroke_width (in pixels, 0 for no outline) and opacity (of the fill).

radius

Radius of rounded corners, in pixels. Each corner is rounded by at most half the length of its shorter edge. 0 (the default) keeps sharp corners.

Value

Modified glyph_spec object with the polygon mark added

Examples

triangles <- data.frame(
  id = rep(c("a", "b"), each = 3),
  x = c(0, 1, 0.5, 1.5, 2.5, 2),
  y = c(0, 0, 1, 0, 0, 1)
)
glyph(triangles, x = x, y = y) |>
  mark_polygon(group = id, color = id)

Add a ribbon/band mark (for confidence intervals, ranges)

Description

Add a ribbon/band mark (for confidence intervals, ranges)

Usage

mark_ribbon(spec, ..., data = NULL, style = list())

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

Value

Modified glyph_spec object with the ribbon mark added


Add a rule (reference line) mark

Description

Add a rule (reference line) mark

Usage

mark_rule(
  spec,
  ...,
  data = NULL,
  style = list(),
  x_intercept = NULL,
  y_intercept = NULL
)

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

x_intercept

Fixed x position for vertical rule

y_intercept

Fixed y position for horizontal rule

Value

Modified glyph_spec object with the rule mark added


Add a sina mark

Description

Draws every row as a point, spread sideways within its group in proportion to the density of values at that height. The points fill the same outline a mark_violin() with the same settings would draw, so the two combine well.

Each point is still a row of the data, so tooltips, brushing and linked selections work on individual rows, as they do for mark_point(). The sideways positions are computed in R when the plot is compiled and are the same every time.

Usage

mark_sina(
  spec,
  ...,
  data = NULL,
  style = list(),
  bw = "nrd0",
  adjust = 1,
  width = 0.9
)

Arguments

spec

A glyph_spec

...

Aesthetic mappings: y (required, numeric), x, color and size.

data

Optional per-mark data override

style

Named list of fixed visual properties

bw

Smoothing bandwidth, passed to stats::density(): a number or the name of a bandwidth rule such as "nrd0" (the default) or "SJ".

adjust

Multiplier for the bandwidth. Values above 1 give smoother curves.

width

Width of the spread at the highest density, as a fraction of the space for its group, between 0 and 1.

Value

Modified glyph_spec object with the sina mark added

Examples

glyph(mtcars, x = factor(cyl), y = mpg) |>
  mark_violin() |>
  mark_sina()

Add a text/label mark

Description

Add a text/label mark

Usage

mark_text(spec, ..., data = NULL, style = list(), smart_repel = TRUE)

Arguments

spec

A glyph_spec

...

Aesthetic mappings (x, y, color, size, shape, alpha, tooltip)

data

Optional per-mark data override

style

Named list of fixed visual properties

smart_repel

Automatically avoid label overlaps (TRUE by default). Inspired by ggrepel, but simpler: rather than a full force-directed simulation, glyph resolves overlaps by iterative pairwise nudging. It repeatedly compares every pair of label bounding boxes and, for any pair that still overlaps, pushes them a small step apart vertically (up to 50 passes, stopping as soon as none overlap).

Value

Modified glyph_spec object with the text mark added


Add a violin mark

Description

Draws the distribution of a numeric variable for each group as a mirrored kernel density curve, trimmed to the range of the group's values. All violins in the mark use the same density scale, so their widths can be compared directly.

Map y to the numeric variable and x to the grouping variable, which is treated as categorical even when it is numeric. Without x, all rows form one violin. Mapping color to a different variable than x places one violin per colour side by side within each x group.

The densities are computed in R when the plot is compiled, with stats::density(). A group needs at least two distinct values; groups with fewer are left out with a warning.

Usage

mark_violin(
  spec,
  ...,
  data = NULL,
  style = list(),
  bw = "nrd0",
  adjust = 1,
  width = 0.9
)

Arguments

spec

A glyph_spec

...

Aesthetic mappings: y (required, numeric), x and color.

data

Optional per-mark data override

style

Named list of fixed visual properties

bw

Smoothing bandwidth, passed to stats::density(): a number or the name of a bandwidth rule such as "nrd0" (the default) or "SJ".

adjust

Multiplier for the bandwidth. Values above 1 give smoother curves.

width

Width of the widest violin as a fraction of the space for its group, between 0 and 1.

Value

Modified glyph_spec object with the violin mark added

Examples

glyph(mtcars, x = factor(cyl), y = mpg) |>
  mark_violin()

Visual Marks

Description

Marks are the layers that draw data: points, lines, bars, areas, text, rules, ribbons, links and polygons. Each ⁠mark_*()⁠ call adds one layer to the spec. mark_boxplot(), mark_violin(), mark_sina() and mark_histogram() summarise the distribution of a variable; their statistics are computed from the data when the plot is compiled.

Mappings are passed as bare column names or expressions, and are added to the global mappings set in glyph(). A mark can also take its own data, so one plot can draw from several data frames. Fixed visual properties that don't depend on the data, such as size, opacity or stroke_width, go in style. Interaction and animation are set for the whole plot with interact() and animate().


Draw a glyph layout when it is printed

Description

A printed layout draws itself, the same way a single plot does. At the console it opens in the viewer, and in R Markdown, Quarto and vignettes it is embedded in the document. Call render() only to set the width and height, and use summary() for a text description.

In a non-interactive session that isn't knitting a document, such as Rscript, there is nowhere to draw, so printing gives a one-line description instead.

Usage

## S3 method for class 'glyph_layout'
print(x, ...)

## S3 method for class 'glyph_layout'
knit_print(x, ..., options = NULL)

Arguments

x

A glyph_layout object

...

Passed on to knitr when knitting; otherwise ignored.

options

knitr chunk options, supplied by knitr.

Value

print() invisibly returns x. knit_print() returns the rendered widget's HTML for knitr to include in the document.


Draw a glyph spec when it is printed

Description

A printed spec draws itself. At the console it opens in the viewer, and in R Markdown, Quarto and vignettes it is embedded in the document. Call render() only to set the width and height, and use summary() for a text description.

In a non-interactive session that isn't knitting a document, such as Rscript, there is nowhere to draw, so printing gives a one-line description instead.

Usage

## S3 method for class 'glyph_spec'
print(x, ...)

## S3 method for class 'glyph_spec'
knit_print(x, ..., options = NULL)

Arguments

x

A glyph_spec object

...

Passed on to knitr when knitting; otherwise ignored.

options

knitr chunk options, supplied by knitr.

Value

print() invisibly returns x. knit_print() returns the rendered widget's HTML for knitr to include in the document.


Render a glyph spec as an htmlwidget

Description

Render a glyph spec as an htmlwidget

Usage

render(spec, width = NULL, height = NULL)

Arguments

spec

A glyph_spec, glyph_layout, or glyph_compiled

width

Widget width

height

Widget height

Value

An htmlwidget object that renders the visualization in an HTML viewer


Rendering Pipeline

Description

The rendering pipeline compiles a glyph_spec into output. The spec is first resolved (evaluate quosures, drop rows with missing mapped values, merge defaults), then serialized to JSON, then handed to a backend:

Details

A glyph spec is a plain data structure, and nothing is drawn until the compile() step resolves it into a concrete render tree. That means you can:


Scales

Description

Scales map data values to visual properties. Every scale is set with one function, ⁠set_scale(<aesthetic>, <type>, ...)⁠, where the aesthetic is the channel (such as "x" or "color") and the type is how values are mapped (such as "log" or "time"). scale_color(), scale_log() and scale_time() are shortcuts for common cases.

Calls for the same aesthetic add up: each changes only the options it is given and keeps the rest (see set_scale()).

Usage

scale_color(spec, palette = "Tableau10", ...)

scale_log(spec, aesthetic = "y", base = 10, ...)

scale_time(spec, aesthetic = "x", ...)

Arguments

spec

A glyph_spec

palette

A palette name, such as "Tableau10" (the default), "Set2", "Blues" or "Viridis" (see color_palettes()), or a vector of colours. Colour is always discrete; see range in set_scale(). Left out, the colour scale keeps the palette set by an earlier call.

...

Additional arguments passed to set_scale()

aesthetic

Which channel: "x", "y", "color", "size", "shape", "alpha"

base

Log base (default 10). Left out, the scale keeps a base set by an earlier call.

Value

Modified glyph_spec object

Modified glyph_spec object

Modified glyph_spec object

Functions


Add a selection parameter (advanced)

Description

For complex interactions: define a named selection that can be referenced by marks and scales. This is how you build linked views, conditional encoding, and interactive legends.

Usage

selection(
  spec,
  name,
  type = "point",
  on = "click",
  fields = NULL,
  resolve = "global"
)

Arguments

spec

A glyph_spec

name

Selection name (referenced in conditional encodings)

type

"point", "interval", or "legend"

on

Event trigger: "click", "mouseover", "drag"

fields

Which data fields the selection projects onto

resolve

For composed views: "global", "union", "intersect"

Value

Modified glyph_spec

Examples

# Interactive legend: click legend entries to filter
glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = cyl) |>
  selection("legend_filter", type = "legend", fields = "cyl")

Define a scale for an aesthetic channel

Description

Sets how an aesthetic (an axis, or colour) maps data to the plot: its type, domain, colours, label, tick format and so on.

Calls for the same aesthetic add up. Each call changes only the options it is given and keeps the others from earlier calls; for an option given again, the last call wins. An option left out is never reset to its default, so set_scale("y", reverse = TRUE) followed by set_scale("y", label = "Miles per gallon") keeps the axis reversed. The shortcuts scale_color(), scale_log() and scale_time() work the same way. To drop an option set earlier, give it again as NULL (for domain, range, label, format or base) or as its default.

Usage

set_scale(
  spec,
  aesthetic,
  type = "auto",
  domain = NULL,
  range = NULL,
  nice = TRUE,
  zero = FALSE,
  reverse = FALSE,
  label = NULL,
  format = NULL,
  base = NULL,
  expand = 0.05
)

Arguments

spec

A glyph_spec

aesthetic

Which channel: "x", "y", "color", "size", "shape", "alpha"

type

Scale type: "linear", "log", "sqrt", "time", "ordinal", "band", "quantize", "threshold"

domain

Explicit domain (data range). NULL for auto. A domain set here is used exactly as given, with no padding (expand) and no rounding (nice), so it is also how to start the value axis of a bar, area or histogram somewhere other than zero.

range

Explicit output range. For "color", the colours of the marks and the legend: a palette name (see color_palettes(); case doesn't matter) or a vector of colours, as R names ("steelblue") or hex codes ("#1b9e77"), used in order and repeated if there are more categories than colours. Colour is always discrete: each category, or each distinct value of a numeric mapping, gets its own colour, in factor level order (sorted values otherwise). A sequential or diverging palette is sampled at that many evenly spaced colours. Continuous colour scales (a gradient with a colour bar) aren't supported yet.

nice

Round domain to nice values (TRUE/FALSE). A domain set here is never rounded.

zero

Force zero in domain (TRUE/FALSE). Bar, area and histogram marks always include zero on their value axis unless domain is set.

reverse

TRUE flips a position axis ("x" or "y"): y runs from the top down, x from right to left. Brushing, zoom and selections work as usual.

label

Axis/legend label (NULL for auto from column name)

format

Format string for the tick labels of a position axis: a d3-format specifier on a numeric axis, such as ",.0f" (thousands separators, no decimals), ".1%" (percentages) or "$,.2f" (dollars), or a d3-time-format string on a date axis, such as "%Y" or "%b %Y". compile() stops with an error for a numeric format that d3 can't read. Tooltips still show values as R prints them.

base

Log base, used when type = "log" (default 10 if NULL)

expand

Padding around the data on a position axis ("x" or "y"), as a fraction of the data's range added on each side before the axis is rounded to nice values, like ggplot2's default. The default, 0.05, keeps points at the data's minimum and maximum off the axis lines. 0 turns padding off. On a log scale the padding is a fraction of the range in log space. It applies to quantitative and date axes, not to a domain set here, which is used as given. Bars, areas and histograms still start flush on the axis at zero, padded only at their other end. Faceted and composed panels that share an axis are padded alike, so they still line up.

Value

Modified glyph_spec

Examples

glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = cyl) |>
  set_scale("y", "linear", zero = TRUE, label = "Miles per gallon") |>
  set_scale("color", "ordinal", range = "Set2")

# A vector of colours, in order of the factor's levels.
glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = factor(cyl)) |>
  scale_color(c("#1b9e77", "#d95f02", "#7570b3"))

# Tick formats: a thousands separator on y, dollars on x, and a reversed
# y axis.
glyph(mtcars, x = hp * 100, y = disp * 10) |>
  mark_point() |>
  set_scale("x", format = "$,.0f") |>
  set_scale("y", format = ",.0f", reverse = TRUE)

# Years on a date axis.
econ <- data.frame(date = seq(as.Date("1970-01-01"), by = "month", length.out = 240),
                   rate = cumsum(rnorm(240)) / 100 + 0.05)
glyph(econ, x = date, y = rate) |>
  mark_line() |>
  set_scale("x", format = "%Y") |>
  set_scale("y", format = ".1%")

# Calls add up: a label, then a palette, then a format, all kept.
glyph(mtcars, x = wt, y = mpg) |>
  mark_point(color = factor(cyl)) |>
  set_scale("color", label = "Cylinders") |>
  scale_color("Set2") |>
  set_scale("y", reverse = TRUE) |>
  set_scale("y", label = "Miles per gallon")

# No padding: the axes end at the data's minimum and maximum.
glyph(mtcars, x = wt, y = mpg) |>
  mark_point() |>
  set_scale("x", expand = 0) |>
  set_scale("y", expand = 0)

Summarize a glyph layout

Description

Summarize a glyph layout

Usage

## S3 method for class 'glyph_layout'
summary(object, ...)

Arguments

object

A glyph_layout object

...

Additional arguments (ignored)

Value

No return value, called for side effects (prints a summary of the layout to the console)


Summarize a glyph specification

Description

Summarize a glyph specification

Usage

## S3 method for class 'glyph_spec'
summary(object, ...)

Arguments

object

A glyph_spec object

...

Additional arguments (ignored)

Value

No return value, called for side effects (prints a summary of the specification to the console)


Set theme tokens

Description

Sets the plot's design tokens: fonts, colours, grid, border, padding and facet strips.

Calls combine in order, and for each token the last call wins. A preset sets every token, so it replaces tokens set by earlier calls; tokens named in the same call as the preset, or in later calls, override it. theme_tokens() never changes the title, subtitle or caption set with titles(), whichever is called first.

Usage

theme_tokens(
  spec,
  preset = NULL,
  font = NULL,
  font_size = NULL,
  bg = NULL,
  fg = NULL,
  accent = NULL,
  grid = NULL,
  border = NULL,
  padding = NULL,
  title_size = NULL,
  legend = NULL,
  strip_size = NULL,
  strip_weight = NULL,
  strip_fg = NULL,
  strip_bg = NULL
)

Arguments

spec

A glyph_spec

preset

A named preset: "light" (default), "dark", "minimal", "publication", "presentation". Presets set all tokens to coherent defaults.

font

Font family for all text

font_size

Base font size in px (axis labels scale relative to this)

bg

Background color

fg

Primary text/axis color

accent

Primary accent color (used for single-series marks)

grid

Grid line visibility: TRUE, FALSE, "x", "y"

border

Plot border: TRUE draws a line around the plot area (each panel's, in a facet grid) in the fg colour; FALSE draws none. The publication preset sets it.

padding

Padding around the plot area in px: the least room left on each side. A side gets more when it needs it, to fit the tick labels and axis title on the left and bottom, the title and subtitle on top, and the caption at the bottom.

title_size

Title font size multiplier (relative to font_size)

legend

Where the colour legend goes: "right" (the default), "bottom", or "none" for no legend. A plot that maps colour draws one, titled with the colour scale's label (the mapping, or set_scale("color", label =)), with an entry for each colour in factor level order. The plot area makes room for it: on the right, entries stack in as many columns as they need; at the bottom, they flow in rows under the x axis. Long labels are cut short with an ellipsis and shown in full on hover. A facet grid has one legend for all its panels; an inset() plot, too small for one, has none. With selection(type = "legend"), clicking an entry filters by it, and the legend is drawn (on the right) even with "none".

strip_size

Font size of facet strip labels in px. Follows font_size (at 0.9 of it) unless set.

strip_weight

Font weight of facet strip labels: "normal", "bold", or a number from 100 to 900.

strip_fg

Text colour of facet strip labels. Follows fg unless set.

strip_bg

Background colour of facet strips, or "transparent". Follows bg (a shade towards fg) unless set.

Value

Modified glyph_spec

Examples

glyph(mtcars, x = wt, y = mpg) |>
  mark_point() |>
  theme_tokens(preset = "dark")

# The last call wins: a dark preset with a border, then a larger font.
# The preset keeps the titles set before it.
glyph(mtcars, x = wt, y = mpg) |>
  mark_point() |>
  titles(title = "Motor Trend Cars", caption = "Source: mtcars") |>
  theme_tokens(border = TRUE) |>         # replaced by the preset below
  theme_tokens(preset = "dark", border = TRUE) |>
  theme_tokens(font_size = 14)

glyph(mtcars, x = wt, y = mpg) |>
  mark_point() |>
  theme_tokens(font = "IBM Plex Sans", bg = "#fafafa", grid = "y")

# The colour legend under the plot, or none at all.
p <- glyph(mtcars, x = wt, y = mpg) |> mark_point(color = factor(cyl))
p |> theme_tokens(legend = "bottom")
p |> theme_tokens(legend = "none")

# Facet strips: larger, normal-weight labels on a pale blue strip.
glyph(mtcars, x = wt, y = mpg) |>
  mark_point() |>
  facet(rows = cyl, cols = gear) |>
  theme_tokens(strip_size = 13, strip_weight = "normal", strip_bg = "#e8eef8")

Token-Based Theming

Description

A theme is set with design tokens: a small set of named values (font, colours, spacing, sizes) that cascade through the whole plot, much like CSS custom properties.

Details

Tokens cascade: setting bg changes the background, but also automatically adjusts text color for contrast, grid line opacity, and tooltip styling. You override only what you want; everything else adapts.


Add a title, subtitle, or caption

Description

The title and subtitle are centred above the plot and the caption sits at its bottom right, under the right edge of the plot area. The subtitle and caption are drawn smaller than the title, and the plot area shrinks to make room for all three. A facet grid has them once, around the whole grid.

Usage

titles(spec, title = NULL, subtitle = NULL, caption = NULL)

Arguments

spec

A glyph_spec

title

Main title

subtitle

Subtitle (below title)

caption

Caption (bottom of plot, for source attribution)

Value

Modified glyph_spec object with updated title metadata

Examples

glyph(mtcars, x = wt, y = mpg) |>
  mark_point() |>
  titles(title = "Motor Trend Cars", subtitle = "Weight vs fuel efficiency",
         caption = "Source: mtcars")

Export the spec as Vega-Lite JSON (interop)

Description

Because glyph's spec is structurally similar to Vega-Lite, we can export to Vega-Lite JSON for use in Python (Altair), JavaScript, or the Vega Editor.

Usage

to_vegalite(spec)

Arguments

spec

A glyph_spec

Value

A JSON string in Vega-Lite format