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.
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.
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.
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.
marginals()
draws the distribution of each axis along its edge. The choices are
"histogram", "density" and
"boxplot".
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.
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.
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.
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.
The function reference covers every mark, scale and layout function in more detail.