knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5 )
library(glyph)
Every plot on this page is built by the code above it and drawn by glyph's
D3 renderer. They are live, so you can hover, click, brush and zoom them.
All the examples use mtcars, which means you can paste them into R and get
the same result.
interact()
adds behaviour to the plot. Here it turns on tooltips and makes a point grow
when the mouse is over it.
titles()
adds a title in the same pipeline.
glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> interact(tooltip = TRUE, hover = "enlarge") |> titles(title = "Motor Trend Cars")
Move the mouse over a point to enlarge it, and rest on it to see the tooltip.
animate()
describes how marks enter the plot. With stagger, each bar starts a little
after the one before, so they draw in sequence.
glyph(mtcars, x = cyl, y = mpg) |> mark_bar() |> animate(transition = "slide", stagger = 50)
Reload the page, or run the chunk again in R, to watch the bars slide in.
theme_tokens()
sets the look of a plot from a few values. This example uses the "dark"
preset. You can also set single tokens such as bg, font or accent.
Text, grid and title colours are worked out from the background so they
stay readable.
glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> interact(tooltip = TRUE) |> theme_tokens(preset = "dark") |> titles(title = "Dark Theme Example")
mark_text()
draws one label per row. With smart_repel = TRUE, labels that overlap are
nudged apart. mtcars keeps the car names in its row names, so the first
line copies them into a column.
mtcars_named <- data.frame(model = rownames(mtcars), mtcars, row.names = NULL) glyph(mtcars_named, x = wt, y = mpg) |> mark_point(color = cyl) |> mark_text(label = model, smart_repel = TRUE)
compose()
places several plots in one widget. Both panels below allow brushing, and
linked_selections = TRUE connects them. Rows you select in one panel are
highlighted in the other.
p1 <- glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> interact(brush = TRUE) p2 <- glyph(mtcars, x = hp, y = mpg) |> mark_point(color = cyl) |> interact(brush = TRUE) compose(p1, p2, type = "hstack", linked_selections = TRUE)
Drag a rectangle over a few points in either panel. The same cars light up in both.
facet()
splits a plot into one panel per value of a variable. You can facet by rows,
by columns or by both.
glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> facet(cols = cyl)
marginals()
draws the distribution of each axis along its edge. The choices are
"histogram", "density" and "boxplot".
glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) |> marginals(x = "histogram", y = "density")
inset()
puts a smaller plot inside a corner of the main one. It works well for a
detail view or a second breakdown of the same data.
main_plot <- glyph(mtcars, x = wt, y = mpg) |> mark_point(color = cyl) detail_plot <- glyph(mtcars, x = cyl, y = mpg) |> mark_bar() inset(main_plot, detail_plot, position = "top-right")
animate(by = ..., transition = "morph") shows one group of the data at a
time. When the group changes, each mark moves to its new position.
glyph(mtcars, x = wt, y = mpg) |> mark_point(size = hp, color = cyl) |> animate(by = gear, transition = "morph", duration = 800)
Use the play/pause button to step through the gear groups.
mark_boxplot()
summarises a numeric variable for each group. The box runs from the first
to the third quartile and the line across it is the median. Values beyond
the whiskers are drawn as separate points.
glyph(mtcars, x = factor(cyl), y = mpg) |> mark_boxplot() |> interact(tooltip = TRUE, hover = "outline")
Rest the pointer on a box to read its quartiles.
mark_violin()
draws each group's distribution as a mirrored density curve. Every violin
uses the same density scale, so a wide section means many cars with similar
values.
glyph(mtcars, x = factor(gear), y = mpg) |> mark_violin(color = factor(gear)) |> interact(tooltip = TRUE)
mark_sina()
draws one point per row and spreads the points sideways by how common their
value is. They fill the same outline a violin would, but each point is
still a car, so you can brush across them.
glyph(mtcars, x = factor(cyl), y = mpg) |> mark_violin(style = list(opacity = 0.2)) |> mark_sina() |> interact(tooltip = TRUE, brush = TRUE)
Drag across a group to select the cars in it.
mark_histogram()
counts the rows that fall in each bin of a numeric variable. Mapping colour
stacks the counts for each group within a bin.
glyph(mtcars, x = mpg) |> mark_histogram(binwidth = 2.5, color = factor(am)) |> interact(tooltip = TRUE)
mark_polygon()
draws closed shapes from x/y vertices, one shape per value of group. Here
each shape is the convex hull of a cylinder group, found with chull(), and
drawn under the cars it encloses.
hulls <- do.call(rbind, lapply(split(mtcars, mtcars$cyl), function(d) { d[chull(d$wt, d$mpg), ] })) glyph(mtcars, x = wt, y = mpg) |> mark_polygon(data = hulls, group = factor(cyl), color = factor(cyl), style = list(opacity = 0.2)) |> mark_point(color = factor(cyl)) |> interact(tooltip = TRUE)
The function reference covers every mark, scale and layout function in more detail.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.