-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathREADME.Rmd
162 lines (133 loc) · 5.99 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
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%",
fig.retina = 3
)
```
# ggconsort <img src="man/figures/logo.png" align="right" height="139" />
<!-- badges: start -->
<!-- badges: end -->
## Overview
The goal of ggconsort is to provide convenience functions for creating [CONSORT diagrams](http://www.consort-statement.org/consort-statement/flow-diagram) with ggplot2. ggconsort segments CONSORT creation into two stages: (1) counting and annotation at the time of data wrangling, and (2) diagram layout and aesthetic design. With the introduction of a `ggconsort_cohort` class, stage (1) can be accomplished within dplyr chains. Specifically, the following functions are implemented inside a dplyr chain to define a `ggconsort_cohort`:
* `cohort_start()` initializes a `ggconsort_cohort` object which contains a labeled copy of the source data
* `cohort_define()` constructs cohorts that are variations of the source data or other cohorts
* `cohort_label()` adds labels to each named cohort within the `ggconsort_cohort` object
Stage 2 makes use of three ggconsort `consort_` verbs which equip the `ggconsort_cohort` object with `ggconsort` properties. The `ggconsort` object can be viewed with ggplot via `geom_consort() + theme_consort()`. `plot` and `print` methods are also available for the `ggconsort` object for visually iterative development.
* `consort_box_add()` adds a text box to the CONSORT diagram
* `consort_arrow_add()` adds an arrow to the CONSORT diagram
* `consort_line_add()` adds a line (without an arrow head) the CONSORT diagram
## Installation
You can install the released version of ggconsort from [GitHub](https://github.com/tgerke/ggconsort) with:
``` r
# install.packages("devtools")
devtools::install_github("tgerke/ggconsort")
```
## Usage
To demonstrate usage, we use a simulated dataset within ggconsort called `trial_data`, which contains 1200 patients who were approached to participate in a randomized trial comparing Drug A to Drug B.
```{r trial_data}
library(ggconsort)
head(trial_data)
```
Of the 1200 approached patients, only a subset were ultimately randomized: some declined to participate or were ineligible (due to prior chemotherapy or bone metastasis). We will use ggconsort verbs and geoms to count the patient flow and represent the process in a CONSORT diagram.
We first define the `ggconsort_cohort` object (`study_cohorts`) in the following dplyr chain.
```{r example-cohort, message=FALSE}
library(dplyr)
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"
)
```
We can have a look at the `study_cohorts` object with its print and summary methods:
```{r print-summary}
study_cohorts
summary(study_cohorts)
```
Next, we add CONSORT "boxes" and "arrows" with appropriate ggconsort verbs, and plot with the CONSORT diagram `ggplot`. Note the use of `cohort_count_adorn()`, which is a convenience function that glues cohort counts to their labels.
```{r example-consort}
library(ggplot2)
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) +
# you can include other ggplot geoms, as needed -------------
ggtext::geom_richtext(
aes(x = 0, y = 10, label = "Allocation"),
fill = "#9bc0fc"
)
```
At this point, we are ready for analysis. The following retrieves the desired data frame of randomized subjects:
```{r pull-data}
study_cohorts %>%
cohort_pull(randomized)
```