R/qbinplotlist.R

Defines functions `[.qbinplotlist` print.qbinplotlist qbinplotlist

qbinplotlist <- function(l, x, ncols=NULL){
  structure(
    l,
    class="qbinplotlist",
    x = x,
    ncols = ncols
  )
}

#' @export
print.qbinplotlist <- function(x, ...){
  x[] <- lapply(x, function(p){
    p + ggplot2::scale_x_continuous(
      name = NULL,
      expand = c(0,0),
      labels = scales::label_percent()
    ) +
    ggplot2::theme_minimal() +
    ggplot2::theme(
      legend.position = "bottom"
    )
  })

  ncols <- attr(x, "ncols")
  title <- sprintf("Quantile binned: %s", attr(x, "x"))

  pw <- patchwork::wrap_plots(
    x,
    ncol = ncols,
    axes = 'collect',
    ...
  ) + patchwork::plot_annotation(
    title=title
  )
  print(pw, ...)
}

#' @export
`[.qbinplotlist` <- function(x, i){
  xs <- unclass(x)[i]
  class(xs) <- class(x)

  ncols <- attr(x, "ncols")
  if (is.numeric(ncols)){
    attr(xs, "ncols") <- min(length(xs), ncols)
  }

  attr(xs, "x") <- attr(x, 'x')
  xs
}

Try the qbinplots package in your browser

Any scripts or data that you put into this service are public.

qbinplots documentation built on April 3, 2025, 6:33 p.m.