蜜蜂群图

作者

[编辑] 郑虎;

[审核] .

蜜蜂群图是一种类似蜜蜂群,样本间互不干涉的列散点图。

环境配置

  • 系统: Cross-platform (Linux/MacOS/Windows)

  • 编程语言: R

  • 依赖包: ggplot2; ggthemes

# 安装包
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("ggthemes", quietly = TRUE)) {
  install.packages("ggthemes")
}

# 加载包
library(ggbeeswarm)
library(ggthemes)

数据准备

载入数据为不同分组及其数据。

# 加载数据
data <- read.table("files/Hiplot/012-beeswarm-data.txt", header = T)

# 整理数据格式
data[, 1] <- factor(data[, 1], levels = unique(data[, 1]))
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

可视化

# 蜜蜂群图
p <- ggplot(data, aes(Group, y, color = Group)) +
  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
图 1: 蜜蜂群图

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