@@ -13,7 +13,7 @@ ggplot2::autoplot
13
13
# '
14
14
# '
15
15
# ' @inheritParams epiprocess::autoplot.epi_df
16
- # ' @param object An `epi_workflow`
16
+ # ' @param object,x An `epi_workflow`
17
17
# ' @param predictions A data frame with predictions. If `NULL`, only the
18
18
# ' original data is shown.
19
19
# ' @param .levels A numeric vector of levels to plot for any prediction bands.
@@ -48,7 +48,7 @@ ggplot2::autoplot
48
48
# '
49
49
# ' latest <- jhu %>% filter(time_value >= max(time_value) - 14)
50
50
# ' preds <- predict(wf, latest)
51
- # ' autoplot(wf, preds, .max_facets = 4 )
51
+ # ' autoplot(wf, preds, .facet_filter = geo_value %in% c("ca", "ny", "de", "mt") )
52
52
# '
53
53
# ' # ------- Show multiple horizons
54
54
# '
@@ -63,19 +63,19 @@ ggplot2::autoplot
63
63
# ' })
64
64
# '
65
65
# ' p <- do.call(rbind, p)
66
- # ' autoplot(wf, p, .max_facets = 4 )
66
+ # ' autoplot(wf, p, .facet_filter = geo_value %in% c("ca", "ny", "de", "mt") )
67
67
# '
68
68
# ' # ------- Plotting canned forecaster output
69
69
# '
70
70
# ' jhu <- covid_case_death_rates %>%
71
71
# ' filter(time_value >= as.Date("2021-11-01"))
72
72
# ' flat <- flatline_forecaster(jhu, "death_rate")
73
- # ' autoplot(flat, .max_facets = 4 )
73
+ # ' autoplot(flat, .facet_filter = geo_value %in% c("ca", "ny", "de", "mt") )
74
74
# '
75
75
# ' arx <- arx_forecaster(jhu, "death_rate", c("case_rate", "death_rate"),
76
76
# ' args_list = arx_args_list(ahead = 14L)
77
77
# ' )
78
- # ' autoplot(arx, .max_facets = 6 )
78
+ # ' autoplot(arx, .facet_filter = geo_value %in% c("ca", "ny", "de", "mt", "mo", "in") )
79
79
NULL
80
80
81
81
# ' @export
@@ -87,7 +87,8 @@ autoplot.epi_workflow <- function(
87
87
.facet_by = c(" .response" , " other_keys" , " all_keys" , " geo_value" , " all" , " none" ),
88
88
.base_color = " dodgerblue4" ,
89
89
.point_pred_color = " orange" ,
90
- .max_facets = Inf ) {
90
+ .facet_filter = NULL ,
91
+ .max_facets = deprecated()) {
91
92
rlang :: check_dots_empty()
92
93
arg_is_probabilities(.levels )
93
94
rlang :: arg_match(.color_by )
@@ -134,7 +135,7 @@ autoplot.epi_workflow <- function(
134
135
return (autoplot(
135
136
edf , new_name_y ,
136
137
.color_by = .color_by , .facet_by = .facet_by , .base_color = .base_color ,
137
- .max_facets = .max_facets
138
+ .facet_filter = {{ .facet_filter }}
138
139
))
139
140
}
140
141
@@ -153,25 +154,25 @@ autoplot.epi_workflow <- function(
153
154
return (autoplot(
154
155
edf , !! new_name_y ,
155
156
.color_by = .color_by , .facet_by = .facet_by , .base_color = .base_color ,
156
- .max_facets = .max_facets
157
+ .facet_filter = {{ .facet_filter }}
157
158
))
158
159
}
159
160
160
161
# First we plot the history, always faceted by everything
161
162
bp <- autoplot(edf , !! new_name_y ,
162
163
.color_by = " none" , .facet_by = " all_keys" ,
163
- .base_color = " black" , .max_facets = .max_facets
164
+ .base_color = " black" , .facet_filter = {{ .facet_filter }}
164
165
)
165
166
166
167
# Now, prepare matching facets in the predictions
167
168
ek <- epi_keys_only(edf )
168
169
predictions <- predictions %> %
169
170
mutate(
170
- .facets = interaction(!!! rlang :: syms(as.list(ek )), sep = " / " ),
171
+ .facets = interaction(!!! rlang :: syms(as.list(ek )), sep = " / " ),
171
172
)
172
- if ( .max_facets < Inf ) {
173
- top_n <- levels(as.factor( bp $ data $ .facets ))[seq_len( .max_facets )]
174
- predictions <- filter(predictions , .facets %in% top_n ) %> %
173
+ .facet_filter <- rlang :: enquo( .facet_filter )
174
+ if ( ! rlang :: quo_is_null( .facet_filter ) && " .facets " %in% names( bp $ data )) {
175
+ predictions <- filter(predictions , .facets %in% unique( bp $ data $ .facets ) ) %> %
175
176
mutate(.facets = droplevels(.facets ))
176
177
}
177
178
@@ -207,7 +208,8 @@ autoplot.canned_epipred <- function(
207
208
.facet_by = c(" .response" , " other_keys" , " all_keys" , " geo_value" , " all" , " none" ),
208
209
.base_color = " dodgerblue4" ,
209
210
.point_pred_color = " orange" ,
210
- .max_facets = Inf ) {
211
+ .facet_filter = NULL ,
212
+ .max_facets = deprecated()) {
211
213
rlang :: check_dots_empty()
212
214
rlang :: arg_match(.color_by )
213
215
rlang :: arg_match(.facet_by )
@@ -218,10 +220,23 @@ autoplot.canned_epipred <- function(
218
220
219
221
autoplot(ewf , predictions ,
220
222
.color_by = .color_by , .facet_by = .facet_by ,
221
- .base_color = .base_color , .max_facets = .max_facets
223
+ .base_color = .base_color , .facet_filter = {{ .facet_filter }}
222
224
)
223
225
}
224
226
227
+ # ' @export
228
+ # ' @rdname autoplot-epipred
229
+ plot.epi_workflow <- function (x , ... ) {
230
+ autoplot(x , ... )
231
+ }
232
+
233
+ # ' @export
234
+ # ' @rdname autoplot-epipred
235
+ plot.canned_epipred <- function (x , ... ) {
236
+ autoplot(x , ... )
237
+ }
238
+
239
+
225
240
starts_with_impl <- function (x , vars ) {
226
241
n <- nchar(x )
227
242
x == substr(vars , 1 , n )
0 commit comments