Browse Source

updated scales

tags/0.8.0
boB Rudis 5 years ago
parent
commit
1a01bbae6e
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 4
      DESCRIPTION
  2. 5
      NEWS.md
  3. 289
      R/scales.r
  4. 54
      man/scale_x_percent.Rd

4
DESCRIPTION

@ -1,8 +1,8 @@
Package: hrbrthemes
Type: Package
Title: Additional Themes, Theme Components and Utilities for 'ggplot2'
Version: 0.7.1
Date: 2019-07-03
Version: 0.7.2
Date: 2019-08-08
Authors@R: c(
person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-5670-2640")),

5
NEWS.md

@ -1,3 +1,8 @@
# hrbrthemes 0.7.2
- Further enhancements to the `scale_[xy]_… functions` to support passing in
of all the `…_format()` params to make them more flexible
# hrbrthemes 0.7.1
- Small modification to percent scales to account for

289
R/scales.r

@ -27,13 +27,8 @@ is.formula <- function (x) { inherits(x, "formula") }
#' each major break)
#' - A numeric vector of positions
#' - A function that given the limits returns a vector of minor breaks.
#' @param labels One of:
#' - `NULL` for no labels
#' - `waiver()` for the default labels computed by the
#' transformation object
#' - A character vector giving labels (must be same length as `breaks`)
#' - A function that takes the breaks as input and returns labels
#' as output
#' @param labels Specifying overrides the default format (i.e. you really don't
#' want to do that). `NULL` means no labels.
#' @param limits A numeric vector of length two providing limits of the scale.
#' Use `NA` to refer to the existing minimum or maximum.
#' @param oob Function that handles limits outside of the scale limits
@ -50,89 +45,237 @@ is.formula <- function (x) { inherits(x, "formula") }
#' scales, "top" or "bottom" for horizontal scales
#' @param sec.axis specify a secondary axis
#' @export
scale_x_percent <- function (name = waiver(), breaks = waiver(), minor_breaks = waiver(),
labels = scales::percent_format(accuracy = 1), limits = NULL, expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity", position = "bottom",
sec.axis = waiver()) {
sc <- ggplot2::continuous_scale(c("x", "xmin", "xmax", "xend", "xintercept",
"xmin_final", "xmax_final", "xlower", "xmiddle", "xupper"),
"position_c", identity, name = name, breaks = breaks,
minor_breaks = minor_breaks, labels = labels, limits = limits,
expand = expand, oob = oob, na.value = na.value, trans = trans,
guide = "none", position = position, super = ScaleContinuousPosition)
scale_x_percent <- function(name = waiver(), breaks = waiver(),
minor_breaks = waiver(),
limits = NULL, expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity",
position = "bottom", sec.axis = waiver(), labels,
accuracy = 1, scale = 100, prefix = "", suffix = "%",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...) {
if (missing(labels)) {
scales::percent_format(
accuracy = accuracy,
scale = scale,
prefix = prefix,
suffix = suffix,
big.mark = big.mark,
decimal.mark = decimal.mark,
trim = trim,
...
) -> labels
}
ggplot2::continuous_scale(
aesthetics = c(
"x", "xmin", "xmax", "xend", "xintercept", "xmin_final",
"xmax_final", "xlower", "xmiddle", "xupper"
),
scale_name = "position_c",
palette = identity,
name = name,
breaks = breaks,
minor_breaks = minor_breaks,
labels = labels,
limits = limits,
expand = expand,
oob = oob,
na.value = na.value,
trans = trans,
guide = "none",
position = position,
super = ScaleContinuousPosition
) -> sc
if (!is.waive(sec.axis)) {
if (is.formula(sec.axis))
sec.axis <- sec_axis(sec.axis)
if (!is.sec_axis(sec.axis))
stop("Secondary axes must be specified using 'sec_axis()'")
if (is.formula(sec.axis)) sec.axis <- sec_axis(sec.axis)
if (!is.sec_axis(sec.axis)) stop("Secondary axes must be specified using 'sec_axis()'")
sc$secondary.axis <- sec.axis
}
sc
}
#' @rdname scale_x_percent
#' @export
scale_y_percent <- function (name = waiver(), breaks = waiver(), minor_breaks = waiver(),
labels = scales::percent_format(accuracy = 1), limits = NULL, expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity", position = "left",
sec.axis = waiver()) {
sc <- ggplot2::continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept",
"ymin_final", "ymax_final", "lower", "middle", "upper"),
"position_c", identity, name = name, breaks = breaks,
minor_breaks = minor_breaks, labels = labels, limits = limits,
expand = expand, oob = oob, na.value = na.value, trans = trans,
guide = "none", position = position, super = ScaleContinuousPosition)
if (!is.waive(sec.axis)) {
if (is.formula(sec.axis))
sec.axis <- ggplot2::sec_axis(sec.axis)
if (!is.sec_axis(sec.axis))
stop("Secondary axes must be specified using 'sec_axis()'")
sc$secondary.axis <- sec.axis
}
sc
scale_y_percent <- function(name = waiver(), breaks = waiver(),
minor_breaks = waiver(),
limits = NULL, expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity",
position = "left", sec.axis = waiver(), labels,
accuracy = 1, scale = 100, prefix = "", suffix = "%",
big.mark = " ", decimal.mark = ".", trim = TRUE, ...) {
if (missing(labels)) {
scales::percent_format(
accuracy = accuracy,
scale = scale,
prefix = prefix,
suffix = suffix,
big.mark = big.mark,
decimal.mark = decimal.mark,
trim = trim,
...
) -> labels
}
ggplot2::continuous_scale(
aesthetics = c(
"y", "ymin", "ymax", "yend", "yintercept",
"ymin_final", "ymax_final", "lower", "middle", "upper"
),
scale_name = "position_c",
palette = identity,
name = name,
breaks = breaks,
minor_breaks = minor_breaks,
labels = labels,
limits = limits,
expand = expand,
oob = oob,
na.value = na.value,
trans = trans,
guide = "none",
position = position,
super = ScaleContinuousPosition
) -> sc
if (!is.waive(sec.axis)) {
if (is.formula(sec.axis)) sec.axis <- ggplot2::sec_axis(sec.axis)
if (!is.sec_axis(sec.axis)) stop("Secondary axes must be specified using 'sec_axis()'")
sc$secondary.axis <- sec.axis
}
sc
}
#' @rdname scale_x_percent
#' @param accuracy,scale,prefix,suffix,big.mark,decimal.mark,trim See
#' [scales::comma_format()] or [scales::percent_format()]
#' @param ... passed on to [scales::comma_format()] or [scales::percent_format()]
#' @export
scale_x_comma <- function (name = waiver(), breaks = waiver(), minor_breaks = waiver(),
labels = scales::comma_format(), limits = NULL, expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity", position = "bottom",
sec.axis = waiver()) {
sc <- ggplot2::continuous_scale(c("x", "xmin", "xmax", "xend", "xintercept",
"xmin_final", "xmax_final", "xlower", "xmiddle", "xupper"),
"position_c", identity, name = name, breaks = breaks,
minor_breaks = minor_breaks, labels = labels, limits = limits,
expand = expand, oob = oob, na.value = na.value, trans = trans,
guide = "none", position = position, super = ScaleContinuousPosition)
scale_x_comma <- function(name = waiver(), breaks = waiver(),
minor_breaks = waiver(),
limits = NULL,
expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity",
position = "bottom", sec.axis = waiver(), labels,
accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = ",", decimal.mark = ".", trim = TRUE,
...) {
if (missing(labels)) {
scales::comma_format(
accuracy = accuracy,
scale = scale,
prefix = prefix,
suffix = suffix,
big.mark = big.mark,
decimal.mark = decimal.mark,
trim = trim,
...
) -> labels
}
ggplot2::continuous_scale(
aesthetics = c(
"x", "xmin", "xmax", "xend", "xintercept",
"xmin_final", "xmax_final",
"xlower", "xmiddle", "xupper"
),
scale_name = "position_c",
palette = identity,
name = name,
breaks = breaks,
minor_breaks = minor_breaks,
labels = labels,
limits = limits,
expand = expand,
oob = oob,
na.value = na.value,
trans = trans,
guide = "none",
position = position,
super = ScaleContinuousPosition
) -> sc
if (!is.waive(sec.axis)) {
if (is.formula(sec.axis))
sec.axis <- ggplot2::sec_axis(sec.axis)
if (!is.sec_axis(sec.axis))
stop("Secondary axes must be specified using 'sec_axis()'")
if (is.formula(sec.axis)) sec.axis <- ggplot2::sec_axis(sec.axis)
if (!is.sec_axis(sec.axis)) stop("Secondary axes must be specified using 'sec_axis()'")
sc$secondary.axis <- sec.axis
}
sc
}
}
#' @rdname scale_x_percent
#' @export
scale_y_comma <- function (name = waiver(), breaks = waiver(), minor_breaks = waiver(),
labels = scales::comma_format(), limits = NULL, expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity", position = "left",
sec.axis = waiver()) {
sc <- ggplot2::continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept",
"ymin_final", "ymax_final", "lower", "middle", "upper"),
"position_c", identity, name = name, breaks = breaks,
minor_breaks = minor_breaks, labels = labels, limits = limits,
expand = expand, oob = oob, na.value = na.value, trans = trans,
guide = "none", position = position, super = ScaleContinuousPosition)
if (!is.waive(sec.axis)) {
if (is.formula(sec.axis))
sec.axis <- ggplot2::sec_axis(sec.axis)
if (!is.sec_axis(sec.axis))
stop("Secondary axes must be specified using 'sec_axis()'")
sc$secondary.axis <- sec.axis
}
sc
}
scale_y_comma <- function(name = waiver(),
breaks = waiver(),
minor_breaks = waiver(),
limits = NULL, expand = c(0.01,0), oob = censor,
na.value = NA_real_, trans = "identity",
position = "left", sec.axis = waiver(), labels,
accuracy = 1, scale = 1, prefix = "", suffix = "",
big.mark = ",", decimal.mark = ".", trim = TRUE,
...) {
if (missing(labels)) {
scales::comma_format(
accuracy = accuracy,
scale = scale,
prefix = prefix,
suffix = suffix,
big.mark = big.mark,
decimal.mark = decimal.mark,
trim = trim,
...
) -> labels
}
ggplot2::continuous_scale(
aesthetics = c(
"y", "ymin", "ymax", "yend", "yintercept",
"ymin_final", "ymax_final",
"lower", "middle", "upper"
),
scale_name = "position_c",
palette = identity,
name = name,
breaks = breaks,
minor_breaks = minor_breaks,
labels = labels,
limits = limits,
expand = expand,
oob = oob,
na.value = na.value,
trans = trans,
guide = "none",
position = position,
super = ScaleContinuousPosition
) -> sc
if (!is.waive(sec.axis)) {
if (is.formula(sec.axis)) sec.axis <- ggplot2::sec_axis(sec.axis)
if (!is.sec_axis(sec.axis)) stop("Secondary axes must be specified using 'sec_axis()'")
sc$secondary.axis <- sec.axis
}
sc
}

