-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
128 lines (99 loc) · 4.85 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
dev = "ragg_png",
dpi = 400,
fig.width = 12, fig.height = 6
)
```
# ggswim <a href="https://chop-cgtinformatics.github.io/ggswim/"><img src="man/figures/logo.png" align="right" height="138" /></a>
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/CHOP-CGTInformatics/ggswim/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/CHOP-CGTInformatics/ggswim/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/CHOP-CGTInformatics/ggswim/branch/main/graph/badge.svg)](https://app.codecov.io/gh/CHOP-CGTInformatics/ggswim?branch=main)
<!-- badges: end -->
The ggswim package provides a convenient set of commands to easily create swimmer plots. As an extension of ggplot2, it streamlines the process of generating legends that effectively communicate events of interest along subject response paths.
ggswim solves some of the headaches associated with layer management in ggplot2 by organizing and classifying data into "markers" and "lanes." While nothing changes about the data itself, the way it's presented winds up being much closer to what's expected to communicate a swimmer plot's contents.
## Installation
You can install the development version of ggswim like so:
``` r
devtools::install_github("CHOP-CGTInformatics/ggswim")
```
## Usage
To help you get started, ggswim includes three sample datasets: `patient_data`, `infusion_events`, and `end_study_events`. These de-identified datasets simulate real world data related to infusions, disease assessments, and study statuses for a clinical trial.
ggswim offers several geom-functions, and by using `geom_swim_lane()` we can set up the horizontal response paths of our swimmer plot, i.e. the "lanes". We'll also set up corresponding arrows to indicate subjects that are still on the trial:
```{r, message=FALSE}
library(ggswim)
library(ggplot2)
# 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()
p <- patient_data |>
ggplot() +
geom_swim_lane(
mapping = aes(
x = start_time, y = pt_id, xend = end_time,
colour = disease_assessment
)
) +
geom_swim_arrow(
data = arrow_data,
mapping = aes(xend = end_time, y = pt_id)
) +
scale_colour_brewer(
name = "Disease Assessments",
palette = "Set1"
)
p
```
Next we'll add on events of interest: end of study updates and infusions. We'll refer to these as "markers" and call them with the next main "geom" function: `geom_swim_marker()`. While it is often common to see these datasets as separate components in the wild, we'll make our lives a little easier during plotting by combining them first.
```{r, message=FALSE}
all_events <- dplyr::bind_rows(
infusion_events,
end_study_events
)
p <- p +
geom_swim_marker(
data = all_events,
aes(x = time_from_initial_infusion, y = pt_id, marker = label),
size = 5
)
p
```
This looks OK as a default, but it's not quite as nice as we'd like it to be. Let's specify that we have particular `glyph`s and `colour`s we'd like to use for the markers with ggswim's `scale_marker_discrete()`.
```{r, message = FALSE}
p <- p +
scale_marker_discrete(
glyphs = all_events$glyph,
colours = all_events$colour,
limits = all_events$label,
name = "Study Events"
)
p
```
Using the custom `marker` `aes()` in `geom_swim_marker()` in combination with specific scale definitions helps us keep the markers and lanes separate in the legend. Finally, we'll beautify the plot with familiar ggplot2 techniques and a last finishing touch with `theme_ggswim()`:
```{r, message=FALSE}
p +
scale_colour_brewer(name = "Lanes", palette = "Set1") +
labs(title = "My Swimmer Plot") +
xlab("Time Since Initial Infusion (Months)") + ylab("Patient ID") +
theme_ggswim()
```
## Collaboration
We invite you to give feedback and collaborate with us! If you are familiar with GitHub and R packages, please feel free to submit a [pull request](https://github.com/CHOP-CGTInformatics/ggswim/pulls). Please do let us know if ggswim fails for whatever reason with your use case and submit a bug report by creating a GitHub [issue](https://github.com/CHOP-CGTInformatics/ggswim/issues).
Please note that this project is released with a Contributor Code of Conduct. By participating you agree to abide by its terms.