# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
install.packages("data.table")
}if (!requireNamespace("jsonlite", quietly = TRUE)) {
install.packages("jsonlite")
}if (!requireNamespace("ggplotify", quietly = TRUE)) {
install.packages("ggplotify")
}if (!requireNamespace("beanplot", quietly = TRUE)) {
install.packages("beanplot")
}
# Load packages
library(data.table)
library(jsonlite)
library(ggplotify)
library(beanplot)
Beanplot
The beanplot is a method of visualizing the distribution characteristics.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
data.table
;jsonlite
;ggplotify
;beanplot
Data Preparation
The loaded data is data set (data on treatment outcomes of different treatment regimens).
# Load data
<- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/beanplot/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
data
# convert data structure
<- as.numeric(factor(data[, 2], levels = unique(data[, 2])))
GroupOrder 2] <- paste0(data[,2], " ", as.numeric(factor(data[, 3])))
data[, <- cbind(data, GroupOrder)
data
# 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
<- as.ggplot(function() {
p 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
