#' Customised scale_ functions
#'
#' This uses scale_* functions from ggplot2 and creates variations for
#' comma, percent and dollar scales. Moves the axis to 0
#' @param accuracy Accuracy level for scale. Defaults to 5L
#' @param ... Passed to ggplot2 scale function
#' @keywords scale
#' @export
#' @examples
#' scale_fill_pct()
#' scale_fill_pct(accuracy = 0.1)
#' scale_fill_comma()
#' scale_fill_dollar()
#' scale_x_pct()
#' scale_x_pct(accuracy = 0.1)
#' scale_x_comma()
#' scale_x_dollar()
#' scale_y_pct()
#' scale_y_pct(accuracy = 0.1)
#' scale_y_comma()
#' scale_y_dollar()
#-------------------------------------------------------------------------------
# scale_fill
#-------------------------------------------------------------------------------
scale_fill_pct <- function(accuracy = 5L, ...) {
scale_fill_continuous(
label = scales::percent_format(accuracy = accuracy),
breaks = pretty_breaks(),
...
)
}
scale_fill_comma <- function(...) {
scale_fill_continuous(
label = scales::comma_format(accuracy = 5L),
breaks = pretty_breaks(),
...
)
}
scale_fill_dollar <- function(...) {
scale_fill_continuous(
label = scales::dollar_format(accuracy = 5L),
breaks = pretty_breaks(),
...
)
}
#-------------------------------------------------------------------------------
# scale_x
#-------------------------------------------------------------------------------
scale_x_pct <- function(accuracy = 5L, ...) {
scale_x_continuous(
label = scales::percent_format(accuracy = accuracy),
breaks = pretty_breaks(),
expand = c(0, 0),
...
)
}
scale_x_comma <- function(...) {
scale_x_continuous(
label = scales::comma_format(accuracy = 5L),
breaks = pretty_breaks(),
expand = c(0, 0),
...
)
}
scale_x_dollar <- function(...) {
scale_x_continuous(
label = scales::dollar_format(accuracy = 5L),
breaks = pretty_breaks(),
expand = c(0, 0),
...
)
}
#-------------------------------------------------------------------------------
# scale_y
#-------------------------------------------------------------------------------
scale_y_pct <- function(accuracy = 5L, ...) {
scale_y_continuous(
label = scales::percent_format(accuracy = accuracy),
breaks = pretty_breaks(),
expand = c(0, 0),
...
)
}
scale_y_comma <- function(...) {
scale_y_continuous(
label = scales::comma_format(accuracy = 5L),
breaks = pretty_breaks(),
expand = c(0, 0),
...
)
}
scale_y_dollar <- function(...) {
scale_y_continuous(
label = scales::dollar_format(accuracy = 5L),
breaks = pretty_breaks(),
expand = c(0, 0),
...
)
}
#-------------------------------------------------------------------------------
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.