Status Quo
library(tidyverse)
library(ggswim)
library(ggplot2)
p1 <- ggplot() +
geom_swim_lane(
data = patient_data,
mapping = aes(
x = start_time, y = pt_id, xend = end_time,
colour = disease_assessment
))
p1

# Construct arrow_data for arrow display later
arrow_data <- patient_data |>
dplyr::left_join(
end_study_events |>
dplyr::select(pt_id, label),
by = "pt_id"
) |>
dplyr::select(pt_id, end_time, label) |>
dplyr::filter(.by = pt_id, end_time == max(end_time)) |>
dplyr::filter(is.na(label)) |>
unique()
p2 <- ggplot() +
geom_swim_arrow(
data = arrow_data,
mapping = aes(xend = end_time, y = pt_id)
) +
scale_colour_brewer(
name = "Disease Assessments",
palette = "Set1"
)
p2

all_events <- dplyr::bind_rows(
infusion_events,
end_study_events
)
head(all_events)
## # A tibble: 6 × 5
## pt_id time_from_initial_infusion label glyph colour
## <chr> <dbl> <chr> <chr> <chr>
## 1 01 1 First Reinfusion ⬤ #999999
## 2 02 0 First Reinfusion ⬤ #999999
## 3 03 2 First Reinfusion ⬤ #999999
## 4 03 3 Second Reinfusion ⬤ #f57dc1
## 5 04 5 First Reinfusion ⬤ #999999
## 6 05 2 First Reinfusion ⬤ #999999
p3 <- ggplot() +
geom_swim_marker(
data = all_events,
aes(x = time_from_initial_infusion, y = pt_id, marker = label),
size = 5
)
p3

library(patchwork)
p1 / p2 / p3

ggplot() +
p1$layers +
p2$layers +
p3$layers

# but...
p1.2 <- ggplot(data = patient_data) +
geom_swim_lane(
mapping = aes(
x = start_time, y = pt_id, xend = end_time,
colour = disease_assessment
))
ggplot() +
p1.2$layers +
p2$layers +
p3$layers
## Error in `geom_swim_lane()`:
## ! Problem while computing aesthetics.
## ℹ Error occurred in the 1st layer.
## Caused by error:
## ! object 'start_time' not found