| Title: | 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'. |
| Authors: | Joash Joshua Ayo [aut, cre], Mike Bostock [ctb, cph] (Author of the bundled D3.js library) |
| Maintainer: | Joash Joshua Ayo <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1.9000 |
| Built: | 2026-10-21 06:16:04 UTC |
| Source: | https://github.com/josh45-source/glyph |
Add animation/transition behavior
animate( spec, transition = "morph", duration = 500, stagger = 0, by = NULL, easing = "ease-in-out", loop = FALSE )animate( spec, transition = "morph", duration = 500, stagger = 0, by = NULL, easing = "ease-in-out", loop = FALSE )
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. |
easing |
Easing function: "linear", "ease-in", "ease-out", "ease-in-out", "bounce", "elastic" |
loop |
Repeat animation (TRUE, FALSE, or number of times) |
Modified glyph_spec
# 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)# 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)
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.
Two animation paradigms:
Transitions: animate between data states (e.g. year 2020 → 2021). Marks morph smoothly, entering/exiting as data changes.
Entrances: animate marks appearing on first render (stagger bars growing from zero, points fading in).
Compile a glyph spec to a resolved representation
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)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)
spec |
A glyph_spec or glyph_layout |
engine |
Rendering backend. Only |
width |
Width in pixels (NULL for auto) |
height |
Height in pixels (NULL for auto) |
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
compose( ..., type = "hstack", widths = NULL, heights = NULL, gap = 10, shared_scales = FALSE, linked_selections = FALSE, title = NULL )compose( ..., type = "hstack", widths = NULL, heights = NULL, gap = 10, shared_scales = FALSE, linked_selections = FALSE, title = NULL )
... |
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 |
A glyph_layout object
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")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
export(spec, file, width = 800, height = 600)export(spec, file, width = 800, height = 600)
spec |
A glyph_spec or glyph_compiled |
file |
Output file path. Extension determines format. Currently
implemented: |
width |
Width in pixels |
height |
Height in pixels |
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.
Invisibly returns the output file path
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 a single faceting variable, wrap sets how many
panels go in each row.
facet(spec, rows = NULL, cols = NULL, free_scales = "none", wrap = NULL)facet(spec, rows = NULL, cols = NULL, free_scales = "none", wrap = NULL)
spec |
A glyph_spec |
rows |
Row faceting variable: a bare column name, a computed
expression (e.g. |
cols |
Column faceting variable: a bare column name, a computed
expression, or |
free_scales |
"none", "x", "y", "both" |
wrap |
If only one variable, wrap into a grid with this many columns |
Modified glyph_spec
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.
glyph(data = NULL, ...)glyph(data = NULL, ...)
data |
A data frame or tibble, or |
... |
Global aesthetic mappings, written as bare column names or
expressions, e.g. |
A glyph_spec object
spec <- glyph(mtcars, x = wt, y = mpg) |> mark_point() summary(spec)spec <- glyph(mtcars, x = wt, y = mpg) |> mark_point() summary(spec)
Add an inset plot
inset(spec, inset, position = "top-right")inset(spec, inset, position = "top-right")
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 |
Modified glyph_spec
Add interactive behaviors to a glyph spec
interact( spec, tooltip = FALSE, zoom = FALSE, brush = FALSE, hover = NULL, click = NULL, crossfilter = FALSE, nearest = FALSE )interact( spec, tooltip = FALSE, zoom = FALSE, brush = FALSE, hover = NULL, click = NULL, crossfilter = FALSE, nearest = FALSE )
spec |
A glyph_spec |
tooltip |
Show values on hover. TRUE for auto-generated, or a glue-style template string like "{x}: {y} ({color})". |
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 |
Enable rectangular brush selection. When |
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) |
Modified glyph_spec
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 )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 )
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.
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."
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).
Draws the distribution of the x and y variables along the edges of the plot, as a histogram, density curve or boxplot.
marginals(spec, x = "histogram", y = "histogram", size = 0.15)marginals(spec, x = "histogram", y = "histogram", size = 0.15)
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) |
Modified glyph_spec
glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> marginals(x = "histogram", y = "density")glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> marginals(x = "histogram", y = "density")
Add an area mark
mark_area(spec, ..., data = NULL, style = list())mark_area(spec, ..., data = NULL, style = list())
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 |
Modified glyph_spec object with the area mark added
Add a bar mark
mark_bar(spec, ..., data = NULL, style = list(), orient = "vertical")mark_bar(spec, ..., data = NULL, style = list(), orient = "vertical")
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" |
Modified glyph_spec object with the bar mark added
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.
mark_boxplot( spec, ..., data = NULL, style = list(), coef = 1.5, outliers = TRUE, width = 0.7 )mark_boxplot( spec, ..., data = NULL, style = list(), coef = 1.5, outliers = TRUE, width = 0.7 )
spec |
A glyph_spec |
... |
Aesthetic mappings: |
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 |
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. |
Modified glyph_spec object with the boxplot mark added
glyph(mtcars, x = factor(cyl), y = mpg) |> mark_boxplot()glyph(mtcars, x = factor(cyl), y = mpg) |> mark_boxplot()
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.
mark_histogram( spec, ..., data = NULL, style = list(), bins = NULL, binwidth = NULL )mark_histogram( spec, ..., data = NULL, style = list(), bins = NULL, binwidth = NULL )
spec |
A glyph_spec |
... |
Aesthetic mappings: |
data |
Optional per-mark data override |
style |
Named list of fixed visual properties |
bins |
Number of equal-width bins spanning the range of |
binwidth |
Width of each bin. Bin edges are multiples of |
Modified glyph_spec object with the histogram mark added
glyph(mtcars, x = mpg) |> mark_histogram(binwidth = 2.5)glyph(mtcars, x = mpg) |> mark_histogram(binwidth = 2.5)
Add a line mark
mark_line(spec, ..., data = NULL, style = list(), interpolate = "monotone")mark_line(spec, ..., data = NULL, style = list(), interpolate = "monotone")
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" |
Modified glyph_spec object with the line mark added
Add a link/edge mark (for networks, sankeys, slope graphs)
mark_link(spec, ..., data = NULL, style = list())mark_link(spec, ..., data = NULL, style = list())
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 |
Modified glyph_spec object with the link mark added
Add a point mark (scatterplot)
mark_point(spec, ..., data = NULL, style = list())mark_point(spec, ..., data = NULL, style = list())
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 |
Modified glyph_spec
spec <- glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl, size = hp, tooltip = "{cyl} cyl, {mpg} mpg")spec <- glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl, size = hp, tooltip = "{cyl} cyl, {mpg} mpg")
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.
mark_polygon(spec, ..., data = NULL, style = list(), radius = 0)mark_polygon(spec, ..., data = NULL, style = list(), radius = 0)
spec |
A glyph_spec |
... |
Aesthetic mappings: |
data |
Optional per-mark data override |
style |
Named list of fixed visual properties: |
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. |
Modified glyph_spec object with the polygon mark added
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)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)
mark_ribbon(spec, ..., data = NULL, style = list())mark_ribbon(spec, ..., data = NULL, style = list())
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 |
Modified glyph_spec object with the ribbon mark added
Add a rule (reference line) mark
mark_rule( spec, ..., data = NULL, style = list(), x_intercept = NULL, y_intercept = NULL )mark_rule( spec, ..., data = NULL, style = list(), x_intercept = NULL, y_intercept = NULL )
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 |
Modified glyph_spec object with the rule mark added
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.
mark_sina( spec, ..., data = NULL, style = list(), bw = "nrd0", adjust = 1, width = 0.9 )mark_sina( spec, ..., data = NULL, style = list(), bw = "nrd0", adjust = 1, width = 0.9 )
spec |
A glyph_spec |
... |
Aesthetic mappings: |
data |
Optional per-mark data override |
style |
Named list of fixed visual properties |
bw |
Smoothing bandwidth, passed to |
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. |
Modified glyph_spec object with the sina mark added
glyph(mtcars, x = factor(cyl), y = mpg) |> mark_violin() |> mark_sina()glyph(mtcars, x = factor(cyl), y = mpg) |> mark_violin() |> mark_sina()
Add a text/label mark
mark_text(spec, ..., data = NULL, style = list(), smart_repel = TRUE)mark_text(spec, ..., data = NULL, style = list(), smart_repel = TRUE)
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). |
Modified glyph_spec object with the text mark added
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.
mark_violin( spec, ..., data = NULL, style = list(), bw = "nrd0", adjust = 1, width = 0.9 )mark_violin( spec, ..., data = NULL, style = list(), bw = "nrd0", adjust = 1, width = 0.9 )
spec |
A glyph_spec |
... |
Aesthetic mappings: |
data |
Optional per-mark data override |
style |
Named list of fixed visual properties |
bw |
Smoothing bandwidth, passed to |
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. |
Modified glyph_spec object with the violin mark added
glyph(mtcars, x = factor(cyl), y = mpg) |> mark_violin()glyph(mtcars, x = factor(cyl), y = mpg) |> mark_violin()
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().
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.
## S3 method for class 'glyph_layout' print(x, ...) ## S3 method for class 'glyph_layout' knit_print(x, ..., options = NULL)## S3 method for class 'glyph_layout' print(x, ...) ## S3 method for class 'glyph_layout' knit_print(x, ..., options = NULL)
x |
A glyph_layout object |
... |
Passed on to knitr when knitting; otherwise ignored. |
options |
knitr chunk options, supplied by knitr. |
print() invisibly returns x. knit_print() returns the
rendered widget's HTML for knitr to include in the document.
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.
## S3 method for class 'glyph_spec' print(x, ...) ## S3 method for class 'glyph_spec' knit_print(x, ..., options = NULL)## S3 method for class 'glyph_spec' print(x, ...) ## S3 method for class 'glyph_spec' knit_print(x, ..., options = NULL)
x |
A glyph_spec object |
... |
Passed on to knitr when knitting; otherwise ignored. |
options |
knitr chunk options, supplied by knitr. |
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
render(spec, width = NULL, height = NULL)render(spec, width = NULL, height = NULL)
spec |
A glyph_spec, glyph_layout, or glyph_compiled |
width |
Widget width |
height |
Widget height |
An htmlwidget object that renders the visualization
in an HTML viewer
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:
"html" (default): htmlwidgets + D3.js for interactive viewing. This is currently the only implemented renderer; the visual output is SVG, drawn by D3 inside the widget.
"svg", "canvas", "webgl", "pdf": standalone static/ large-data export backends. Planned, not yet implemented.
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:
Inspect the compiled spec as JSON (for debugging or export)
Serialize it and render on a different machine
Export it to Vega-Lite JSON (near 1:1 mapping)
Add new backends later without changing the user-facing spec API
Define a scale for an aesthetic channel
scale( spec, aesthetic, type = "auto", domain = NULL, range = NULL, nice = TRUE, zero = FALSE, reverse = FALSE, label = NULL, format = NULL, base = NULL )scale( spec, aesthetic, type = "auto", domain = NULL, range = NULL, nice = TRUE, zero = FALSE, reverse = FALSE, label = NULL, format = NULL, base = NULL )
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 as given, 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: a palette name or vector. |
nice |
Round domain to nice values (TRUE/FALSE) |
zero |
Force zero in domain (TRUE/FALSE). Bar, area and histogram
marks always include zero on their value axis unless |
reverse |
Reverse the scale |
label |
Axis/legend label (NULL for auto from column name) |
format |
Format string for tick labels (e.g. "$.2f", "%b %Y") |
base |
Log base, used when |
Modified glyph_spec
glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> scale("y", "linear", zero = TRUE, label = "Miles per gallon") |> scale("color", "ordinal", range = "Set2")glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> scale("y", "linear", zero = TRUE, label = "Miles per gallon") |> scale("color", "ordinal", range = "Set2")
Scales map data values to visual properties. Every scale is
set with one function, 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.
scale_color(spec, palette = "Tableau10", ...) scale_log(spec, aesthetic = "y", base = 10, ...) scale_time(spec, aesthetic = "x", ...)scale_color(spec, palette = "Tableau10", ...) scale_log(spec, aesthetic = "y", base = 10, ...) scale_time(spec, aesthetic = "x", ...)
spec |
A glyph_spec |
palette |
A named palette: "viridis", "Set2", "Tableau10", "Blues", etc. |
... |
Additional arguments passed to |
aesthetic |
Which channel: "x", "y", "color", "size", "shape", "alpha" |
base |
Log base (default 10) |
Modified glyph_spec object
Modified glyph_spec object
Modified glyph_spec object
scale_color(): Set color palette by name
scale_log(): Log-transform an axis
scale_time(): Time/date axis
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.
selection( spec, name, type = "point", on = "click", fields = NULL, resolve = "global" )selection( spec, name, type = "point", on = "click", fields = NULL, resolve = "global" )
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" |
Modified glyph_spec
# Interactive legend: click legend entries to filter glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> selection("legend_filter", type = "legend", fields = "cyl")# Interactive legend: click legend entries to filter glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> selection("legend_filter", type = "legend", fields = "cyl")
Summarize a glyph layout
## S3 method for class 'glyph_layout' summary(object, ...)## S3 method for class 'glyph_layout' summary(object, ...)
object |
A glyph_layout object |
... |
Additional arguments (ignored) |
No return value, called for side effects (prints a summary of the layout to the console)
Summarize a glyph specification
## S3 method for class 'glyph_spec' summary(object, ...)## S3 method for class 'glyph_spec' summary(object, ...)
object |
A glyph_spec object |
... |
Additional arguments (ignored) |
No return value, called for side effects (prints a summary of the specification to the console)
Set theme tokens
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 )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 )
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/FALSE |
padding |
Padding around the plot area in px |
title_size |
Title font size multiplier (relative to font_size) |
Modified glyph_spec
glyph(mtcars, x = wt, y = mpg) |> mark_point() |> theme_tokens(preset = "dark") glyph(mtcars, x = wt, y = mpg) |> mark_point() |> theme_tokens(font = "IBM Plex Sans", bg = "#fafafa", grid = "y")glyph(mtcars, x = wt, y = mpg) |> mark_point() |> theme_tokens(preset = "dark") glyph(mtcars, x = wt, y = mpg) |> mark_point() |> theme_tokens(font = "IBM Plex Sans", bg = "#fafafa", grid = "y")
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.
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
titles(spec, title = NULL, subtitle = NULL, caption = NULL)titles(spec, title = NULL, subtitle = NULL, caption = NULL)
spec |
A glyph_spec |
title |
Main title |
subtitle |
Subtitle (below title) |
caption |
Caption (bottom of plot, for source attribution) |
Modified glyph_spec object with updated title metadata
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.
to_vegalite(spec)to_vegalite(spec)
spec |
A glyph_spec |
A JSON string in Vega-Lite format