Skip to content

Beeswarms instead of histograms

June 28, 2018

Histograms are good, density plots are also good. Violin and bean plots too. Recently I had someone ask for a plot where you could see each individual point along a continuum, give the points specific colours based on a second variable (similar to the figure), which deviates somewhat from the typical density type plots. Apparently, they’re called beeplots or beeswarms. And there’s a way to make them in R (of course, there’s probably more than one… ggplot2??).

beeswarm

Here’s one way (slightly modified from the packages help files)…

library(beeswarm) # assuming you've installed it ;)
data(breast)
beeswarm(time_survival ~ ER, data = breast,
         pch = 16, pwcol = 1 + as.numeric(event_survival),
         xlab = "", ylab = "Follow-up time (months)", horizontal = TRUE, 
         labels = c("ER neg", "ER pos"), method = "c")
legend("topright", legend = c("Yes", "No"),
       title = "Censored", pch = 16, col = 1:2)

Horizontal is optional of course…

Feel free to comment if you know of other approaches.

See here for more examples.

Hope that helps someone 🙂

UPDATE… I just remembered…these plots are also sometimes referred to as turnip plots…

 

Advertisements

From → R

9 Comments
  1. talegari permalink

    Thanks for the handy package. I would have done this with ggplot2:

    “`
    library(“dplyr”)
    library(“ggplot2”)
    data(“breast”, package = “beeswarm”)

    # prepare data
    breast %>%
    count(ER, time_survival, event_survival) %>%
    mutate(ER = factor(ER, levels = c(“pos”, “neg”))) %>%
    mutate(Censored = factor(event_survival)) %>%
    # plotting
    ggplot(aes(time_survival, fill = Censored)) +
    geom_bar(aes(weight = n)) +
    facet_grid(ER ~.) +
    # beautification
    xlab(“Follow-up time (months)”) +
    ylab(“Counts (number of people)”) +
    ggtitle(“Beeswarm plot using ggplot2”)
    “`

  2. kent37 permalink

    The ggplot2 version is geom_dotplot: https://ggplot2.tidyverse.org/reference/geom_dotplot.html
    It doesn’t seem to be as flexible as beeswarm.

    • I thought as much. I’m not a ggplot2 user though. I vaguely remember trying it with ggplot2 and failed to colour the points as I wanted them…

  3. Hugo permalink

    You can use the ggbeeswarm package for a ggplot2 solution:

    https://github.com/eclarke/ggbeeswarm

    Here is an example of code that gives you what you are looking for:

    “`
    library(ggbeeswarm)

    data(“breast”, package = “beeswarm”)

    # Vertical beeswarm
    ggplot(breast, aes(x = ER, y = time_survival, col = factor(event_survival))) +
    geom_beeswarm()

    # Horizontal beeswarm
    ggplot(breast, aes(x = time_survival, y = ER, col = factor(event_survival))) +
    geom_beeswarm(groupOnX=FALSE)
    “`

    (let’s hope the markdown formatting will work!)

  4. Jordan permalink

    There’s also a package for that: ggbeeswarm. 😉
    See the github site for more details: https://github.com/eclarke/ggbeeswarm

  5. Jordan permalink

    Also, if you want a easy/quick way to highlight (color) specific points in ggplot2, try: gghighlight (https://github.com/yutannihilation/gghighlight).
    The author just released a version that makes gghighlight function similar to dplyr::filter. See here for article: https://yutani.rbind.io/post/2018-06-16-re-intro-to-gghighlight/

Trackbacks & Pingbacks

  1. Beeswarms instead of histograms – Mubashir Qasim
  2. Distilled News | Data Analytics & R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: