|
| 1 | +#' Automatically plot an epi_df |
| 2 | +#' |
| 3 | +#' @param object An `epi_df` |
| 4 | +#' @param ... <[`tidy-select`][dplyr_tidy_select]> One or more unquoted |
| 5 | +#' expressions separated by commas. Variable names can be used as if they |
| 6 | +#' were positions in the data frame, so expressions like `x:y` can |
| 7 | +#' be used to select a range of variables. |
| 8 | +#' @param .color_by Which variables should determine the color(s) used to plot |
| 9 | +#' lines. Options include: |
| 10 | +#' * `all_keys` - the default uses the interaction of any key variables |
| 11 | +#' including the `geo_value` |
| 12 | +#' * `geo_value` - `geo_value` only |
| 13 | +#' * `other_keys` - any available keys that are not `geo_value` |
| 14 | +#' * `.response` - the numeric variables (same as the y-axis) |
| 15 | +#' * `all` - uses the interaction of all keys and numeric variables |
| 16 | +#' * `none` - no coloring aesthetic is applied |
| 17 | +#' @param .facet_by Similar to `.color_by` except that the default is to display |
| 18 | +#' each numeric variable on a separate facet |
| 19 | +#' @param .base_color Lines will be shown with this color. For example, with a |
| 20 | +#' single numeric variable and faceting by `geo_value`, all locations would |
| 21 | +#' share the same color line. |
| 22 | +#' @param .max_facets Cut down of the number of facets displayed. Especially |
| 23 | +#' useful for testing when there are many `geo_value`'s or keys. |
| 24 | +#' |
| 25 | +#' @return A ggplot object |
| 26 | +#' @export |
| 27 | +#' |
| 28 | +#' @examples |
| 29 | +#' autoplot(jhu_csse_daily_subset, cases, death_rate_7d_av) |
| 30 | +#' autoplot(jhu_csse_daily_subset, case_rate_7d_av, .facet_by = "geo_value") |
| 31 | +#' autoplot(jhu_csse_daily_subset, case_rate_7d_av, |
| 32 | +#' .color_by = "none", |
| 33 | +#' .facet_by = "geo_value" |
| 34 | +#' ) |
| 35 | +#' autoplot(jhu_csse_daily_subset, case_rate_7d_av, .color_by = "none", |
| 36 | +#' .base_color = "red", .facet_by = "geo_value") |
| 37 | +#' |
| 38 | +#' # .base_color specification won't have any effect due .color_by default |
| 39 | +#' autoplot(jhu_csse_daily_subset, case_rate_7d_av, |
| 40 | +#' .base_color = "red", .facet_by = "geo_value") |
| 41 | +autoplot.epi_df <- function( |
| 42 | + object, ..., |
| 43 | + .color_by = c("all_keys", "geo_value", "other_keys", ".response", "all", "none"), |
| 44 | + .facet_by = c(".response", "other_keys", "all_keys", "geo_value", "all", "none"), |
| 45 | + .base_color = "#3A448F", |
| 46 | + .max_facets = Inf) { |
| 47 | + .color_by <- match.arg(.color_by) |
| 48 | + .facet_by <- match.arg(.facet_by) |
| 49 | + |
| 50 | + assert(anyInfinite(.max_facets), assert_int(.max_facets), combine = "or") |
| 51 | + assert_character(.base_color, len = 1) |
| 52 | + |
| 53 | + key_cols <- key_colnames(object) |
| 54 | + non_key_cols <- setdiff(names(object), key_cols) |
| 55 | + geo_and_other_keys <- kill_time_value(key_cols) |
| 56 | + |
| 57 | + # --- check for numeric variables |
| 58 | + allowed <- purrr::map_lgl(object[non_key_cols], is.numeric) |
| 59 | + allowed <- allowed[allowed] |
| 60 | + if (length(allowed) == 0 && rlang::dots_n(...) == 0L) { |
| 61 | + cli::cli_abort("No numeric variables were available to plot automatically.", |
| 62 | + class = "epiprocess__no_numeric_vars_available") |
| 63 | + } |
| 64 | + vars <- tidyselect::eval_select(rlang::expr(c(...)), object) |
| 65 | + if (rlang::is_empty(vars)) { # find them automatically if unspecified |
| 66 | + vars <- tidyselect::eval_select(names(allowed)[1], object) |
| 67 | + cli::cli_warn( |
| 68 | + "Plot variable was unspecified. Automatically selecting {.var {names(allowed)[1]}}.", |
| 69 | + class = "epiprocess__unspecified_plot_var" |
| 70 | + ) |
| 71 | + } else { # if variables were specified, ensure that they are numeric |
| 72 | + ok <- names(vars) %in% names(allowed) |
| 73 | + if (!any(ok)) { |
| 74 | + cli::cli_abort( |
| 75 | + "None of the requested variables {.var {names(vars)}} are numeric.", |
| 76 | + class = "epiprocess__all_requested_vars_not_numeric" |
| 77 | + ) |
| 78 | + } else if (!all(ok)) { |
| 79 | + cli::cli_warn(c( |
| 80 | + "Only the requested variables {.var {names(vars)[ok]}} are numeric.", |
| 81 | + i = "`autoplot()` cannot display {.var {names(vars)[!ok]}}." |
| 82 | + ), |
| 83 | + class = "epiprocess__some_requested_vars_not_numeric") |
| 84 | + vars <- vars[ok] |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + # --- create a viable df to plot |
| 89 | + pos <- tidyselect::eval_select( |
| 90 | + rlang::expr(c("time_value", tidyselect::all_of(geo_and_other_keys), names(vars))), object |
| 91 | + ) |
| 92 | + if (length(vars) > 1) { |
| 93 | + object <- tidyr::pivot_longer( |
| 94 | + object[pos], tidyselect::all_of(names(vars)), |
| 95 | + values_to = ".response", |
| 96 | + names_to = ".response_name" |
| 97 | + ) |
| 98 | + } else { |
| 99 | + object <- dplyr::rename(object[pos], .response := !!names(vars)) |
| 100 | + } |
| 101 | + all_keys <- rlang::syms(as.list(geo_and_other_keys)) |
| 102 | + other_keys <- rlang::syms(as.list(setdiff(geo_and_other_keys, "geo_value"))) |
| 103 | + all_avail <- rlang::syms(as.list(c(geo_and_other_keys, ".response_name"))) |
| 104 | + |
| 105 | + object <- object %>% |
| 106 | + dplyr::mutate( |
| 107 | + .colours = switch(.color_by, |
| 108 | + all_keys = interaction(!!!all_keys, sep = "/"), |
| 109 | + geo_value = geo_value, |
| 110 | + other_keys = interaction(!!!other_keys, sep = "/"), |
| 111 | + all = interaction(!!!all_avail, sep = "/"), |
| 112 | + NULL |
| 113 | + ), |
| 114 | + .facets = switch(.facet_by, |
| 115 | + all_keys = interaction(!!!all_keys, sep = "/"), |
| 116 | + geo_value = as.factor(geo_value), |
| 117 | + other_keys = interaction(!!!other_keys, sep = "/"), |
| 118 | + all = interaction(!!!all_avail, sep = "/"), |
| 119 | + NULL |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + if (.max_facets < Inf && ".facets" %in% names(object)) { |
| 124 | + n_facets <- nlevels(object$.facets) |
| 125 | + if (n_facets > .max_facets) { |
| 126 | + top_n <- levels(as.factor(object$.facets))[seq_len(.max_facets)] |
| 127 | + object <- dplyr::filter(object, .facets %in% top_n) %>% |
| 128 | + dplyr::mutate(.facets = droplevels(.facets)) |
| 129 | + if (".colours" %in% names(object)) { |
| 130 | + object <- dplyr::mutate(object, .colours = droplevels(.colours)) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + p <- ggplot2::ggplot(object, ggplot2::aes(x = .data$time_value)) + |
| 136 | + ggplot2::theme_bw() |
| 137 | + |
| 138 | + if (".colours" %in% names(object)) { |
| 139 | + p <- p + ggplot2::geom_line( |
| 140 | + ggplot2::aes(y = .data$.response, colour = .data$.colours), |
| 141 | + key_glyph = "timeseries" |
| 142 | + ) + |
| 143 | + ggplot2::scale_colour_viridis_d(name = "") |
| 144 | + } else if (length(vars) > 1 && .color_by == ".response") { |
| 145 | + p <- p + |
| 146 | + ggplot2::geom_line(ggplot2::aes( |
| 147 | + y = .data$.response, colour = .data$.response_name |
| 148 | + )) + |
| 149 | + ggplot2::scale_colour_viridis_d(name = "") |
| 150 | + } else { # none |
| 151 | + p <- p + |
| 152 | + ggplot2::geom_line(ggplot2::aes(y = .data$.response), color = .base_color) |
| 153 | + } |
| 154 | + |
| 155 | + if (".facets" %in% names(object)) { |
| 156 | + p <- p + ggplot2::facet_wrap(~.facets, scales = "free_y") + |
| 157 | + ggplot2::ylab(names(vars)) |
| 158 | + if (.facet_by == "all") p <- p + ggplot2::ylab("") |
| 159 | + } else if ((length(vars) > 1 && .facet_by == ".response")) { |
| 160 | + p <- p + ggplot2::facet_wrap(~.response_name, scales = "free_y") + |
| 161 | + ggplot2::ylab("") |
| 162 | + } else { |
| 163 | + p <- p + ggplot2::ylab(names(vars)) |
| 164 | + } |
| 165 | + p |
| 166 | +} |
0 commit comments