-
Notifications
You must be signed in to change notification settings - Fork 11
/
11c-plots-examples-safety.Rmd
167 lines (120 loc) · 5.77 KB
/
11c-plots-examples-safety.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
163
164
165
166
### Safety Plots
Again, we refer to the FDA safety guide which you can find int he [scatterplot section][Scatter plot]. The [falcon](https://github.com/pharmaverse/falcon) package aims to cover all tables and plots eventually. However, similar visualisation might be of interest also outside of this package. This is why we provide some code here to show how to combine a plot and a table using the *patchwork* package.
But we start with loading the required libraries and adsl and adlb dataset:
```{r, message = FALSE, warning = FALSE, error = FALSE}
### load relevant libraries and data
library(dplyr)
library(haven)
library(patchwork)
library(ggplot2)
adam_path <- "https://github.com/phuse-org/TestDataFactory/raw/main/Updated/TDF_ADaM/"
adsl <- read_xpt(paste0(adam_path, "adsl.xpt"))
adlb <- read_xpt(paste0(adam_path, "adlbc.xpt"))
# prepare lab data set for plotting
data <- adlb %>%
filter(PARAMCD == "SODIUM" & !is.na(AVISIT)) %>%
select(USUBJID, TRTA, AVISIT, PARAM, AVAL, BASE, CHG) %>%
mutate(AVISIT = factor(AVISIT, levels = c("Baseline", "Week 2", "Week 4", "Week 6", "Week 8", "Week 12",
"Week 16", "Week 20", "Week 24", "Week 26", "End of Treatment")))
```
```{r, echo=FALSE}
kableExtra::kable(head(data, n=5), escape = FALSE, align = 'c')
```
Now, we create a line plot which deomnstrates the change of the average of Sodium per week. Each line represents one dose. Additionally, we also provide the standard error intervals.
```{r, message = FALSE, warning = FALSE, error = FALSE}
# create plot
p <- data %>%
filter(!AVISIT %in% c("Baseline", "End of Treatment") & !is.na(AVISIT)) %>%
group_by(AVISIT, TRTA) %>%
mutate(mean_chg = mean(CHG, na.rm = TRUE),
se_chg = sd(CHG, na.rm = TRUE) / sqrt(length(CHG))) %>%
ungroup() %>%
ggplot(., aes(x=AVISIT, y=mean_chg, group=TRTA, linetype = TRTA)) +
geom_errorbar(aes(ymin=mean_chg-se_chg, ymax=mean_chg+se_chg, color=TRTA), width=.1) +
geom_line(aes(color=TRTA)) +
geom_point(aes(color=TRTA)) +
geom_hline(yintercept=0, linetype='dashed')+
scale_linetype_manual(values = c("dashed",rep("solid",2))) +
scale_color_manual(values=c("#93918E","#0B5A8F", "#73BDEE")) +
labs(x = "", y = "Mean Change from Baseline (95% CI) \n Sodium (mmol/l)") +
theme_minimal() +
theme(legend.position="bottom",
legend.title=element_blank()) +
coord_fixed() +
theme(aspect.ratio = 0.5)
p
```
Addtionally, we would like to create two tables. The first one should display the mean change from baseline and the mean value for each visit, the second one the number of subjects per visit.
```{r, message = FALSE, warning = FALSE, error = FALSE}
# prepare data for table
table_data <- data %>%
filter(!AVISIT %in% c("Baseline", "End of Treatment") & !is.na(AVISIT)) %>%
group_by(AVISIT, TRTA) %>%
mutate(mean_chg = mean(CHG, na.rm = TRUE),
mean_aval = mean(AVAL, na.rm = TRUE),
n_patients = n_distinct(USUBJID),
n_patients = n_distinct(USUBJID),
TRTA = case_when(
TRTA == "Xanomeline High Dose" ~"High Dose",
TRTA == "Xanomeline Low Dose" ~ "Low Dose",
TRUE ~ TRTA)) %>%
ungroup() %>%
select(TRTA, AVISIT, mean_chg, mean_aval, n_patients) %>%
distinct()
data_table1 <- table_data %>%
select(-n_patients) %>%
mutate(value = paste0(round(mean_chg, 2), "/", round(mean_aval, 2)))
data_table2 <- table_data %>%
select(-mean_chg, -mean_aval)
```
```{r, echo=FALSE}
kableExtra::kable(head(data_table1, n=5), escape = FALSE, align = 'c')
```
Now, we transform the table to a plot to be able to style it and display it aligned with the visualisation above.
```{r, message = FALSE, warning = FALSE, error = FALSE}
# plot table 1
p_table1 <- ggplot(data = data_table1, aes(x = AVISIT, y = TRTA)) +
scale_shape_manual(values = 1:length(data_table1$TRTA))+
ggpubr::geom_exec(geom_text, data = data_table1, label = data_table1$value, color = "TRTA", size=3) +
theme_classic() +
labs(title = "Mean Change from Baseline / Mean Value", x = "", y = "" ) +
scale_color_manual(values=c("#93918E","#0B5A8F", "#73BDEE")) +
theme(axis.text.y = element_text(colour = c("#93918E","#0B5A8F", "#73BDEE")),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank()) +
theme(legend.position="none",
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.title = element_text(size=10, face = "bold"))
# just for the sake of displaying it
p_table1 +
coord_fixed(ratio=.5)
```
We do the same for the second table.
```{r, message = FALSE, warning = FALSE, error = FALSE}
# plot table 2
p_table2 <- ggplot(data = data_table2, aes(x = AVISIT, y = TRTA)) +
scale_shape_manual(values = 1:length(data_table2$TRTA))+
ggpubr::geom_exec(geom_text, data = data_table2, label = data_table2$n_patients, color = "TRTA", size=3) +
theme_classic() +
labs(title = "Number of Patients with Data", x = "", y = "" ) +
scale_color_manual(values=c("#93918E","#0B5A8F", "#73BDEE")) +
theme(axis.text.y = element_text(colour = c("#93918E","#0B5A8F", "#73BDEE")),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_blank()) +
theme(legend.position="none",
panel.border = element_rect(colour = "black", fill=NA, size=1),
plot.title = element_text(size=10, face = "bold"))
# just for the sake of displaying it
p_table2 +
coord_fixed(ratio=.5)
```
Finally, we combine all three visualisations using the patchwork package functionality:
```{r, message = FALSE, warning = FALSE, error = FALSE, fig.width = 12, fig.asp = 0.9}
# combine plots
final_plot <- p + p_table1 + p_table2
# display plot w/ layout
final_plot + plot_layout(byrow = FALSE,
heights = c(5,1,1))
```