sm_raincloud: Raincloud Plot

View source: R/sm_raincloud.R

sm_raincloudR Documentation

Raincloud Plot

Description

Creates a raincloud plot, a combination of jittered points, boxplots, and violin plots. Inspired by the 'raincloudplots' R package by Jordy van Langen, this function offers enhanced customization and automatic sorting of data based on the x-axis factor levels.

This function allows detailed control over the aesthetics of individual components (boxplot, violin, and points) and provides options to adjust layout and orientation.

Usage

sm_raincloud(
  ...,
  boxplot.params = list(),
  violin.params = list(alpha = 0.3, color = "transparent"),
  point.params = list(alpha = 1, size = 3, shape = 21, color = "transparent"),
  which_side = "r",
  sep_level = 2,
  point_jitter_width = 0.12,
  vertical = TRUE,
  points = TRUE,
  borders = TRUE,
  legends = FALSE,
  seed = NULL,
  forget = FALSE
)

Arguments

...

Additional aesthetic parameters applied across all elements (points, boxplot, and violin plot). Optional.

boxplot.params

A list of parameters for customizing the boxplot. Common options include:

  • fill: Fill color of the boxplot.

  • color: Outline color of the boxplot.

  • alpha: Transparency level of the boxplot.

  • width: Width of the boxplot.

Default: list().

violin.params

A list of parameters for customizing the violin plot. Common options include:

  • alpha: Transparency level of the violin plot.

  • color: Outline color of the violin plot.

  • fill: Fill color of the violin plot.

Default: list(alpha = 0.3, color = 'transparent').

point.params

A list of parameters for customizing individual points. Common options include:

  • alpha: Transparency level of the points.

  • size: Size of the points.

  • shape: Shape of the points.

  • color: Outline color of the points.

Default: list(alpha = 1, size = 3, shape = 21, color = 'transparent').

which_side

Specifies the side of the violin and boxplots. Options:

  • 'right': Displays elements on the right side (default).

  • 'left': Displays elements on the left side.

The 'mixed' option has been removed due to limited usage.

sep_level

A numeric value (0-4) controlling the separation level among the boxplot, violin plot, and points:

  • 0: All elements are clustered together.

  • 4: All elements are maximally separated.

  • Intermediate values (1-3) provide varying levels of separation.

Default: 2.

point_jitter_width

A numeric value determining the horizontal jitter for individual points:

  • If set to 0, points are aligned along the y-axis without jitter.

  • Default: 0.12.

vertical

Logical. Specifies the orientation of the plot:

  • TRUE: Vertical orientation (default).

  • FALSE: Horizontal orientation.

points

Logical. Determines whether individual points are displayed:

  • TRUE: Display points (default).

  • FALSE: Hide points.

borders

Logical. Determines whether grid borders are displayed:

  • TRUE: Display borders (default).

  • FALSE: Remove borders.

legends

Logical. Determines whether legends are displayed:

  • TRUE: Display legends.

  • FALSE: Hide legends (default).

seed

A numeric value to set a random seed for reproducible jittered points. Default: NULL (no seed).

forget

Logical. Determines whether to apply the default aesthetic parameters:

  • TRUE: Ignore default aesthetic parameters (boxplot.params, violin.params, and point.params) and apply only user-supplied customizations.

  • FALSE: Merge user-supplied customizations with the defaults (default).

Value

A list of ggplot2 layers for creating a raincloud plot.

Examples

library(ggplot2)
library(smplot2)

set.seed(2) # generate random data
day1 = rnorm(20,0,1)
day2 = rnorm(20,5,1)
day3 = rnorm(20,6,1.5)
day4 = rnorm(20,7,2)
Subject <- rep(paste0('S',seq(1:20)), 4)
Data <- data.frame(Value = matrix(c(day1,day2,day3,day4),ncol=1))
Day <- rep(c('Day 1', 'Day 2', 'Day 3', 'Day 4'), each = length(day1))
df2 <- cbind(Subject, Data, Day)

ggplot(data=df2, aes(x = Day, y = Value, color = Day, fill = Day)) +
sm_raincloud() +
 xlab('Day')  +
 scale_fill_manual(values = sm_palette(4))



smin95/sesR documentation built on Feb. 3, 2025, 10:51 p.m.