spread_level_plot: spread_level_plot

View source: R/spread_level_plot.R

spread_level_plotR Documentation

spread_level_plot

Description

Function produces a scatter plot of median versus spread across a variable's factor levels.

The spread is defined as the difference between the 75th and 25th quartiles. Function returns a named list with a data.table of each level's median, quartile values, and a ggplot2 scatter plot with medians along the x axis and spreads along the y axis.

Usage

spread_level_plot(
  df,
  meas_var,
  factor_var,
  plot_line_fit = TRUE,
  plot_log10 = TRUE,
  title = NULL,
  subtitle = NULL,
  caption = NULL,
  center_titles = FALSE,
  x_title = "Median",
  y_title = "Spread",
  x_limits = NULL,
  x_major_breaks = waiver(),
  y_limits = NULL,
  y_major_breaks = waiver(),
  label_pts = TRUE
)

Arguments

df

The required target data frame with a measure variable and a factor variable with multiple levels.

meas_var

A required string that names the measure variable from 'df'.

factor_var

A required string that names the factor variable from 'df'.

plot_line_fit

A logical which if TRUE plots a line fit between median and spread values.

plot_log10

A logical which if TRUE will plot log10 values of median versus spread instead of raw values.

title

A string that sets the plot title.

subtitle

A string that sets the plot subtitle.

caption

A string that sets the plot caption

center_titles

A logical which if TRUE centers both the 'title' and 'subtitle'.

x_title

A string that sets the x axis title. If NULL then the x axis title does not appear.

y_title

A string that sets the y axis title. If NULL then the y axis title does not appear.

x_limits

A numeric 2 element vector that sets the minimum and maximum for the x axis.

x_major_breaks

A numeric vector or function that defines the exact major tic locations along the x axis.

y_limits

A numeric 2 element vector that sets the minimum and maximum for the y axis.

y_major_breaks

A numeric vector or function that defines the exact major tic locations along the y axis.

label_pts

A logical which if TRUE will label the plot points.

Value

A list object with a data.table and ggplot2 scatter plot.

Examples

library(here)
library(data.table)
library(ggplot2)
library(RplotterPkg)

spread_level_lst <- RplotterPkg::spread_level_plot(
  df = RplotterPkg::homeruns_2000,
  meas_var = "HOMERUNS",
  factor_var = "YEARS",
  x_title = "Log Median",
  y_title = "Log Spread"
)
spread_level_lst$scatter_plot


deandevl/RplotterPkg documentation built on March 1, 2025, 11:17 a.m.