sm_slope: A slope chart

View source: R/sm_slope.R

sm_slopeR Documentation

A slope chart

Description

This function generates a slope chart. This is very useful for comparing the effect between two time points.

ggplot()'s mapping has to be quite specific: each observation has to be grouped.

Error bar types can be specified (ci, sd, and se).

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

...

List of parameters for individual points and lines across different elements (except for except for xTick.params), such as color, alpha, fill etc.

labels

Labels for the ticks of the x-axis. This is a required argument. It has to be a single vector containing either one or multiple elements. ex: c('Day 1', 'Day 2')

group

Name of the variable by which the individual data should be grouped

line.params

List of parameters for the individual lines, such as color, alpha etc

point.params

List of parameters for the individual points, such as color, alpha, fill etc

avgLine.params

List of parameters for the average line, such as color, alpha etc

avgPoint.params

List of parameters for the average point, such as color, alpha, fill etc

err.params

List of parameters for the error bar from the average plot, such as color, alpha etc

xTick.params

List of parameters for the x tick from the average plot, such as color, alpha etc

errorbar_type

This argument determines the errorbar type. If it is set to 'se', standard error bar will be shown. If it is set to 'sd' (default), the error bar will display standard deviation. If it is set to 'ci', the error bar will display 95% confidence interval.

many_groups

This argument determines whether the average line can be plotted for each group when multiple groups are plotted at once. If the average line needs to be plotted across all data presented, set this as FALSE. If there are many groups that are presented and that each average line has to be plotted, then set this as TRUE.

show_err

If the error bar needs to be displayed, the input should be TRUE. If the error bar is not needed, the input should be FALSE.

show_mean

If the average plot needs to be displayed, the input should be TRUE. If the average plot is not needed, the input should be FALSE.

legends

If the legend needs to be displayed, the input should be TRUE. If the legend is not needed, the input should be FALSE.

forget

Forget the defaults when list() is called for a specific parameter (ex. point.params). Set to TRUE when when users want to map aesthetics to different groups more flexibly.. Set to FALSE by default.

Value

Returns a slope chart which is 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, 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/sesplot documentation built on July 2, 2024, 9:35 a.m.