A beeswarm plot or swarmplot is
a type of data visualization that displays individual data points in a
way that they don’t overlap, resulting in a
swarming effect that resembles a swarm of bees.
In
this post, we’ll see how to create a basic beeswarm plot in R using the
ggbeeswarm package.
For this post, we need to install and load the ggbeeswarm package.
We can install it from CRAN using
install.packages("ggbeeswarm")
. Then, we can load it:
Since beeswarm plots are made to
visualize individual data points, we need a dataset
that contains numerical values. Here, we’ll use the iris
dataset, which is a built-in dataset in R.
We can easily load it:
The package comes with 2 main functions:
geom_beeswarm()
: to create a beeswarm
plot with ggplot2geom_quasirandom()
: to create a quasirandom
plot, which is a mix between a beeswarm and a
jitter plot.In this post, we’ll focus on the geom_beeswarm()
function.
We can super easily flip the plot by changing the x
and
y
aesthetics:
We can change the color of the points and the overall theme of the
plot using the theme()
function:
By default, the geom_beeswarm()
function will use the
swarm
method to position the points. We can change this
behavior using the method
argument. Here are the
available options:
swarm
: the default methodcompactswarm
: similar to swarm
, but the
points are more compactcenter
: the points are centered on the
x-axishex
: the points are positioned in
hexagonssquare
: the points are positioned in
squaresHere’s an example with center
:
This post explains how to create and customize a beeswarm plot with ggbeeswarm.
You might also be interested in how to create a grouped beeswarm plot with ggplot2.
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