Beeswarm Plot - R Package

R Package

This example shows a basic beeswarm plot developed in R. A beeswarm plot is like a strip plot (continuous y-axis, categorical x-axis), but with the x-values shifted just far enough left or right to avoid data points overlaying one another.

Producing a beeswarm plot in R involves using the beeswarm package.

Source Code

R
################################################################################
# Library
################################################################################     
library(Hmisc)
library(beeswarm)
library(lattice)
library(latticeExtra)
library(grid)


################################################################################
# Data
################################################################################

b <- read.csv("beeswarm.csv")
names(b) <- tolower(names(b))
levels(b$ridageyr)[1] <- "85"
b$ridageyr <- as.numeric(as.character(b$ridageyr))
b$ridageyr2 <- cut2(b$ridageyr,g=2)

   
################################################################################
# Figure 
################################################################################

pdf("beeswarm.pdf",width=8,height=7)

beeswarm(lbxige~lbxi_any,data=b, method='center',
         log=T,
         labels=c("Atopic","Non-Atopic"),
         main="Relation between Atopy and Total IgE among Asthmatics",
         ylab="Serum total IgE antibody (kU/L)",
         xlab="",
         pch=19,col=2:3)

dev.off()

Data

The above graph is based on data from the NHANES study. The graph is generated based on a comma-delimited dataset (.CSV file type) where each row is a distinct data point to be plotted. Download

top