R/viz.R

Defines functions plot_lines

Documented in plot_lines

#' A new way of getting line plots of all the variables.
#'
#' Plot all numeric variables against any existing base or zoo date variable.
#' You can specify which by using the `dc_order` argument.
#'
#' @param data The data to plot
#' @param dc_order Which date column should be used as the x-axis. Default is the first ordinal occurring date column.
#' @export
plot_lines <- function(data, dc_order = 1L) {
  is_yearmon <- function(x) identical("yearmon", class(x))
  stopifnot(any(are_date <- purrr::map_lgl(data, ~ lubridate::is.Date(.x) | is_yearmon(.x))))

  date_col <- which(are_date)[dc_order]

  stopifnot(any(are_numbers <- purrr::map_lgl(data, is.numeric)))

  num_cols <- which(are_numbers)

  data <- dplyr::select(data, date_col, num_cols)
  data <- tidyr::gather(data, ... = -c(date_col))

  names(data)[1] <- "date"

  gg <- ggplot2::ggplot(data, ggplot2::aes(date, value, group = key)) +
    ggplot2::geom_line() +
    ggplot2::facet_wrap(~key, scales = "free")

  return(gg)
}
seasmith/blg documentation built on May 29, 2019, 12:40 p.m.