Skip to content

Commit 24e6a4e

Browse files
committed
Add functionality to plot_measures and replace ggplot code with called functions in Rmd
1 parent 28fa0a2 commit 24e6a4e

File tree

2 files changed

+206
-315
lines changed

2 files changed

+206
-315
lines changed

lib/functions/plot_measures.R

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ plot_measures <- function(
3232
shape_var = NULL,
3333
save_path = NULL,
3434
colour_palette = NULL,
35+
y_scale = NULL,
36+
scale_measure = NULL,
37+
shapes = NULL,
38+
date_breaks = "1 month",
3539
legend_position = "bottom") {
3640
# Test if all columns expected in output from generate measures exist
3741
# expected_names <- c("measure", "interval_start", "interval_end", "ratio", "numerator", "denominator")
@@ -48,7 +52,8 @@ plot_measures <- function(
4852
y = {{ select_value }},
4953
colour = {{ colour_var }},
5054
group = {{ colour_var }},
51-
shape = {{ colour_var }}
55+
shape = {{ colour_var }},
56+
fill = {{ colour_var }}
5257
)
5358
) +
5459
geom_point(size = 2) +
@@ -60,7 +65,7 @@ plot_measures <- function(
6065
linewidth = .7
6166
) +
6267
scale_x_date(
63-
date_breaks = "1 month",
68+
date_breaks = {{ date_breaks }},
6469
labels = scales::label_date_short()
6570
) +
6671
guides(
@@ -72,33 +77,52 @@ plot_measures <- function(
7277
x = x_label,
7378
y = y_label,
7479
colour = guide_label,
75-
shape = guide_label
80+
shape = NULL,
81+
fill = NULL
7682
) +
7783
theme(
7884
legend.position = legend_position,
7985
plot.title = element_text(hjust = 0.5),
8086
text = element_text(size = 14)
8187
)
8288

83-
if(!is.null(colour_palette)) {
84-
plot_tmp <- plot_tmp + scale_colour_manual(values = colour_palette)
89+
# Change colour based on specified colour palette
90+
if (!is.null(colour_palette)) {
91+
if (length(colour_palette) == 1 && colour_palette == "plasma") {
92+
plot_tmp <- plot_tmp + scale_colour_viridis_d(option = "plasma", end = .75) +
93+
geom_line(size = 0.5) +
94+
geom_point(size = 2.5)
95+
} else {
96+
plot_tmp <- plot_tmp + scale_colour_manual(values = colour_palette)
97+
}
8598
} else {
8699
plot_tmp <- plot_tmp + scale_colour_viridis_d(end = .75)
87100
}
88101

102+
if (!is.null(shapes) && shapes == "condition_shapes") {
103+
plot_tmp <- plot_tmp + scale_shape_manual(values = condition_shapes)
104+
}
105+
89106
# Automatically change y scale depending selected value
90-
if (rlang::as_label(enquo(select_value)) == "ratio") {
107+
scale_label <- rlang::as_label(enquo(scale_measure))
108+
if (is.null(scale_measure)) {
109+
plot_tmp <- plot_tmp + scale_y_continuous(
110+
limits = c(0, NA),
111+
labels = scales::label_number()
112+
)
113+
} else if (scale_measure == "rate") {
91114
plot_tmp <- plot_tmp + scale_y_continuous(
92115
limits = c(0, NA),
93-
# scale = 1000 to calculate rate per 1000 people
94116
labels = scales::label_number(scale = 1000)
95117
)
118+
} else if (scale_measure == "percent") {
119+
plot_tmp <- plot_tmp + scale_y_continuous(labels = scales::percent)
96120
} else {
97-
plot_tmp <- plot_tmp + scale_y_continuous(
98-
limits = c(0, NA),
99-
labels = scales::label_number()
100-
)
101-
}
121+
plot_tmp <- plot_tmp + scale_y_continuous(
122+
limits = c(0, NA),
123+
labels = scales::label_number()
124+
)
125+
}
102126

103127
# Add facets if requested
104128
# Ideally we would want to check facet_var instead of having an additional argument facet_wrap
@@ -107,7 +131,13 @@ plot_measures <- function(
107131
plot_tmp <- plot_tmp +
108132
facet_wrap(vars({{ facet_var }}), ncol = 2)
109133
}
134+
# Add y_scale to add option for free_y
135+
if (!is.null(y_scale) && y_scale == "free_y") {
136+
plot_tmp <- plot_tmp +
137+
facet_wrap(~source, scales = "free_y")
138+
}
110139

140+
# Add save_path option
111141
if (!is.null(save_path)) {
112142
ggsave(
113143
filename = here("released_output", "results", "figures", save_path),
@@ -120,8 +150,43 @@ plot_measures <- function(
120150
plot_tmp
121151
}
122152

153+
# Combining two figures into one using patchwork
154+
patch_figures <- function(figure_1, figure_2, save_path=NULL) {
155+
combined_figure <- (figure_1 + figure_2) +
156+
plot_annotation(tag_levels = "A") +
157+
plot_layout(guides = "collect", widths = c(2, 1)) &
158+
theme(
159+
legend.position = "bottom",
160+
text = element_text(size = 15),
161+
strip.background = element_rect(size = 0),
162+
strip.text.x = element_text(size = 13, face = "bold")
163+
)
164+
165+
if (!is.null(save_path)) {
166+
ggsave(
167+
filename = here("released_output", "results", "figures", save_path),
168+
plot = combined_figure,
169+
width = 15,
170+
height = 6
171+
)
172+
}
173+
combined_figure
174+
}
175+
123176
# Colour palettes
124177
gradient_palette <- c("#001F4D", "#0056B3", "#007BFF", "#66B3E2", "#A4D8E1", "grey")
125178
region_palette <- c("red", "navy", "#018701", "#ffa600ca", "purple", "brown", "#f4a5b2", "cyan", "green", "grey")
126179
ethnicity_palette <- c("#42db0188", "#0056B3", "#ff0000c2", "#a52a2a5a", "purple", "grey")
127180
sex_palette <- c("red", "blue")
181+
dark2_palette <- RColorBrewer::brewer.pal(n = 8, name = "Dark2")
182+
183+
# Custom shapes
184+
condition_shapes = c(
185+
"Acute Sinusitis" = 15,
186+
"Infected Insect Bite" = 19,
187+
"UTI" = 4,
188+
"Acute Otitis Media" = 23,
189+
"Acute Pharyngitis" = 3,
190+
"Herpes Zoster" = 17,
191+
"Impetigo" = 8
192+
)

0 commit comments

Comments
 (0)