A beeswarm plot displays individual data points
in a way that they don’t overlap, resulting in a
swarming effect that resembles a swarm of
bees.
In the previous
post we saw how to build one with R and the beeswarm package.
In
this post, we’ll see how to custom the dots: changing
their size, color and shape.
For this post, we need to install and load the beeswarm package.
We can install it from CRAN using
install.packages("beeswarm")
. 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 a beeswarm()
function, and thanks
to its col
argument, we can change the color of the dots.
Here, we’ll use the blue
color.
The pch
argument allows to change the symbol of the
dots. Here, we’ll use the 16
symbol since it’s the same as
before but filled.
The cex
argument allows to change the size of the dots.
Here, we’ll use a 2.5
size (default is 1).
The method
argument allows to change the
position of the dots. Here are the available
options:
swarm
(default): the dots are placed randomly, but they
don’t overlap.center
: the dots are symetrically placed around the
center of the plot.hex
: the dots are placed on a hexagonal grid.square
: the dots are placed on a square grid.In our case, we’ll use the center
method:
This post explains how to customize the dots in a beeswarm plot.
You might also be interested in how to flip a beeswarm plot or how to create a grouped beeswarm plot.
👋 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! 🔥