Visualization Demo

Author

Ashley E. Mullan

Modified

March 28, 2026

Package and Data Setup

Code
library(echarts4r) 
library(lubridate) 
library(dplyr) 
library(tidyr)
library(ggiraph)
library(ggplot2)
library(ggconsort)

df <- airquality

df <- df |> mutate(Month = sprintf("%02d", Month), 
                   Day = sprintf("%02d", Day)) |> 
  mutate(Date = as.Date(paste("1973", Month, Day, sep = "-"), 
                        format = "%Y-%m-%d"))

df_long <- df |> pivot_longer(c(Temp, Wind))

Interactivity

Examples: echarts4r

Code
#option 1: long form requires a group_by and a single e_line call 
df_long |> group_by(name) |> e_charts(x = Date) |> e_line(serie = value)
Code
#option 2: wide form can skip the grouping but needs two separate e_line calls 
df |> e_charts(x = Date) |> 
  e_line(serie = Temp) |> 
  e_line(serie = Wind) |> 
  e_tooltip(trigger = "item") |> #mouse over item for its value 
  e_title(text = "Example Plot", #add title 
          subtext = "generated from wide form") |> #add subtitle 
  e_axis_labels(x = "Month in 1973", #add axis labels
                y = "Whatever, I don't really care") |> 
  e_x_axis(nameLocation = 'center') |> #axis label placement (default: right)
  e_y_axis(nameLocation = "center") #axis label placement (default: top left)

Example: ggiraph

Code
df_long |>
  mutate(dv = paste0(Date, ": ", value)) |>
  ggplot(aes(x = Date, y = value, color = name)) +
  geom_line() +
  geom_point_interactive(aes(tooltip = dv, data_id = Date), 
                        hover_nearest = TRUE) + #create interactive points
  scale_y_continuous(limits = c(0,100)) +
  scale_color_manual(values = c("#5070dd", "#b6d635")) +
  labs(color = "Measurement") +
  theme_bw() +
  theme(legend.position = "bottom") -> plot_obj
girafe(ggobj = plot_obj) #generate interactive plot

Consort Diagrams

Example: ggconsort

Thank you to Travis Gerke for developing this example.

Code
study_cohorts <- trial_data |> 
  cohort_start("Assessed for eligibility") |>
  # Define cohorts using named expressions --------------------
  # Notice that you can use previously defined cohorts in subsequent steps
  cohort_define(
    consented = .full |> filter(declined != 1),
    consented_chemonaive = consented |> filter(prior_chemo != 1),
    randomized = consented_chemonaive |> filter(bone_mets != 1),
    treatment_a = randomized |> filter(treatment == "Drug A"),
    treatment_b = randomized |> filter(treatment == "Drug B"),
    # anti_join is useful for counting exclusions -------------
    excluded = anti_join(.full, randomized, by = "id"),
    excluded_declined = anti_join(.full, consented, by = "id"),
    excluded_chemo = anti_join(consented, consented_chemonaive, by = "id"),
    excluded_mets = anti_join(consented_chemonaive, randomized, by = "id")
  ) |>
  # Provide text labels for cohorts ---------------------------
  cohort_label(
    consented = "Consented",
    consented_chemonaive = "Chemotherapy naive",
    randomized = "Randomized",
    treatment_a = "Allocated to arm A",
    treatment_b = "Allocated to arm B",
    excluded = "Excluded",
    excluded_declined = "Declined to participate",
    excluded_chemo = "Prior chemotherapy",
    excluded_mets = "Bone metastasis"
  )
Code
study_consort <- study_cohorts |>
  consort_box_add(
    "full", 0, 50, cohort_count_adorn(study_cohorts, .full)
  ) |>
  consort_box_add(
    "exclusions", 20, 40, glue::glue(
      '{cohort_count_adorn(study_cohorts, excluded)}<br>
      • {cohort_count_adorn(study_cohorts, excluded_declined)}<br>
      • {cohort_count_adorn(study_cohorts, excluded_chemo)}<br>
      • {cohort_count_adorn(study_cohorts, excluded_mets)}
      ')
  ) |>
  consort_box_add(
    "randomized", 0, 30, cohort_count_adorn(study_cohorts, randomized)
  ) |>
  consort_box_add(
    "arm_a", -30, 10, cohort_count_adorn(study_cohorts, treatment_a)
  ) |>
  consort_box_add(
    "arm_b", 30, 10, cohort_count_adorn(study_cohorts, treatment_b)
  ) |>
  consort_arrow_add(
    end = "exclusions", end_side = "left", start_x = 0, start_y = 40
  ) |>
  consort_arrow_add(
    "full", "bottom", "randomized", "top"
  ) |> 
  consort_arrow_add(
    start_x = 0, start_y = 30, end_x = 0, end_y = 20,
  ) |>
  consort_line_add(
    start_x = -30, start_y = 20, end_x = 30, end_y = 20,
  ) |> 
  consort_arrow_add(
    end = "arm_a", end_side = "top", start_x = -30, start_y = 20
  ) |>
  consort_arrow_add(
    end = "arm_b", end_side = "top", start_x = 30, start_y = 20
  )

study_consort |>
  ggplot() + 
  geom_consort() +
  theme_consort(margin_h = 8, margin_v = 1)