Grouped beeswarm plot



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 grouped beeswarm plot in R using the beeswarm package.

Beeswarm Data to Viz

Packages


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:

# install.packages("beeswarm")
library(beeswarm)


Dataset


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:

data(iris)

Default grouped beeswarm plot


The package comes with a beeswarm() function, and thanks to the ~ operator, we can easily create a grouped beeswarm plot. Here, we’ll use the Species column to group the data:

The following code basically means:

plot Sepal.Length for each Species using the iris dataset.

beeswarm(
  Sepal.Length ~ Species,
  data=iris
)

Use different colors


Even tough the default option does what we want, having the same color for all the Species makes the plot less readable. We can use the col argument to change the color of the dots:

beeswarm(
  Sepal.Length ~ Species, 
  data=iris,
  col=c("orange", "lightblue", "magenta"),
  pch = 19 # fill the dots
)

Custom position behavior


When you have lots of data points, it can be useful to change the position behavior of the dots in order to avoid overlapping between groups.

Fortunately, the corral argument allows you to change this. The available options are:

Here’s an example with the gutter option:

beeswarm(
  Sepal.Length ~ Species, 
  data=iris,
  col=c("orange", "lightblue", "magenta"),
  pch = 19, # fill the dots
  corral = "gutter"
)

Going further


This post explains how to create a grouped beeswarm plot with R.

You might also be interested in how to create a beeswarm plot with ggplot2.

Related chart types


Violin
Density
Histogram
Boxplot
Ridgeline



❤️ 10 best R tricks ❤️

👋 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! 🔥