sm_slope: Slope Chart

View source: R/sm_slope.R

sm_slopeR Documentation

Slope Chart

Description

Generates a slope chart, which is particularly useful for comparing effects between two time points. The function supports grouped data and provides various customization options for lines, points, error bars, and x-axis ticks. Users can specify the type of error bars and control whether to display mean and error bars.

The mapping in 'ggplot()' requires grouping each observation to ensure correct pairing of points.

Usage

sm_slope(
  ...,
  labels,
  group,
  line.params = list(color = "gray53", linewidth = 0.4, alpha = 0.4),
  point.params = list(size = 2.5, shape = 21, color = "white"),
  avgLine.params = list(linewidth = 1),
  avgPoint.params = list(size = 4),
  err.params = list(linewidth = 1),
  xTick.params = list(position = "top", expand = c(0.17, 0.1), drop = FALSE),
  errorbar_type = "sd",
  many_groups = FALSE,
  show_err = FALSE,
  show_mean = FALSE,
  legends = FALSE,
  forget = FALSE
)

Arguments

...

Additional aesthetic parameters applied across points and lines, such as color, alpha, and fill. Optional.

labels

A vector specifying the labels for the x-axis ticks. This is a required argument. For example: c('Day 1', 'Day 2').

group

The name of the variable used to group individual data points. This is a required argument.

line.params

A list of parameters for individual lines. Common parameters include:

  • color: Color of the lines.

  • alpha: Transparency of the lines.

  • linewidth: Width of the lines.

Default: list(color = 'gray53', linewidth = 0.4, alpha = 0.4).

point.params

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

  • size: Size of the points.

  • shape: Shape of the points.

  • color: Color of the points.

Default: list(size = 2.5, shape = 21, color = 'white').

avgLine.params

A list of parameters for the average line. Common parameters include:

  • color: Color of the average line.

  • linewidth: Width of the average line.

Default: list(linewidth = 1).

avgPoint.params

A list of parameters for the average points. Common parameters include:

  • size: Size of the average points.

  • fill: Fill color of the average points.

Default: list(size = 4).

err.params

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

  • color: Color of the error bars.

  • linewidth: Width of the error bars.

Default: list(linewidth = 1).

xTick.params

A list of parameters for customizing the x-axis ticks. Common options include:

  • position: Location of the ticks (default: 'top').

  • expand: Space around the ticks (default: c(0.17, 0.1)).

  • drop: Whether to drop unused factor levels (default: FALSE).

errorbar_type

A string specifying the type of error bars to display:

  • 'se': Standard error.

  • 'sd': Standard deviation (default).

  • 'ci': 95

many_groups

Logical. Determines whether the average line is plotted for each group:

  • TRUE: An average line is plotted for each group.

  • FALSE: A single average line is plotted across all data.

Default: FALSE.

show_err

Logical. Determines whether to display error bars:

  • TRUE: Display error bars.

  • FALSE: Hide error bars (default).

show_mean

Logical. Determines whether to display the average line and points:

  • TRUE: Display the average line and points.

  • FALSE: Hide the average line and points (default).

legends

Logical. Determines whether to display legends:

  • TRUE: Display legends.

  • FALSE: Hide legends (default).

forget

Logical. Determines whether to apply the default aesthetic parameters:

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

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

Value

A list of ggplot2 layers for creating a slope chart.

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)

ggplot(data=df, aes(x = Day, y = Value, fill = Day)) +
 sm_slope(labels = c('Day 1', 'Day 2'), group = Subject) +
 scale_fill_manual(values=  sm_color('blue','orange'))
ggplot(data = df, aes(x = Day, y = Value, fill = Day)) +
 sm_slope(labels = c('Day 1','Day 2'),group = Subject,
          point.params = list(alpha = 0.3, size = 2.5, color = 'white',
                              shape = 21, fill = sm_color('skyblue')),
          line.params = list(color = sm_color('skyblue'),
                             alpha = 0.3),
          avgPoint.params = list(color='transparent', shape = 21,
                                 size = 4, fill = sm_color('blue')),
          avgLine.params = list(color = sm_color('blue'), linewidth = 1),
          show_mean = TRUE)



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