54
man/scale_x_percent.Rd

@ -8,28 +8,32 @@
\title{X & Y scales with opinionated pre-sets for percent & comma label formats}
\usage{
scale_x_percent(name = waiver(), breaks = waiver(),
minor_breaks = waiver(), labels = scales::percent_format(accuracy =
1), limits = NULL, expand = c(0.01, 0), oob = censor,
na.value = NA_real_, trans = "identity", position = "bottom",
sec.axis = waiver())
minor_breaks = waiver(), limits = NULL, expand = c(0.01, 0),
oob = censor, na.value = NA_real_, trans = "identity",
position = "bottom", sec.axis = waiver(), labels, accuracy = 1,
scale = 100, prefix = "", suffix = "\%", big.mark = " ",
decimal.mark = ".", trim = TRUE, ...)
scale_y_percent(name = waiver(), breaks = waiver(),
minor_breaks = waiver(), labels = scales::percent_format(accuracy =
1), limits = NULL, expand = c(0.01, 0), oob = censor,
na.value = NA_real_, trans = "identity", position = "left",
sec.axis = waiver())
minor_breaks = waiver(), limits = NULL, expand = c(0.01, 0),
oob = censor, na.value = NA_real_, trans = "identity",
position = "left", sec.axis = waiver(), labels, accuracy = 1,
scale = 100, prefix = "", suffix = "\%", big.mark = " ",
decimal.mark = ".", trim = TRUE, ...)
scale_x_comma(name = waiver(), breaks = waiver(),
minor_breaks = waiver(), labels = scales::comma_format(),
limits = NULL, expand = c(0.01, 0), oob = censor,
na.value = NA_real_, trans = "identity", position = "bottom",
sec.axis = waiver())
minor_breaks = waiver(), limits = NULL, expand = c(0.01, 0),
oob = censor, na.value = NA_real_, trans = "identity",
position = "bottom", sec.axis = waiver(), labels, accuracy = 1,
scale = 1, prefix = "", suffix = "", big.mark = ",",
decimal.mark = ".", trim = TRUE, ...)
scale_y_comma(name = waiver(), breaks = waiver(),
minor_breaks = waiver(), labels = scales::comma_format(),
limits = NULL, expand = c(0.01, 0), oob = censor,
na.value = NA_real_, trans = "identity", position = "left",
sec.axis = waiver())
minor_breaks = waiver(), limits = NULL, expand = c(0.01, 0),
oob = censor, na.value = NA_real_, trans = "identity",
position = "left", sec.axis = waiver(), labels, accuracy = 1,
scale = 1, prefix = "", suffix = "", big.mark = ",",
decimal.mark = ".", trim = TRUE, ...)
}
\arguments{
\item{name}{The name of the scale. Used as axis or legend title. If
@ -56,16 +60,6 @@ each major break)
\item A function that given the limits returns a vector of minor breaks.
}}
\item{labels}{One of:
\itemize{
\item \code{NULL} for no labels
\item \code{waiver()} for the default labels computed by the
transformation object
\item A character vector giving labels (must be same length as \code{breaks})
\item A function that takes the breaks as input and returns labels
as output
}}
\item{limits}{A numeric vector of length two providing limits of the scale.
Use \code{NA} to refer to the existing minimum or maximum.}
@ -87,6 +81,14 @@ object itself. Built-in transformations include "asn", "atanh",
scales, "top" or "bottom" for horizontal scales}
\item{sec.axis}{specify a secondary axis}
\item{labels}{Specifying overrides the default format (i.e. you really don't
want to do that). \code{NULL} means no labels.}
\item{accuracy, scale, prefix, suffix, big.mark, decimal.mark, trim}{See
[scales::comma_format()] or [scales::percent_format()]}
\item{...}{passed on to [scales::comma_format()] or [scales::percent_format()]}
}
\description{
The \code{_comma} ones set comma format for axis text and \code{expand=c(0,0)} (you need to set limits).

Loading…
Cancel
Save