@@ -29,62 +29,98 @@ plot_measures <- function(
29
29
facet_wrap = FALSE ,
30
30
facet_var = NULL ,
31
31
colour_var = NULL ,
32
+ shape_var = NULL ,
33
+ colour_palette = NULL ,
34
+ y_scale = NULL ,
35
+ scale_measure = NULL ,
36
+ shapes = NULL ,
37
+ date_breaks = " 1 month" ,
32
38
legend_position = " bottom" ) {
33
39
# Test if all columns expected in output from generate measures exist
34
- expected_names <- c(" measure" , " interval_start" , " interval_end" , " ratio" , " numerator" , " denominator" )
35
- missing_columns <- setdiff(expected_names , colnames(data ))
40
+ # expected_names <- c("measure", "interval_start", "interval_end", "ratio", "numerator", "denominator")
41
+ # missing_columns <- setdiff(expected_names, colnames(data))
36
42
37
- if (length(missing_columns ) > 0 ) {
38
- stop(" Data does not have expected column(s): " , paste(missing_columns , collapse = " , " ), call. = FALSE )
39
- }
43
+ # if (length(missing_columns) > 0) {
44
+ # stop("Data does not have expected column(s): ", paste(missing_columns, collapse = ", "), call. = FALSE)
45
+ # }
40
46
41
47
plot_tmp <- ggplot(
42
48
data ,
43
49
aes(
44
50
x = {{ select_interval_date }},
45
51
y = {{ select_value }},
46
52
colour = {{ colour_var }},
47
- group = {{ colour_var }}
53
+ group = {{ colour_var }},
54
+ shape = {{ colour_var }},
55
+ fill = {{ colour_var }}
48
56
)
49
57
) +
50
- geom_point() +
51
- geom_line(alpha = .5 ) +
58
+ geom_point(size = 2 ) +
59
+ geom_line(alpha = .3 ) +
52
60
geom_vline(
53
61
xintercept = lubridate :: as_date(" 2024-02-01" ),
54
62
linetype = " dotted" ,
55
63
colour = " orange" ,
56
64
linewidth = .7
57
65
) +
58
66
scale_x_date(
59
- date_breaks = " 1 month " ,
67
+ date_breaks = {{ date_breaks }} ,
60
68
labels = scales :: label_date_short()
61
69
) +
62
70
guides(
63
- color = guide_legend(nrow = guide_nrow )
71
+ color = guide_legend(nrow = guide_nrow ),
72
+ shape = guide_legend(nrow = guide_nrow )
64
73
) +
65
74
labs(
66
75
title = title ,
67
76
x = x_label ,
68
77
y = y_label ,
69
78
colour = guide_label ,
79
+ shape = NULL ,
80
+ fill = NULL
70
81
) +
71
82
theme(
72
83
legend.position = legend_position ,
73
- plot.title = element_text(hjust = 0.5 )
84
+ plot.title = element_text(hjust = 0.5 ),
85
+ text = element_text(size = 14 )
74
86
)
75
87
88
+ # Change colour based on specified colour palette
89
+ if (! is.null(colour_palette )) {
90
+ if (length(colour_palette ) == 1 && colour_palette == " plasma" ) {
91
+ plot_tmp <- plot_tmp + scale_colour_viridis_d(option = " plasma" , end = .75 ) +
92
+ geom_line(size = 0.5 ) +
93
+ geom_point(size = 2.5 )
94
+ } else {
95
+ plot_tmp <- plot_tmp + scale_colour_manual(values = colour_palette )
96
+ }
97
+ } else {
98
+ plot_tmp <- plot_tmp + scale_colour_viridis_d(end = .75 )
99
+ }
100
+
101
+ if (! is.null(shapes ) && shapes == " condition_shapes" ) {
102
+ plot_tmp <- plot_tmp + scale_shape_manual(values = condition_shapes )
103
+ }
104
+
76
105
# Automatically change y scale depending selected value
77
- if (rlang :: as_label(enquo(select_value )) %in% c(" numerator" , " denominator" )) {
106
+ scale_label <- rlang :: as_label(enquo(scale_measure ))
107
+ if (is.null(scale_measure )) {
78
108
plot_tmp <- plot_tmp + scale_y_continuous(
79
109
limits = c(0 , NA ),
80
110
labels = scales :: label_number()
81
111
)
82
- } else {
112
+ } else if ( scale_measure == " rate " ) {
83
113
plot_tmp <- plot_tmp + scale_y_continuous(
84
114
limits = c(0 , NA ),
85
- # scale = 1000 to calculate rate per 1000 people
86
115
labels = scales :: label_number(scale = 1000 )
87
116
)
117
+ } else if (scale_measure == " percent" ) {
118
+ plot_tmp <- plot_tmp + scale_y_continuous(labels = scales :: percent )
119
+ } else {
120
+ plot_tmp <- plot_tmp + scale_y_continuous(
121
+ limits = c(0 , NA ),
122
+ labels = scales :: label_number()
123
+ )
88
124
}
89
125
90
126
# Add facets if requested
@@ -94,12 +130,51 @@ plot_measures <- function(
94
130
plot_tmp <- plot_tmp +
95
131
facet_wrap(vars({{ facet_var }}), ncol = 2 )
96
132
}
133
+ # Add y_scale to add option for free_y
134
+ if (! is.null(y_scale ) && y_scale == " free_y" ) {
135
+ plot_tmp <- plot_tmp +
136
+ facet_wrap(~ source , scales = " free_y" )
137
+ }
97
138
98
139
plot_tmp
99
140
}
100
141
142
+ set_patchwork_theme <- function (patchwork_figure ) {
143
+ patchwork_figure +
144
+ plot_annotation(tag_levels = " A" ) +
145
+ plot_layout(guides = " collect" , widths = c(2 , 1 )) &
146
+ theme(
147
+ legend.position = " bottom" ,
148
+ text = element_text(size = 15 ),
149
+ strip.background = element_rect(size = 0 ),
150
+ strip.text.x = element_text(size = 13 , face = " bold" )
151
+ )
152
+ }
153
+
154
+ save_figure <- function (figure , width = 10 , height = 6 ) {
155
+ # this uses the 'figure' argument as a string to later generate a filename
156
+ figure_name <- deparse(substitute(figure ))
157
+ ggsave(
158
+ filename = here(" released_output" , " results" , " figures" , paste(figure_name , " png" ,sep = " ." )),
159
+ figure ,
160
+ width = width , height = height
161
+ )
162
+ }
163
+
101
164
# Colour palettes
102
165
gradient_palette <- c(" #001F4D" , " #0056B3" , " #007BFF" , " #66B3E2" , " #A4D8E1" , " grey" )
103
166
region_palette <- c(" red" , " navy" , " #018701" , " #ffa600ca" , " purple" , " brown" , " #f4a5b2" , " cyan" , " green" , " grey" )
104
167
ethnicity_palette <- c(" #42db0188" , " #0056B3" , " #ff0000c2" , " #a52a2a5a" , " purple" , " grey" )
105
168
sex_palette <- c(" red" , " blue" )
169
+ dark2_palette <- RColorBrewer :: brewer.pal(n = 8 , name = " Dark2" )
170
+
171
+ # Custom shapes
172
+ condition_shapes <- c(
173
+ " Acute Sinusitis" = 15 ,
174
+ " Infected Insect Bite" = 19 ,
175
+ " UTI" = 4 ,
176
+ " Acute Otitis Media" = 23 ,
177
+ " Acute Pharyngitis" = 3 ,
178
+ " Herpes Zoster" = 17 ,
179
+ " Impetigo" = 8
180
+ )
0 commit comments