sm_slope_mean: Slope Chart with Mean for a Single Group

View source: R/sm_slope_mean.R

sm_slope_meanR Documentation

Slope Chart with Mean for a Single Group

Description

Generates a slope chart with mean for a single group. This is useful for comparing the effect between two time points. The function includes options for shadow lines and points, mean lines, points, and error bars, all of which can be customized.

Note: This functionality can also be reproduced using 'sm_slope()' with appropriate customization. In 'ggplot()', the mapping requires grouping each observation to correctly pair points.

Usage

sm_slope_mean(
  ...,
  labels,
  group,
  main_color = sm_color("blue"),
  main_shape = 21,
  back_alpha = 0.25,
  line_width = 0.25,
  avgline_width = 1,
  point_size = 2.5,
  avgpoint_size = 4,
  err_width = 1,
  xTick.params = list(position = "top", expand = c(0.17, 0.1), drop = FALSE),
  errorbar_type = "sd",
  show_err = FALSE,
  legends = FALSE
)

Arguments

...

Additional aesthetic parameters applied across points, lines, and error bars. 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.

main_color

The main color of the slope chart, shared across:

  • Points

  • Lines

  • Error bars

Default: sm_color('blue').

main_shape

The shape of the points in the slope chart:

  • Shapes above 20 use color = 'white' and a fill color matching main_color.

Default: 21.

back_alpha

Transparency (alpha) for the shadow lines and points:

  • Lines: controlled by back_alpha.

  • Points: controlled by back_alpha * 0.65.

Default: 0.25.

line_width

Line width for the shadow lines connecting points. Default: 0.25.

avgline_width

Line width for the average line. Default: 1.

point_size

Size of the points in the shadow. Default: 2.5.

avgpoint_size

Size of the points representing the mean of the data. Default: 4.

err_width

Line width for the error bars. Default: 1.

xTick.params

A list of parameters for customizing the x-axis ticks. 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

show_err

Logical. Determines whether to display error bars:

  • TRUE: Display error bars.

  • FALSE: Hide error bars (default).

legends

Logical. Determines whether to display legends:

  • TRUE: Display legends.

  • FALSE: Hide legends (default).

Value

A slope chart with mean, represented as a ggplot2 object.

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)) +
 sm_slope_mean(labels = c('Day 1', 'Day 2'), group = Subject, back_alpha = .3,
main_color = sm_color('green'))

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