# 用R语言怎么画小提琴图 ## 一、什么是小提琴图 小提琴图(Violin Plot)是箱线图与核密度图的结合体,它不仅能展示数据的分布形态(通过宽度变化),还能显示四分位数等统计量(通常内嵌箱线图)。这种可视化方法特别适合展示**多组数据的分布比较**。 ### 主要组成部分: 1. **密度曲线**:左右对称的"小提琴"形状,宽度表示数据密度 2. **箱线图组件**:通常包含中位数线、四分位框和须线 3. **数据点**:可选择叠加原始数据点显示 ## 二、准备工作 ### 1. 安装必要包 ```r install.packages("ggplot2") # 主要绘图包 install.packages("vioplot") # 基础绘图系统替代方案 install.packages("ggpubr") # 增强ggplot2的统计功能
library(ggplot2) data(iris) # 使用R内置的鸢尾花数据集 head(iris)
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + geom_violin() + labs(title = "基础小提琴图", x = "鸢尾花种类", y = "萼片长度") + theme_minimal()
library(vioplot) vioplot(Sepal.Length ~ Species, data = iris, col = c("#1b9e77", "#d95f02", "#7570b3"), main = "vioplot绘制的小提琴图")
ggplot(iris, aes(Species, Sepal.Length)) + geom_violin(adjust = 0.5) # 值越小越平滑
ggplot(iris, aes(Species, Sepal.Length)) + geom_violin() + stat_summary(fun = median, geom = "point", size = 2, color = "red")
ggplot(iris, aes(Sepal.Length, Species)) + geom_violin() + coord_flip()
ggplot(tidyr::pivot_longer(iris, -Species), aes(Species, value, fill = Species)) + geom_violin() + facet_wrap(~name, scales = "free_y")
ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_violin() + scale_fill_manual(values = c("#FF9999", "#99FF99", "#9999FF"))
ggplot(iris, aes(Species, Sepal.Length)) + geom_violin(fill = "skyblue", alpha = 0.3)
ggplot(iris, aes(Species, Sepal.Length)) + geom_violin(color = "darkblue", lwd = 1, linetype = "dashed")
ggplot(iris, aes(Species, Sepal.Length)) + geom_violin(width = 0.8) + geom_boxplot(width = 0.1, fill = "white", outlier.shape = NA)
ggplot(iris, aes(Species, Sepal.Length)) + geom_violin() + geom_jitter(width = 0.1, alpha = 0.3)
ggplot(iris, aes(Species, Sepal.Length)) + geom_violin() + stat_summary(fun = mean, geom = "crossbar", width = 0.2, color = "red")
ggplot(iris, aes(Species, Sepal.Length^3)) + # 对偏态数据做变换 geom_violin() + scale_y_continuous(labels = function(x) round(x^(1/3), 1))
# 创建分组数据示例 set.seed(123) df <- data.frame( group = rep(c("A","B"), each = 100), value = c(rnorm(100, mean = 5), rnorm(100, mean = 7)) ) ggplot(df, aes(group, value, fill = group)) + geom_violin(position = position_dodge(0.8), alpha = 0.7)
使用plotly创建可交互版本:
library(plotly) p <- ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_violin() ggplotly(p)
# 使用trim参数控制尾部 ggplot(iris, aes(Species, Sepal.Length)) + geom_violin(trim = FALSE) # 保留完整密度曲线
iris_na <- iris iris_na$Sepal.Length[c(3,10)] <- NA ggplot(na.omit(iris_na), aes(Species, Sepal.Length)) + geom_violin()
# 使用抽样方法 set.seed(123) ggplot(dplyr::sample_n(iris, 50), aes(Species, Sepal.Length)) + geom_violin()
# 模拟表达数据 set.seed(456) exp_data <- data.frame( gene = rep(paste0("Gene",1:5), each = 30), expression = c(rnorm(30,10,2), rnorm(30,8,3), rnorm(30,12,1), rnorm(30,9,2), rnorm(30,11,1.5)) ) ggplot(exp_data, aes(gene, expression)) + geom_violin(fill = "lightgreen") + labs(title = "基因表达分布比较", y = "Expression Level")
# 模拟用户停留时间 user_data <- data.frame( group = rep(c("Android","iOS"), each = 500), time = c(rgamma(500, shape = 2, rate = 0.5), rgamma(500, shape = 3, rate = 0.7)) ) ggplot(user_data, aes(group, time)) + geom_violin(aes(fill = group), alpha = 0.6) + scale_y_log10() + # 对数转换处理长尾分布 labs(title = "不同平台用户停留时间分布")
小提琴图作为数据分布可视化的强大工具,通过R语言的ggplot2等包可以轻松实现。本文从基础绘制到高级定制,涵盖了颜色调整、统计量添加、特殊数据处理等实用技巧。实际应用中,建议根据数据特点选择合适的参数配置,并考虑与其他图表类型的组合使用,以更全面地展现数据特征。
提示:所有代码示例均已在R 4.2.0环境下测试通过,建议读者复制到RStudio中实际运行观察效果。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。