View source: R/beeswarm_plot.R
| beeswarm_plot | R Documentation |
A beeswarm plot displays individual data points along a numeric axis, spreading them out so they do not overlap (like a swarm of bees). It is a transparent alternative to box plots or violin plots because every observation is visible. When a grouping variable is supplied, one swarm is drawn per group along the opposite axis.
The layout is computed with a D3 force simulation that pulls each point toward its true value on the x-axis while resolving collisions so no two circles overlap.
The plot is interactive: hovering a point highlights it while dimming the
rest of the swarm, and (when tooltip columns are supplied) shows a
tooltip with those columns' values. When a group is supplied,
hovering a group label focuses that swarm and clicking a label toggles it
on or off. An optional mean/median reference line can be drawn per swarm.
The interactive behaviors are controlled by tooltip, hover,
animate, and stat.
beeswarm_plot(
data,
x,
group = NULL,
tooltip = NULL,
hover = TRUE,
animate = TRUE,
stat = c("none", "mean", "median"),
statColor = "#d62728",
col = "steelblue",
colorPalette = "Tableau10",
radius = 4,
opacity = 0.75,
stroke = "white",
strokeWidth = 0.5,
xtitle = NULL,
xtitleFontSize = 13,
title = NULL,
titleFontSize = 16,
xFontSize = 11,
yFontSize = 11,
font = "Verdana, Geneva, Tahoma, sans-serif",
bgcol = "white",
axisCol = "black",
width = NULL,
height = NULL
)
data |
The data frame containing the variables to consider. |
x |
The name of the numeric column whose distribution is displayed. |
group |
Optional. The name of a categorical column used to split
the data into separate swarms. Defaults to |
tooltip |
Optional. A column name, or a vector of column names, whose
values are shown in a tooltip when a point is hovered. Each column appears
on its own line as |
hover |
Logical. When |
animate |
Logical. When |
stat |
Character. Draws a reference line per swarm at the
|
statColor |
Color of the mean/median reference line and its label.
Defaults to |
col |
Fill color of the points when no |
colorPalette |
D3 categorical color scheme used when |
radius |
Radius of each point in pixels. Defaults to |
opacity |
Fill opacity of the points (0 to 1). Defaults to |
stroke |
Stroke color of the points. Defaults to |
strokeWidth |
Stroke width in pixels. Defaults to |
xtitle |
Optional. Title of the x-axis. |
xtitleFontSize |
Font size of the x-axis title. Defaults to |
title |
Optional. Title of the chart. |
titleFontSize |
Font size of the chart title. Defaults to |
xFontSize |
Font size of the x-axis tick labels. Defaults to |
yFontSize |
Font size of the y-axis group labels. Defaults to |
font |
The font family used for all text. Defaults to
|
bgcol |
Background color of the SVG. Defaults to |
axisCol |
Color of axis lines, ticks, and labels. Defaults to
|
width |
Optional. The width of the SVG output. |
height |
Optional. The height of the SVG output. |
An SVG beeswarm plot.
library(ggplot2) # for the mpg dataset
# Single swarm — distribution of highway fuel economy
beeswarm_plot(
data = mpg,
x = "hwy",
col = "steelblue",
xtitle = "Highway miles per gallon",
title = "Distribution of hwy"
)
# Grouped swarm — hwy by vehicle class
beeswarm_plot(
data = mpg,
x = "hwy",
group = "class",
xtitle = "Highway miles per gallon",
title = "Highway fuel economy by vehicle class"
)
# Grouped swarm on the iris dataset
beeswarm_plot(
data = iris,
x = "Sepal.Length",
group = "Species",
colorPalette = "Set2",
radius = 5,
xtitle = "Sepal length (cm)",
title = "Sepal length by species"
)
# Interactive extras: median line per swarm and a tooltip label column
beeswarm_plot(
data = mpg,
x = "hwy",
group = "class",
tooltip = "model",
stat = "median",
xtitle = "Highway miles per gallon",
title = "Highway fuel economy by vehicle class"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.