Beanplot

Authors

[Editor] Hu Zheng;

[Contributors]

The beanplot is a method of visualizing the distribution characteristics.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: ggplotify; beanplot

# Install packages
if (!requireNamespace("ggplotify", quietly = TRUE)) {
  install.packages("ggplotify")
}
if (!requireNamespace("beanplot", quietly = TRUE)) {
  install.packages("beanplot")
}

# Load packages
library(ggplotify)
library(beanplot)

Data Preparation

The loaded data is data set (data on treatment outcomes of different treatment regimens).

# Load data
data <- read.table("files/Hiplot/011-beanplot-data.txt", header = T)

# convert data structure
GroupOrder <- as.numeric(factor(data[, 2], levels = unique(data[, 2])))
data[, 2] <- paste0(data[,2], " ", as.numeric(factor(data[, 3])))
data <- cbind(data, GroupOrder)

# View data
head(data)
     Y     X  Group GroupOrder
1  4.2 low 1 treat1          1
2 11.5 low 1 treat1          1
3  7.3 low 1 treat1          1
4  5.8 low 1 treat1          1
5  6.4 low 1 treat1          1
6 10.0 low 1 treat1          1

Visualization

# Beanplot
p <- as.ggplot(function() {
  beanplot(Y ~ reorder(X, GroupOrder, mean), data = data, ll = 0.04,
           main = "Bean Plot", ylab = "Y", xlab = "X", side = "both",
           border = NA, horizontal = F, 
           col = list(c("#2b70c4", "#2b70c4"),c("#e9c216", "#e9c216")),
           beanlines = "mean", overallline = "mean", kernel = "gaussian")
  
  legend("bottomright", fill = c("#2b70c4", "#e9c216"),
         legend = levels(factor(data[, 3])))
})

p
FigureΒ 1: Beanplot