Grouped beeswarm plot with ggbeeswarm



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.

Beeswarm Data to Viz

Packages


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:

# install.packages("ggbeeswarm")
library(ggbeeswarm)
library(ggplot2)


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)

Grouped beeswarm


We can easily create a grouped beeswarm plot by specifying a categorical variable in the aes() function.

ggplot(iris,aes(x=Species, y=Sepal.Length)) +
  geom_beeswarm()

Color by group


And in order to make the plot more readable, we can add some color to the points using the colour argument.

ggplot(iris,aes(x=Species, y=Sepal.Length, colour=Species)) +
  geom_beeswarm()

Customize colors


We can also customize the colors using the scale_color_manual() function. And thanks to the theme_minimal() function, we can make the plot a bit more elegant.

ggplot(iris,aes(x=Species, y=Sepal.Length, colour=Species)) +
  geom_beeswarm() +
  scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9")) +
  theme_minimal()

Going further


This post explains how to create a grouped beeswarm plot with R and the ggbeeswarm package.

You might also be interested in how to customize a beeswarm plot.

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