generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treatment_distributions.R
70 lines (60 loc) · 1.87 KB
/
treatment_distributions.R
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
######################################
# Treatment distributions
######################################
## Import libraries
library(tidyverse)
library(lubridate)
library(here)
## Create figures directory
fs::dir_create(here::here("output", "figs"))
## Create tables directory
fs::dir_create(here::here("output", "tables"))
## Import command-line arguments
args <- commandArgs(trailingOnly=TRUE)
## Set input and output pathways for matched/unmatched data - default is unmatched
if (length(args) == 0){
data_label = "day5"
} else if (args[[1]] == "day0") {
data_label = "day0"
} else if (args[[1]] == "day5") {
data_label = "day5"
} else {
# Print error if no argument specified
stop("No outcome specified")
}
## Import data
if (data_label == "day5") {
data_cohort <-
read_rds(here::here("output", "data", "data_processed_day5.rds"))
} else if (data_label == "day0") {
data_cohort <-
read_rds(here::here("output", "data", "data_processed_day0.rds"))
}
## Restrict to patients treated
d_trt <- data_cohort %>%
filter(treatment_strategy_cat != "Untreated") %>%
select(treatment_strategy_cat, tb_postest_treat)
d_trt$treatment_strategy_cat %>% table() %>% print()
## Plot treatment distributions by group
q <- d_trt %>%
ggplot(
aes(x=tb_postest_treat,
y=..density..,
fill=treatment_strategy_cat)) +
geom_histogram(color="#e9ecef",
alpha=0.8,
position = 'identity',
binwidth = 1) +
scale_fill_manual(values=c("#E69F00", "#0072B2")) +
scale_x_continuous(limits = c(-1, 5)) +
labs(fill="Treatment") +
xlab("Time-between positive test and treatment (days)") +
ylab("Density") +
labs(color='Treatment') +
theme_bw()
# Save plot
ggsave(q,
filename =
here("output", "figs",
paste0(data_label, "_treatment_pattern.png")),
width=20, height=14, units="cm")