# 导入 ggplot2 包
library(ggplot2)
# 示例数据框,包含数值数据和分组信息
data <- data.frame(
Group = c(rep("Group A",10), rep("Group B",15),rep("Group C",20)),
Value = c(rnorm(10, mean = 10, sd = 2),
rnorm(15, mean = 15, sd = 2),
rnorm(20, mean = 20, sd = 2))
)
# 使用 ggplot2 绘制箱线图,同时显示每个数据点
ggplot(data, aes(x = Group, y = Value, fill = Group)) +
geom_boxplot(outlier.shape = NA) + # 不显示异常值
geom_jitter(width = 0.2, alpha = 0.7) + # 绘制数据点,width控制点的分布宽度,alpha控制透明度
labs(title = "Box Plot with Unequal Group Sizes and Data Points",
x = "Group",
y = "Value") +
theme_minimal() # 使用简洁的主题