# 安装包
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}if (!requireNamespace("ggthemes", quietly = TRUE)) {
install.packages("ggthemes")
}
# 加载包
library(ggbeeswarm)
library(ggthemes)
蜜蜂群图
蜜蜂群图是一种类似蜜蜂群,样本间互不干涉的列散点图。
环境配置
系统: Cross-platform (Linux/MacOS/Windows)
编程语言: R
依赖包:
ggplot2
;ggthemes
数据准备
载入数据为不同分组及其数据。
# 加载数据
<- read.table("files/Hiplot/012-beeswarm-data.txt", header = T)
data
# 整理数据格式
1] <- factor(data[, 1], levels = unique(data[, 1]))
data[, colnames(data) <- c("Group", "y")
# 查看数据
head(data)
Group y
1 G1 5.1
2 G1 4.9
3 G1 4.7
4 G1 4.6
5 G1 5.0
6 G1 5.4
可视化
# 蜜蜂群图
<- ggplot(data, aes(Group, y, color = Group)) +
p geom_beeswarm(alpha = 1, size = 0.8) +
labs(x = NULL, y = "value") +
ggtitle("BeeSwarm Plot") +
scale_color_manual(values = c("#e04d39","#5bbad6","#1e9f86")) +
theme_stata() +
theme(text = element_text(family = "Arial"),
plot.title = element_text(size = 12,hjust = 0.5),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 1),
legend.position = "right",
legend.direction = "vertical",
legend.title = element_text(size = 10),
legend.text = element_text(size = 10))
p

不同颜色表示不同组群 ,点表示数据。