R/demo-ggplot.R

Defines functions demo_ggplot demo_number

Documented in demo_number

#' ggplot2 demo
#'
#' @param x a numeric
#' @param scale_name scale name
#' @param ... further arguments to be passed to the scale
#'
#' @return A `ggplot` object, invisibly printed as a demo of the requested
#'     scale.
#'
#' @export
#'
#' @examples
#' demo_number(c(32, 212), scale_name = "number_locale")
demo_number <- function(x, scale_name, ...) {
  demo_ggplot(x, paste("scale", "x", scale_name, sep = "_"), ...)
}

demo_ggplot <- function(x, scale_name, ...) {
  call <- substitute(list(...))
  call[[1]] <- as.name(scale_name)
  message(paste0(deparse(call), collapse = ""))
  scale <- getExportedValue("countryscales", scale_name)
  df <- data.frame(x = x, stringsAsFactors = FALSE)
  ggplot2::ggplot(df, ggplot2::aes(x, 1)) +
    ggplot2::geom_blank() +
    scale(NULL, ...) +
    ggplot2::scale_y_continuous(NULL,
      breaks = NULL
    ) +
    ggplot2::theme(aspect.ratio = 1 / 5)
}

Try the countryscales package in your browser

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

countryscales documentation built on Sept. 29, 2026, 5:09 p.m.