R语言 使用带有抖动数据点的半小提琴图绘制雨云图
在这篇文章中,我们将讨论如何在R语言中使用半维林图创建雨云图,并使用抖动的数据点。
雨云图或半小提琴图基本上是用来同时显示数据的分布和整体概要的。这种图是由上面有抖动的点的半小提琴图、boxplots组合而成,并且可以通过添加中心趋势测量、四分位数范围等来进一步加强。
使用的数据集
这里让我们使用 “虹膜 “数据集。这是在R中内置的数据集。
# Loading the packages
library(ggplot2)
library(plyr)
# load the iris data
df <- iris
head(df)
输出
使用ggplot绘制雨云图
例1: 简单的雨云图
让我们为鸢尾花数据集的物种与萼片长度绘制一个雨云/半小提琴图。
# Loading the packages
library("ggplot2")
library("plyr")
# load the iris data
df <- iris
# Code to plot a raincloud
ggplot(df, aes(Species, Sepal.Length, fill=Species)) +
geom_flat_violin() +
coord_flip() +
theme(legend.position = "none")
输出
例2: 顶部有抖动数据点的雨云图
让我们看看如何使用geom_jitter( )函数在雨云图的顶部添加抖动的数据点。
# Loading the packages
library("ggplot2")
library("plyr")
# load the iris data
df <- iris
# Code for Raincloud plot with jittered data points
ggplot(df, aes(Species, Sepal.Length, fill=Species)) +
geom_flat_violin() +
coord_flip() + geom_jitter(alpha = 0.5,width = 0.15)+
theme(legend.position = "none")
输出 :
例3: 雨云图的定位
让我们看看如何调整半小提琴图相对于抖动的数据点的位置。
# Loading the packages
library("ggplot2")
library("plyr")
# load the iris data
df < - iris
# Code for positioning the raincloud plot
ggplot(df, aes(Species, Sepal.Length, fill=Species)) +
geom_flat_violin(position=position_nudge(x=.2, y=0)) +
coord_flip() + geom_jitter(alpha=0.5, width=0.15, aes(color=Species)) +
theme(legend.position="none")
输出:
例四 :雨云图与膨胀图
让我们看看如何在半小提琴图的旁边绘制一个Boxplot,以获得更多的特征洞察力。
# Loading the packages
library("ggplot2")
library("plyr")
# load the iris data
df < - iris
# Code to plot Raincloud and boxplot
ggplot(df, aes(Species, Sepal.Length, fill=Species)) +
geom_flat_violin(position = position_nudge(x = .2, y = 0)) +
coord_flip() + geom_jitter(alpha = 0.5,width = 0.15) +
theme(legend.position = "none") +
geom_boxplot( width = .25, outlier.shape = NA )
输出: