R ggplot2 小提琴图用于按特定数据分组的数值数据的图形可视化。让我们看看如何创建 ggplot2 小提琴图并设置其颜色。并绘制水平小提琴图,使用 ggplot2 绘制多个小提琴图并附带示例。在此 R ggplot 小提琴图演示中,我们使用 Rstudio 提供的 diamonds 数据集。
在此编程中使用 ggplot2 包绘制 ggplot 小提琴图的语法如下所示。
geom_violin(mapping = NULL, data = NULL, stat = "ydensity", position = "dodge", ..., draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
创建基本 R ggplot2 小提琴图
此示例演示了如何使用 ggplot2 包创建基本的小提琴图。在此示例中,我们将使用 R Studio 提供的 diamond 数据集。
提示:ggplot2 包默认未安装。请参阅安装 R 包文章以了解如何安装该包。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price)) + geom_violin()

创建 R ggplot 小提琴图
在我们之前的 R ggplot2 ggplot 示例中,数据量很大,因此无法很好地显示小提琴图。在此示例中,我们将 y 值进行 log10 缩放并使用缩放后的 y 值创建此图表。
注意:如果您需要从外部文件导入数据,请参阅读取 CSV 以了解如何在R 编程中导入 CSV 文件。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price)) + geom_violin() + scale_y_log10()
更改小提琴图的颜色
在此示例中,我们展示了如何使用 fill 参数更改颜色
- fill:请指定您想要使用的颜色。在控制台中键入 colors() 可获取 R 编程中可用颜色的列表
# Change Colors library(ggplot2) ggplot(diamonds, aes(x = cut, y = price)) + geom_violin(fill = "seagreen") + scale_y_log10()
使用列更改颜色
此示例,我们展示了如何使用列数据更改小提琴图颜色。这里,我们使用 cut 列数据来区分颜色。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + geom_violin() + scale_y_log10()

使用列裁剪 R 小提琴图
让我们看看如何使用 trim 参数来裁剪或不裁剪数据
# Trimming library(ggplot2) ggplot(diamonds, aes(x = clarity, y = price, fill = clarity)) + geom_violin(trim = FALSE) + scale_y_log10())

添加均值和中位数
让我们看看如何使用 stat_summary 函数将均值和中位数添加到 ggplot2 小提琴图中。
# Adding Mean & Median library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + geom_violin() + scale_y_log10() + stat_summary(fun.y = "mean", geom = "point", shape = 8, size = 3, color = "midnightblue") + stat_summary(fun.y = "median", geom = "point", shape = 2, size = 3, color = "red")

向 R ggplot2 小提琴图添加箱线图
在此示例中,我们展示了如何使用 geom_boxplot 函数向 R 小提琴图添加箱线图。它可以帮助我们查看图表的中位数和四分位数。
提示:请参阅箱线图文章以了解箱线图参数。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + geom_violin() + scale_y_log10() + geom_boxplot(width = 0.2)
修改 R ggplot2 小提琴图的图例位置
默认情况下,ggplot 将图例放置在右侧。在此示例中,我们将 R ggplot 小提琴图的图例位置从右侧更改为顶部。注意,您可以使用 legend.position = “none” 来完全移除图例。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + geom_violin() + scale_y_log10() + geom_boxplot(width = 0.2) + theme(legend.position = "top")

水平小提琴图
在此 ggplot 示例中,我们使用 coord_flip() 函数将默认的垂直图转换为水平图。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + geom_violin() + scale_y_log10() + geom_boxplot(width = 0.2) + coord_flip()

R 中的多个 ggplot 小提琴图
在此示例中,我们展示了如何对多个小提琴图进行分组。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + geom_violin() + scale_y_log10()

如何在 R ggplot2 小提琴图中使用分面
此示例,我们通过按列值划分数据来绘制多个小提琴图。这里,我们使用 clarity 列数据来划分它们。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + geom_violin(trim= FALSE) + scale_y_log10() + facet_wrap(~ clarity)
默认情况下,facet_wrap() 会为主轴分配相同的 y 轴。但是,您可以通过添加另一个名为 scale 的属性来为每个轴提供独立轴。facet_wrap(~ clarity, scale = “free”)。
分配名称
在此示例中,我们展示了如何使用 labs 函数为图表、X 轴和 Y 轴指定名称
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + geom_violin(trim= FALSE) + scale_y_log10() + facet_wrap(~ clarity) + labs(title="GGPLOT VIOLIN PLOT", x="Price in Dollars", y="Cut")

更改主题
在此示例中,我们展示了如何更改 R ggplot 小提琴图的默认主题。
- theme_dark(): 我们使用此函数将默认主题更改为深色。如果您键入 theme_, then R Studio 智能提示将显示可用选项列表。例如,theme_grey()。
library(ggplot2) ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + geom_violin(trim = FALSE) + scale_y_log10() + geom_boxplot(width = 0.2) + labs(title="GGPLOT VIOLIN PLOT", x="Price in Dollars", y="Cut") + theme_dark()
