@@ -13,7 +13,7 @@ ggplot2::autoplot
1313# '
1414# '
1515# ' @inheritParams epiprocess::autoplot.epi_df
16- # ' @param object An `epi_workflow`
16+ # ' @param object,x An `epi_workflow`
1717# ' @param predictions A data frame with predictions. If `NULL`, only the
1818# ' original data is shown.
1919# ' @param .levels A numeric vector of levels to plot for any prediction bands.
@@ -48,7 +48,7 @@ ggplot2::autoplot
4848# '
4949# ' latest <- jhu %>% filter(time_value >= max(time_value) - 14)
5050# ' 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") )
5252# '
5353# ' # ------- Show multiple horizons
5454# '
@@ -63,19 +63,19 @@ ggplot2::autoplot
6363# ' })
6464# '
6565# ' 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") )
6767# '
6868# ' # ------- Plotting canned forecaster output
6969# '
7070# ' jhu <- covid_case_death_rates %>%
7171# ' filter(time_value >= as.Date("2021-11-01"))
7272# ' 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") )
7474# '
7575# ' arx <- arx_forecaster(jhu, "death_rate", c("case_rate", "death_rate"),
7676# ' args_list = arx_args_list(ahead = 14L)
7777# ' )
78- # ' autoplot(arx, .max_facets = 6 )
78+ # ' autoplot(arx, .facet_filter = geo_value %in% c("ca", "ny", "de", "mt", "mo", "in") )
7979NULL
8080
8181# ' @export
@@ -87,7 +87,8 @@ autoplot.epi_workflow <- function(
8787 .facet_by = c(" .response" , " other_keys" , " all_keys" , " geo_value" , " all" , " none" ),
8888 .base_color = " dodgerblue4" ,
8989 .point_pred_color = " orange" ,
90- .max_facets = Inf ) {
90+ .facet_filter = NULL ,
91+ .max_facets = deprecated()) {
9192 rlang :: check_dots_empty()
9293 arg_is_probabilities(.levels )
9394 rlang :: arg_match(.color_by )
@@ -134,7 +135,7 @@ autoplot.epi_workflow <- function(
134135 return (autoplot(
135136 edf , new_name_y ,
136137 .color_by = .color_by , .facet_by = .facet_by , .base_color = .base_color ,
137- .max_facets = .max_facets
138+ .facet_filter = {{ .facet_filter }}
138139 ))
139140 }
140141
@@ -153,25 +154,25 @@ autoplot.epi_workflow <- function(
153154 return (autoplot(
154155 edf , !! new_name_y ,
155156 .color_by = .color_by , .facet_by = .facet_by , .base_color = .base_color ,
156- .max_facets = .max_facets
157+ .facet_filter = {{ .facet_filter }}
157158 ))
158159 }
159160
160161 # First we plot the history, always faceted by everything
161162 bp <- autoplot(edf , !! new_name_y ,
162163 .color_by = " none" , .facet_by = " all_keys" ,
163- .base_color = " black" , .max_facets = .max_facets
164+ .base_color = " black" , .facet_filter = {{ .facet_filter }}
164165 )
165166
166167 # Now, prepare matching facets in the predictions
167168 ek <- epi_keys_only(edf )
168169 predictions <- predictions %> %
169170 mutate(
170- .facets = interaction(!!! rlang :: syms(as.list(ek )), sep = " / " ),
171+ .facets = interaction(!!! rlang :: syms(as.list(ek )), sep = " / " ),
171172 )
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 ) ) %> %
175176 mutate(.facets = droplevels(.facets ))
176177 }
177178
@@ -207,7 +208,8 @@ autoplot.canned_epipred <- function(
207208 .facet_by = c(" .response" , " other_keys" , " all_keys" , " geo_value" , " all" , " none" ),
208209 .base_color = " dodgerblue4" ,
209210 .point_pred_color = " orange" ,
210- .max_facets = Inf ) {
211+ .facet_filter = NULL ,
212+ .max_facets = deprecated()) {
211213 rlang :: check_dots_empty()
212214 rlang :: arg_match(.color_by )
213215 rlang :: arg_match(.facet_by )
@@ -218,10 +220,23 @@ autoplot.canned_epipred <- function(
218220
219221 autoplot(ewf , predictions ,
220222 .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 }}
222224 )
223225}
224226
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+
225240starts_with_impl <- function (x , vars ) {
226241 n <- nchar(x )
227242 x == substr(vars , 1 , n )
0 commit comments