Skip to content

Commit 0e86519

Browse files
authored
Merge pull request #459 from cmu-delphi/autoplot-fixes
Autoplot fixes
2 parents 5721d41 + bd76bf6 commit 0e86519

File tree

6 files changed

+61
-27
lines changed

6 files changed

+61
-27
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: epipredict
22
Title: Basic epidemiology forecasting methods
3-
Version: 0.1.14
3+
Version: 0.1.15
44
Authors@R: c(
55
person("Daniel J.", "McDonald", , "[email protected]", role = c("aut", "cre")),
66
person("Ryan", "Tibshirani", , "[email protected]", role = "aut"),
@@ -31,7 +31,7 @@ Imports:
3131
checkmate,
3232
cli,
3333
dplyr,
34-
epiprocess (>= 0.10.4),
34+
epiprocess (>= 0.11.2),
3535
generics,
3636
ggplot2,
3737
glue,

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ S3method(forecast,epi_workflow)
4747
S3method(key_colnames,epi_workflow)
4848
S3method(key_colnames,recipe)
4949
S3method(mean,quantile_pred)
50+
S3method(plot,canned_epipred)
51+
S3method(plot,epi_workflow)
5052
S3method(predict,epi_workflow)
5153
S3method(predict,flatline)
5254
S3method(prep,check_enough_data)

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.0.x will indicat
2323
- Rename `check_enough_train_data()` to `check_enough_data()`, and generalize it
2424
enough to use as a check on either training or testing.
2525
- Add check for enough data to predict in `arx_forecaster()`
26+
- Adds the `.facet_filter` option in `epiprocess::autoplot()` (cmu-delphi/epiprocess#647).
2627

2728
## Improvements
2829

R/autoplot.R

+30-15
Original file line numberDiff line numberDiff line change
@@ -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"))
7979
NULL
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+
225240
starts_with_impl <- function(x, vars) {
226241
n <- nchar(x)
227242
x == substr(vars, 1, n)

R/step_climate.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ prep.step_climate <- function(x, training, info = NULL, ...) {
266266

267267
fn <- switch(x$center_method,
268268
mean = function(x, w) stats::weighted.mean(x, w, na.rm = TRUE),
269-
median = function(x, w) median(x, na.rm = TRUE)
269+
median = function(x, w) stats::median(x, na.rm = TRUE)
270270
)
271271
# suppose it's week 52, and there is no week 53 this year; then
272272
# as originally written for 1 week ahead this grabs from week 52+1 %% 53

man/autoplot-epipred.Rd

+25-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)