inst/docs/dumbbell.R

## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  echo = TRUE,
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 4
)

## ----load-packages------------------------------------------------------------
suppressPackageStartupMessages({
  library(dumbbell)
  library(ggplot2)
  library(dplyr)
})

## ----create-example-data------------------------------------------------------
set.seed(123)

example_data <- data.frame(
  group = rep(c("Before", "After"), each = 10),
  subject = rep(paste0("Subject ", 1:10), times = 2),
  score = c(
    sample(40:85, 10, replace = TRUE),
    sample(55:100, 10, replace = TRUE)
  ),
  analysis = rep(c("Outcome A", "Outcome B"), times = 10)
)

before_data <- example_data %>%
  filter(group == "Before")

after_data <- example_data %>%
  filter(group == "After")

plot_data <- merge(
  before_data,
  after_data,
  by = c("subject", "analysis"),
  suffixes = c(".before", ".after")
)

plot_data <- plot_data %>%
  mutate(diff = score.after - score.before) %>%
  arrange(diff)

plot_data$subject <- factor(plot_data$subject, levels = plot_data$subject)

## ----basic-plot---------------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After"
)

## ----custom-colors------------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After",
  p_col1 = "red",
  p_col2 = "blue",
  pointsize = 2,
  pt_alpha = 0.8
)

## ----show-values--------------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After",
  pt_val = 1,
  textsize = 3,
  col_lab1 = "red",
  col_lab2 = "blue"
)

## ----show-delta---------------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After",
  delt = 1,
  textsize = 3,
  expandx = 0.10
)

## ----arrows-------------------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After",
  arrow = 1,
  arrow_size = 0.2,
  segsize = 0.7,
  pointsize = 2,
  col_seg1 = "gray60",
  col_seg2 = "gray60"
)

## ----facets-------------------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After"
) +
  facet_wrap(. ~ analysis, ncol = 1, scales = "free_y")

## ----wilcoxon-pvalues---------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After",
  pval = 1
) +
  facet_wrap(. ~ analysis, ncol = 1, scales = "free_y")

## ----ttest-pvalues------------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After",
  pval = 2
) +
  facet_wrap(. ~ analysis, ncol = 1, scales = "free_y")

## ----complete-example---------------------------------------------------------
dumbbell(
  xdf = plot_data,
  id = "subject",
  key = "analysis",
  column1 = "score.before",
  column2 = "score.after",
  lab1 = "Before",
  lab2 = "After",
  title = "Change in scores before and after intervention",
  delt = 1,
  arrow = 1,
  pval = 2,
  pt_val = 1,
  pointsize = 2,
  textsize = 3,
  segsize = 0.8,
  p_col1 = "red",
  p_col2 = "blue",
  col_seg1 = "gray50",
  col_seg2 = "gray50",
  expandx = 0.15
) +
  facet_wrap(. ~ analysis, ncol = 1, scales = "free_y") +
  ggplot2::coord_cartesian(xlim = c(35, 110))

## ----session-info-------------------------------------------------------------
sessionInfo()

Try the dumbbell package in your browser

Any scripts or data that you put into this service are public.

dumbbell documentation built on July 7, 2026, 1:06 a.m.