Package 'scplot'

Title: Plot Function for Single-Case Data Frames
Description: Add-on for the 'scan' package that creates plots from single-case data frames ('scdf'). It includes functions for styling single-case plots, adding phase-based lines to indicate various statistical parameters, and predefined themes for presentations and publications. More information and in depth examples can be found in the online book "Analyzing Single-Case Data with R and 'scan" Jürgen Wilbert (2023) <https://jazznbass.github.io/scan-Book/>.
Authors: Juergen Wilbert [aut, cre]
Maintainer: Juergen Wilbert <[email protected]>
License: GPL (>= 3)
Version: 0.4.0
Built: 2024-09-28 07:17:44 UTC
Source: CRAN

Help Index


Single-Case Data Plots

Description

A collection of procedures for visualizing single-case data. It is an add-on package for the scan package.

Author(s)

Juergen Wilbert [aut, cre]


Add arrrows to an scplot

Description

Add arrrows to an scplot

Usage

add_arrow(
  object,
  case = 1,
  x0,
  y0,
  x1,
  y1,
  color = "black",
  angle = 30,
  length = unit(5, "points"),
  type = "open",
  ends = "last",
  linewidth = 0.7
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

case

Numerical vector with the case number or character string. case = "all" for all cases.

x0

Origin x position of the line.

y0

Origin y position of the line.

x1

End x position of the line.

y1

End y position of the line.

color

A character string or a number defining the color of an element.

angle

Angle (in [0,360])

length

Size of the arrow angels.

type

One of "open" or "closed" indicating whether the arrow head should be a closed triangle.

ends

One of "last", "first", or "both", indicating which ends of the line to draw arrow heads.

linewidth

A number with the width of the line.

Value

An object of class scplot (seescplot()) with added element lines.

Examples

data(exampleAB, package = "scan")
p1 <- scplot(exampleAB$Anja)  |>
  add_arrow(case = 1, 2, 70, 6, 55, color = "darkred")

Add grid to an scplot

Description

Add grid to an scplot

Usage

add_grid(object, ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

...

Line arguments (see element_line())

Value

An object of class scplot (seescplot()).

See Also

element_line()

Examples

data(exampleAB, package = "scan")
p1 <- scplot(exampleAB$Anja)  |>
  set_theme("minimal")  |>
  add_grid(color = "grey70")

Add value labels to an scplot

Description

Add value labels to an scplot

Usage

add_labels(
  object,
  nudge_y = 5,
  nudge_x = 0,
  round = NULL,
  text = list(),
  background = list(),
  variable = ".dvar",
  padding = NULL
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

nudge_y

Offset on the y-axis.

nudge_x

Offset on the x-axis.

round

Number of digits of the labels.

text

List with text parameters (⁠"family", "face", "colour", "size", "hjust", "vjust", "angle", "lineheight", "margin"⁠). See element_text().

background

A list with background styling arguments (fill, color, size, linetype).

variable

Name of the dataline variable to apply the style.

padding

Padding size around text.

Value

An object of class scplot (seescplot()) with added/changed element labels.


Add a legend to an scplot

Description

Add a legend to an scplot

Usage

add_legend(
  object,
  labels = NULL,
  section_labels = c("Lines", "Phases"),
  case = 1,
  position = "right",
  datalines = TRUE,
  statlines = TRUE,
  phases = TRUE,
  title = NULL,
  text = NULL,
  background = NULL
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

labels

A character vector. Replaces the automatically build labels. When a vector element is NA, the corresponding element will not be replaced (e.g. c(NA, "Variable 2", NA, "Variable 4")).

section_labels

A character vector of length two. The labels for the lines section and phase section.

case

Numerical vector with the case number or character string. case = "all" for all cases.

position

The position ("none", "left", "right", "bottom", "top", or two-element numeric vector)

datalines

If TRUE, a legend for the datalines is generated.

statlines

If TRUE, a legend for the statlines is generated.

phases

If TRUE, a legend for the phases is generated.

title

A list with text style parameters for the title.

text

List with text parameters (⁠"family", "face", "colour", "size", "hjust", "vjust", "angle", "lineheight", "margin"⁠). See element_text().

background

A list with background styling arguments (fill, color, size, linetype).

Value

An object of class scplot (seescplot()) with changed element legend.


Add line to an scplot

Description

Add line to an scplot

Usage

add_line(
  object,
  case = 1,
  x0 = NULL,
  y0 = NULL,
  x1 = NULL,
  y1 = NULL,
  hline = NULL,
  vline = NULL,
  color = "black",
  linewidth = 0.7,
  linetype = "solid"
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

case

Numerical vector with the case number or character string. case = "all" for all cases.

x0

Origin x position of the line.

y0

Origin y position of the line.

x1

End x position of the line.

y1

End y position of the line.

hline

y position of horizontal line.

vline

x position of vertical line.

color

A character string or a number defining the color of an element.

linewidth

A number with the width of the line.

linetype

A character string with the line type: ⁠"solid", "dashed", "dotted"⁠

Value

An object of class scplot (seescplot()) with added element lines.

Examples

data(exampleAB, package = "scan")
p1 <- scplot(exampleAB$Anja)  |>
  add_line(hline = 70, color = "darkred") |>
  add_line(vline = 3, color = "blue") |>
  add_line(x0 = 1, y0 = 70, x1 = 4, y1 = 80, color = "green")

Add marks to an scplot

Description

Add marks to an scplot

Usage

add_marks(
  object,
  case = 1,
  positions,
  color = "red",
  size = 1,
  shape = 1,
  variable = ".dvar"
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

case

Numerical vector with the case number or character string. case = "all" for all cases.

positions

Either a vector indicating the points to be highlighted or a character string with a logical expression (e.g. values < mean(values))

color

A character string or a number defining the color of an element.

size

Text size relative to the base text size.

shape

Number. See pch graphical parameter on par help page par().

variable

Name of the dataline variable to apply the style.

Details

If positions is an object returned from an outlier analysis outlier(), the corresponding outliers are marked.

Value

An object of class scplot (see scplot()) with changed element marks.

Examples

library(scan)
p1 <- scplot(exampleA1B1A2B2$Moritz) |> add_marks(positions = c(1,5,10,14))
p1 <- scplot(Huber2014) |> add_marks(positions = outlier(Huber2014))

Add a ridge to an scplot

Description

Add a ridge to an scplot

Usage

add_ridge(object, color = "grey98", variable = ".dvar")

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

color

A character string or a number defining the color of an element.

variable

Name of the dataline variable to apply the style.

Value

An object of class scplot (seescplot()) with changed element ridges.


Add a statline to an scplot

Description

Add a statline to an scplot

Usage

add_statline(
  object,
  stat = c("mean", "median", "min", "max", "quantile", "sd", "mad", "trend", "trendA",
    "trendA theil-sen", "moving mean", "moving median", "loreg", "lowess", "loess"),
  phase = NULL,
  color = NULL,
  linewidth = NULL,
  linetype = NULL,
  variable = NULL,
  ...
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

stat

A character string for defining a statistical line or curve to be plotted.

phase

Either a numeric or a character vector specifying the reference phase (see details)

color

A character string or a number defining the color of an element.

linewidth

A number with the width of the line.

linetype

A character string with the line type: ⁠"solid", "dashed", "dotted"⁠

variable

Name of the dataline variable to apply the style.

...

additional parameters passed to the statistical function.

Details

The phase argument defines the reference phase for some statistical functions (⁠"median", "mean", "min", "max", "quantile"⁠). The default is NULL which calculates and plots statistics for each phase separately. The arguments takes a numeric vector (phase number(s)) or a character vector (phase name(s)). When more than one phase is defines, statistics are based on the combined values of these phases. Various methods for an extrapolated trendA line exist: "trendA" is based on an OLS regression, "trendA theil-sen" on a nonparametric regression, and "trendA bisplit" / "trendA trisplit" are two median based approaches. Some of the functions defined in stats have additional arguments. The mean() function has a trim argument (e.g. trim = 0.1). quantile() has a proportion argument (e.g. prob = 0.75 for calculating the 75% quantile). ⁠moving mean⁠ and ⁠moving median⁠ have a lag argument (e.g. lag = 2). The local-regression curve function "lowess" (or "loreg") has a proportion argument (e.g. f = 0.5; see lowess()) and the local-regression curve function "loess" has a span argument (e.g. span = 0.75; see loess()).

Value

An object of class scplot (seescplot()) with changed element statlines.


Add test to an scplot

Description

Add test to an scplot

Usage

add_text(
  object,
  label,
  case = 1,
  x,
  y,
  color = "black",
  size = 1,
  angle = 0,
  hjust = 0.5,
  vjust = 0.5,
  face = 1
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

label

A Character vector with text labels.

case

Numerical vector with the case number or character string. case = "all" for all cases.

x

x position

y

y position

color

A character string or a number defining the color of an element.

size

Text size relative to the base text size.

angle

Angle (in [0,360])

hjust

Horizontal justification (in [0,1])

vjust

Vertical justification (in [0,1])

face

Font face (⁠"plain", "italic", "bold", "bold.italic"⁠)

Value

An object of class scplot (seescplot()) with a changed texts element.


Add title and caption to an scplot

Description

Add title and caption to an scplot

Usage

add_title(object, label, ...)

add_caption(object, label, header = "Note:\n", ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

label

A Character vector with text labels.

...

List with text parameters (⁠"family", "face", "colour", "size", "hjust", "vjust", "angle", "lineheight", "margin"⁠). See element_text().

header

String with header above footnote/ caption

Value

An object of class scplot (seescplot()) with changed title and caption elements.


Creates a ggplot2 object from an scplot() object

Description

Creates a ggplot2 object from an scplot() object

Usage

as_ggplot(scplot)

Arguments

scplot

An scplot object

Details

as_ggplot() is used when you want to return a ggplot2 object for further use with external ggplot functions.

Value

A ggplot2 plot object.


Point element

Description

In conjunction with ggplot an object to represent point attributes.

Usage

element_point(colour = NULL, size = NULL, shape = NULL, color = NULL)

Arguments

size

Relative size.

shape

Point shape.

color, colour

Point colour.

Value

An object of class c("element_point", "element").


Create a new scplot theme

Description

Create a new scplot theme

Usage

new_theme()

extract_theme(object)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

Value

An scplot-theme object

An object of class scplot-theme which can be used with the set_theme() function.

Examples

data(exampleABC, package = "scan")
my_theme <- new_theme() |>
  set_panel(color = "red")  |>
  set_base_text(size = 12, color = "blue")  |>
  set_dataline(color = "darkred", linewidth = 2)
p1 <- scplot(exampleABC)  |> set_theme(my_theme)

This function generates a forest plot for the random effects of a mixed hplm model

Description

This function generates a forest plot for the random effects of a mixed hplm model

Usage

## S3 method for class 'sc_hplm'
plot(x, effect = "intercept", mark = "mean", ci = 0.95, ...)

Arguments

x

The return from the hplm() function.

effect

The specific effect to be plotted (default is the intercept).

mark

Set a reference line.

ci

Value between 0 and 1 for calculating the confidence interval.

...

Further arguments.

Value

A forest plot displaying Tau-U effects.

Examples

# plot(hplm(Leidig2018), effect = "intercept")

Plot Tau-U Effects

Description

This function generates a forest plot of Tau-U effects.

Usage

## S3 method for class 'sc_tauu'
plot(x, effect = "A vs. B - Trend A", ...)

Arguments

x

The return from the tau_u() function.

effect

The specific effect to be plotted (default is "A vs. B - Trend A").

...

Further arguments.

Value

A forest plot displaying Tau-U effects.

Examples

# plot(tau_u(Leidig2018), effect = "A vs. B - Trend A")

Plot single-case data

Description

This function provides a plot of a single-case or multiple single-cases.

Usage

scplot(scdf)

Arguments

scdf

A single-case data-frame object (scdf).

Value

An object of class scplot containing the single-case data (element scdf), and information about the plot style (element theme).

Author(s)

Juergen Wilbert


Set plot and panel background of an scplot

Description

Set plot and panel background of an scplot

Usage

set_background(object, ...)

set_panel(object, ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

...

List with rectangle parameters (⁠"fill", "colour", "linewidth", "linetype"⁠). See element_rect().

Value

An object of class scplot (seescplot()).

Examples

data(exampleAB, package = "scan")
p1 <- scplot(exampleAB)  |>
 set_background(fill = "lightblue", colour = "darkblue", linewidth = 1.5) |>
 set_panel(fill = "deepskyblue", color = "darkblue", linewidth = 0.3)

Set base text parameters of an scplot

Description

Set base text parameters of an scplot

Usage

set_base_text(object, ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

...

List with text parameters (⁠"family", "face", "colour", "size", "hjust", "vjust", "angle", "lineheight", "margin"⁠). See element_text().

Value

An object of class scplot (seescplot()).


Set casenames of an scplot

Description

Set casenames of an scplot

Usage

set_casenames(object, labels = NULL, position = NULL, background = list(), ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

labels

A Character vector with text labels.

position

Either ⁠"topleft", "bottomleft", "topright", "bottomright", "strip-right", "strip-top"⁠, or a numerical vector of length 2 with the x and y position (e.g. c(19, 20)).

background

A list with background styling arguments (fill, color, size, linetype).

...

List with text parameters (⁠"family", "face", "colour", "size", "hjust", "vjust", "angle", "lineheight", "margin"⁠). See element_text().

Value

An object of class scplot (seescplot()) with a changed casenames element.


Set data lines of an scplot

Description

Either set aesthetics of the default data line or add another data line.

Usage

set_dataline(object, variable = NULL, line, point, type = "continuous", ...)

add_dataline(...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

variable

String. The name of a new variable for adding a new line. If left empty, the aesthetics of the default data line are changed.

line

List with line parameters (⁠"colour", "linewidth", "linetype", "lineend", "arrow"⁠). See element_line().

point

A list with point parameters (⁠"colour", "size", "shape"⁠). See element_point().

type

Either "continuous" or "discrete"

...

As a shortcut, arguments passed hear are bundled as line arguments (see element_line()).

Value

An object of class scplot (seescplot()) with a changed datalines element.

See Also

element_line(), element_point()

Examples

data(exampleAB_add, package = "scan")
p1 <- scplot(exampleAB_add)  |>
  set_dataline("depression", color = "darkblue")

Set phasenames of an scplot

Description

Set phasenames of an scplot

Usage

set_phasenames(object, labels = NULL, position = NULL, ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

labels

A Character vector with text labels.

position

Character string either 'left', 'center', or 'none'.

...

List with text parameters (⁠"family", "face", "colour", "size", "hjust", "vjust", "angle", "lineheight", "margin"⁠). See element_text().

Value

An object of class scplot (see scplot()) with a changed phasenames element.


Set separator line in an scplot

Description

Set separator line in an scplot

Usage

set_separator(object, ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

...

List with line parameters (⁠"colour"", "linewidth", "linetype"⁠).

Value

An object of class scplot (seescplot()).


Add a theme of to an scplot

Description

Possible themes are: ⁠'basic', 'grid', 'default', 'small', 'tiny', 'big', 'minimal', 'dark', 'sienna', 'phase_color', 'phase_shade', 'grid2'⁠.

Usage

set_theme(object, theme, ...)

add_theme(...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

theme

A character string with a predefined graphical theme or a theme object created with new_theme().

...

Further character strings or scplot-theme objects that are "added" on top.

Value

An object of class scplot (seescplot()) with a changed theme element.


Set a theme element

Description

Set a theme element

Usage

set_theme_element(object, ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

...

various style parameter

Details

Usually, you don't need this function. Possible theme elements are: "text", "plot.background", "panel.background", "panel.spacing.y", "dataline", "datapoint", "statline", "axis.expand.x", "axis.expand.y", "axis.line.x", "axis.line.y", "axis.ticks.length", "axis.ticks", "axis.title.y", "axis.title.x", "axis.text.x", "axis.text.y", "plot.title", "plot.caption", "plot.margin", "casenames", "casenames.strip", "casenames.background", "casenames.position", "phasenames", "phasenames.position.x", "separators", "separators.extent", "label.text", "label.background", "label.padding", "grid", "legend.position", "legend.background", "legend.text", "legend.title", "legend.margin".

The elements are of the following classes:

  • text = c("element_text", "element"),

  • plot.background = c("element_rect", "element"),

  • panel.spacing.y = c("simpleUnit", "unit", "unit_v2"),

  • dataline = "list",

  • datapoint = "list",

  • statline = c("element_line", "element" ),

  • axis.expand.x = "numeric",

  • axis.expand.y = "numeric",

  • axis.line.x = c("element_line", "element"),

  • axis.line.y = c("element_line", "element"),

  • axis.ticks.length = c("simpleUnit", "unit", "unit_v2"),

  • axis.ticks = c("element_line", "element"),

  • axis.title.y = c("element_text", "element"),

  • axis.title.x = c("element_text", "element"),

  • axis.text.x = c("element_text", "element"),

  • axis.text.y = c("element_text", "element"),

  • plot.title = c("element_text", "element"),

  • plot.caption = c("element_text", "element"),

  • plot.margin = c("margin", "simpleUnit", "unit","unit_v2"),

  • casenames = c("element_text", "element"),

  • casenames.strip = c("element_rect", "element"),

  • casenames.background = c("element_rect", "element"),

  • casenames.position = "character",

  • phasenames = c("element_text", "element"),

  • phasenames.position.x = "character",

  • separators = c("element_line", "element"),

  • separators.extent = "character",

  • label.text = c("element_text", "element"),

  • label.background = c("element_rect", "element" ),

  • label.padding = "numeric", grid = c("element_line", "element" ),

  • legend.position = "character",

  • legend.background = c("element_rect", "element"),

  • legend.text = c("element_text", "element"),

  • legend.title = c("element_text", "element"),

  • legend.margin = c("margin", "simpleUnit", "unit", "unit_v2")

Value

An object of class scplot (seescplot()) with a changed theme element.

Examples

data(exampleABC, package = "scan")
p1 <- scplot(exampleABC)  |>
  set_theme_element(
    axis.ticks.length = unit(0, "points"),
    axis.line.y = element_line(color = "darkred", linewidth = 2),
    panel.background = element_rect(color = "darkblue", linewidth = 1),
    panel.spacing.y = unit(0, "points"),
    phasenames = element_text(color = "#00000000")
  )

Set axis parameters of an scplot

Description

Set axis parameters of an scplot

Usage

set_xaxis(
  object,
  limits = NULL,
  increment = NULL,
  increment_from = NULL,
  line = NULL,
  expand = NULL,
  ...
)

set_yaxis(
  object,
  limits = NULL,
  increment = NULL,
  increment_from = NULL,
  line = NULL,
  expand = NULL,
  ...
)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

limits

Lower and upper limits of the axis (e.g., limits = c(0, 20) sets the axis to a scale from 0 to 20). With multiple single-cases you can use limits = c(0, NA) to scale the axis from 0 to the maximum of each case. limits is not set by default, which makes scplot set a proper scale based on the given data.

increment

An integer. Increment of the x-axis. 1 :each mt value will be printed, 2 : every other value, 3 : every third values etc.

increment_from

Number from which increment starts to count. Usually set to 0 if you want marks like 1,5,10,15,...

line

List with line parameters (⁠"colour", "linewidth", "linetype", "lineend", "arrow"⁠). See element_line().

expand

Vector with two values.

...

Further styling arguments: color, size, face, family, hjust, vjust, lineheight, angle, linetype, lineend, arrow, fill, margin.

Value

An object of class scplot (seescplot()) with changed xaxis and yaxis elements.


Set label for axis

Description

Set label for axis

Usage

set_xlabel(object, label = NULL, ...)

set_ylabel(object, label = NULL, ...)

Arguments

object

An scplot object (class scplot) returned from the scplot() function.

label

A Character vector with text labels.

...

Further styling arguments: color, size, face, family, hjust, vjust, lineheight, angle, linetype, lineend, arrow, fill, margin.

Value

An object of class scplot (seescplot()) with a changed xlabel or ylabel element.