Beeswarms instead of histograms
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??).
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.
Hope that helps someone 🙂
UPDATE… I just remembered…these plots are also sometimes referred to as turnip plots…
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”)
“`
Thanks for the code. The idea was to use the points though, rather than a bar…
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…
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!)
There’s also a package for that: ggbeeswarm. 😉
See the github site for more details: https://github.com/eclarke/ggbeeswarm
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/