sm_violin: Violin Plot with Jittered Individual Points

View source: R/sm_violin.R

sm_violinR Documentation

Violin Plot with Jittered Individual Points

Description

Generates a violin plot with optional jittered individual points and error bars. This function allows for flexible customization of the violin, points, and error bars, and supports displaying standard deviation, standard error, or confidence intervals as error bars.

Usage

sm_violin(
  ...,
  violin.params = list(fill = "gray90", color = "transparent"),
  err.params = list(size = 1.2, linewidth = 1.2),
  point.params = list(alpha = 0.25, size = 2),
  errorbar_type = "sd",
  point_jitter_width = 0.17,
  points = TRUE,
  borders = TRUE,
  legends = FALSE,
  seed = NULL,
  forget = FALSE
)

Arguments

...

Additional aesthetic parameters applied across points, the violin plot, and error bars. Optional.

violin.params

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

  • fill: Fill color of the violin plot.

  • color: Outline color of the violin plot.

  • alpha: Transparency level of the violin plot.

Default: list(fill = 'gray90', color = 'transparent').

err.params

A list of parameters for customizing the error bars. Common parameters include:

  • size: Size of the error bar endpoints.

  • linewidth: Width of the error bar lines.

  • color: Color of the error bars.

Default: list(size = 1.2, linewidth = 1.2).

point.params

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

  • alpha: Transparency level of the points.

  • size: Size of the points.

  • shape: Shape of the points.

Default: list(alpha = 0.25, size = 2).

errorbar_type

A string specifying the type of error bars to display:

  • 'se': Standard error.

  • 'sd': Standard deviation (default).

  • 'ci': 95

point_jitter_width

A numeric value specifying the degree of horizontal jitter applied to individual points.

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

  • Default: 0.17.

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 (violin.params, err.params, and point.params) and apply only user-supplied customizations.

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

Value

A ggplot2 object containing a violin plot with optional jittered points and error bars.

Examples

library(ggplot2)
library(smplot2)
set.seed(1) # generate random data
day1 = rnorm(16,2,1)
day2 = rnorm(16,5,1)
Subject <- rep(paste0('S',seq(1:16)), 2)
Data <- data.frame(Value = matrix(c(day1,day2),ncol=1))
Day <- rep(c('Day 1', 'Day 2'), each = length(day1))
df <- cbind(Subject, Data, Day)
# with aesthetic defaults of smplot
ggplot(data = df, mapping = aes(x = Day, y = Value, color = Day)) +
sm_violin() +
scale_color_manual(values = sm_color('blue','orange'))

# without aesthetic defaults of smplot
ggplot(data = df, mapping = aes(x = Day, y = Value, color = Day)) +
sm_violin(violin.params = list()) +
scale_color_manual(values = sm_color('blue','orange'))



smin95/sesplot documentation built on Feb. 3, 2025, 10:52 p.m.