diff --git a/DESCRIPTION b/DESCRIPTION index 87255c4..8ee528e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: gghdx Title: HDX Theme, Scales, and Other Conveniences for 'ggplot2' -Version: 0.1.2 +Version: 0.1.2.9000 Authors@R: person( given = "Seth", diff --git a/NAMESPACE b/NAMESPACE index b2f6676..b0669fe 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,6 +18,7 @@ export(hdx_pal_gray) export(hdx_pal_mint) export(hdx_pal_sapphire) export(hdx_pal_tomato) +export(label_number_hdx) export(load_source_sans_3) export(scale_color_gradient2_hdx) export(scale_color_gradient_hdx) diff --git a/NEWS.md b/NEWS.md index 5bf37d3..5cdd917 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# Development + +- Implement `label_number_hdx()` function to improve numeric scales labeling +to match the data visualization guidelines. + # gghdx 0.1.2 * Use anonymous function format `function()` in `hdx_display_pal()`. diff --git a/R/hdx_display_pal.R b/R/hdx_display_pal.R index 687cb3e..b90516e 100644 --- a/R/hdx_display_pal.R +++ b/R/hdx_display_pal.R @@ -64,7 +64,7 @@ hdx_display_pal <- function( ) + ggplot2::geom_tile( color = "white", - lwd = 1 + linewidth = 1 ) + theme_hdx( base_family = base_family diff --git a/R/label_number_hdx.R b/R/label_number_hdx.R new file mode 100644 index 0000000..e503820 --- /dev/null +++ b/R/label_number_hdx.R @@ -0,0 +1,100 @@ +#' Label numbers in HDX key figures style +#' +#' Formats numeric vector in the Centre style for key figures, which abbreviates +#' numbers 1,000 and above to X.YK, 10,000 and above to XYK, 100,000 and above +#' to XYZK, and the same for 1,000,000 and above, replacing the K with an M, and +#' the same for B. Details of the data viz style can be found in the +#' [visualization guidelines](https://data.humdata.org/dataviz-guide/dataviz-elements/#/data-visualization/text) +#' +#' Just for continuity, values are labeled with T for trillion, and that is the +#' maximum formatting available, anything above the trillions will continue to be +#' truncated to report in the trillions. +#' +#' Deals with negative values in case those ever need to be formatted in similar +#' manners. Also ensures that rounding is performed so numbers look correct. +#' Not to be used for percents, which should just use [scales::label_percent()]. +#' +#' Designed like the `scales::` family of label functions, the return value of +#' `label_number_hdx` is a function, based on the `additional_prefix`. So you +#' should pass it in to `scales_...()` `labels` parameter in the same way as +#' `scales_...()` +#' +#' @param additional_prefix Additional prefix to add to string, that will come +#' between `sign_prefix` and the number. For example, `"$"` could produce a +#' return value of `-$1.1K`. +#' +#' @returns Returns a "labelling" function, in the same way as `scales::label_...()` +#' functions work, i.e. a function that takes `x` and returns a labelled character +#' vector of `length(x)`. +#' +#' @examples +#' library(ggplot2) +#' +#' # discrete scaling +#' p <- ggplot(txhousing) + +#' geom_point( +#' aes( +#' x = median, +#' y = volume +#' ) +#' ) +#' +#' p +#' +#' p + +#' scale_x_continuous( +#' labels = label_number_hdx("$") +#' ) + +#' scale_y_continuous( +#' labels = label_number_hdx() +#' ) +#' +#' @export +label_number_hdx <- function(additional_prefix = "") { + if (!is.character(additional_prefix) || length(additional_prefix) > 1) { + stop( + "`additional_prefix` should be a character string of length 1, not a ", + class(additional_prefix)[1], + ".", + call. = FALSE + ) + } + + function(x) format_number_hdx(x = x, additional_prefix = additional_prefix) +} + +#' Format numbers in HDX style +#' +#' Does the formatting found in `label_number_hdx`. +#' +#' @inherit label_number_hdx params description details +#' +#' @param x Numeric vector to format +#' +#' @returns Character vector of formatted strings +format_number_hdx <- function(x, additional_prefix) { + if (!is.numeric(x)) { + stop( + "`x` must be numeric, not class ", + class(x)[1], + ".", + call. = FALSE + ) + } + + sign_x <- sign(x) + sign_prefix <- ifelse(sign_x == -1, "-", "") + abs_x <- abs(x) + + dplyr::case_when( + abs_x < 1e3 ~ paste0(sign_prefix, additional_prefix, round(abs_x, digits = 0)), + abs_x < 1e4 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e3, digits = 1), "K"), + abs_x < 1e6 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e3, digits = 0), "K"), + abs_x < 1e7 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e6, digits = 1), "M"), + abs_x < 1e9 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e6, digits = 0), "M"), + abs_x < 1e10 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e9, digits = 1), "B"), + abs_x < 1e12 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e9, digits = 0), "B"), + abs_x < 1e13 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e12, digits = 1), "T"), + abs_x >= 1e13 ~ paste0(sign_prefix, additional_prefix, round(abs_x / 1e12, digits = 0), "T") + ) +} diff --git a/R/scale_hdx.R b/R/scale_hdx.R index 4c8dba0..368258c 100644 --- a/R/scale_hdx.R +++ b/R/scale_hdx.R @@ -53,9 +53,8 @@ #' @export scale_color_hdx_discrete <- function(...) { ggplot2::discrete_scale( - "colour", - "hdx", - hdx_pal_discrete(), + aesthetics = "colour", + palette = hdx_pal_discrete(), na.value = hdx_hex("gray-light"), ... ) @@ -69,9 +68,8 @@ scale_colour_hdx_discrete <- scale_color_hdx_discrete #' @export scale_color_hdx_gray <- function(...) { ggplot2::discrete_scale( - "colour", - "hdx", - hdx_pal_gray(), + aesthetics = "colour", + palette = hdx_pal_gray(), na.value = hdx_hex("tomato-hdx"), ... ) @@ -93,9 +91,8 @@ scale_color_hdx_grey <- scale_color_hdx_gray #' @export scale_color_hdx_mint <- function(...) { ggplot2::discrete_scale( - "colour", - "hdx", - hdx_pal_mint(), + aesthetics = "colour", + palette = hdx_pal_mint(), na.value = hdx_hex("gray-light"), ... ) @@ -109,9 +106,8 @@ scale_colour_hdx_mint <- scale_color_hdx_mint #' @export scale_color_hdx_sapphire <- function(...) { ggplot2::discrete_scale( - "colour", - "hdx", - hdx_pal_sapphire(), + aesthetic = "colour", + palette = hdx_pal_sapphire(), na.value = hdx_hex("gray-light"), ... ) @@ -125,9 +121,8 @@ scale_colour_hdx_sapphire <- scale_color_hdx_sapphire #' @export scale_color_hdx_tomato <- function(...) { ggplot2::discrete_scale( - "colour", - "hdx", - hdx_pal_tomato(), + aesthetics = "colour", + palette = hdx_pal_tomato(), na.value = hdx_hex("gray-light"), ... ) @@ -141,9 +136,8 @@ scale_colour_hdx_tomato <- scale_color_hdx_tomato #' @export scale_fill_hdx_discrete <- function(...) { ggplot2::discrete_scale( - "fill", - "hdx", - hdx_pal_discrete(), + aesthetics = "fill", + palette = hdx_pal_discrete(), na.value = hdx_hex("gray-light"), ... ) @@ -153,9 +147,8 @@ scale_fill_hdx_discrete <- function(...) { #' @export scale_fill_hdx_gray <- function(...) { ggplot2::discrete_scale( - "fill", - "hdx", - hdx_pal_gray(), + aesthetics = "fill", + palette = hdx_pal_gray(), na.value = hdx_hex("tomato-hdx"), ... ) @@ -169,9 +162,8 @@ scale_fill_hdx_grey <- scale_fill_hdx_gray #' @export scale_fill_hdx_mint <- function(...) { ggplot2::discrete_scale( - "fill", - "hdx", - hdx_pal_mint(), + aesthetics = "fill", + palette = hdx_pal_mint(), na.value = hdx_hex("gray-light"), ... ) @@ -181,9 +173,8 @@ scale_fill_hdx_mint <- function(...) { #' @export scale_fill_hdx_sapphire <- function(...) { ggplot2::discrete_scale( - "fill", - "hdx", - hdx_pal_sapphire(), + aesthetics = "fill", + palette = hdx_pal_sapphire(), na.value = hdx_hex("gray-light"), ... ) @@ -193,9 +184,8 @@ scale_fill_hdx_sapphire <- function(...) { #' @export scale_fill_hdx_tomato <- function(...) { ggplot2::discrete_scale( - "fill", - "hdx", - hdx_pal_tomato(), + aesthetics = "fill", + palette = hdx_pal_tomato(), na.value = hdx_hex("gray-light"), ... ) @@ -206,7 +196,6 @@ scale_fill_hdx_tomato <- function(...) { scale_fill_gradient_hdx <- function(...) { ggplot2::continuous_scale( aesthetics = "fill", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = hdx_hex("tomato-hdx"), high = hdx_hex("sapphire-hdx") @@ -221,7 +210,6 @@ scale_fill_gradient_hdx <- function(...) { scale_fill_gradient_hdx_sapphire <- function(...) { ggplot2::continuous_scale( aesthetics = "fill", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = "white", high = hdx_hex("sapphire-hdx") @@ -236,7 +224,6 @@ scale_fill_gradient_hdx_sapphire <- function(...) { scale_fill_gradient_hdx_mint <- function(...) { ggplot2::continuous_scale( aesthetics = "fill", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = "white", high = hdx_hex("mint-hdx") @@ -251,7 +238,6 @@ scale_fill_gradient_hdx_mint <- function(...) { scale_fill_gradient_hdx_tomato <- function(...) { ggplot2::continuous_scale( aesthetics = "fill", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = "white", high = hdx_hex("tomato-hdx") @@ -266,7 +252,6 @@ scale_fill_gradient_hdx_tomato <- function(...) { scale_color_gradient_hdx <- function(...) { ggplot2::continuous_scale( aesthetics = "color", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = hdx_hex("tomato-hdx"), high = hdx_hex("sapphire-hdx") @@ -285,7 +270,6 @@ scale_colour_gradient_hdx <- scale_color_gradient_hdx scale_color_gradient_hdx_sapphire <- function(...) { ggplot2::continuous_scale( aesthetics = "color", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = "white", high = hdx_hex("sapphire-hdx") @@ -304,7 +288,6 @@ scale_colour_gradient_hdx_sapphire <- scale_color_gradient_hdx_sapphire scale_color_gradient_hdx_mint <- function(...) { ggplot2::continuous_scale( aesthetics = "color", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = "white", high = hdx_hex("mint-hdx") @@ -323,7 +306,6 @@ scale_colour_gradient_hdx_mint <- scale_color_gradient_hdx_mint scale_color_gradient_hdx_tomato <- function(...) { ggplot2::continuous_scale( aesthetics = "color", - scale_name = "hdx", palette = scales::seq_gradient_pal( low = "white", high = hdx_hex("tomato-hdx") @@ -342,7 +324,6 @@ scale_colour_gradient_hdx_tomato <- scale_color_gradient_hdx_tomato scale_color_gradient2_hdx <- function(...) { ggplot2::continuous_scale( aesthetics = "color", - scale_name = "hdx", palette = scales::div_gradient_pal( low = hdx_hex("tomato-hdx"), mid = hdx_hex("gray-white"), @@ -362,7 +343,6 @@ scale_colour_gradient2_hdx <- scale_color_gradient2_hdx scale_fill_gradient2_hdx <- function(...) { ggplot2::continuous_scale( aesthetics = "fill", - scale_name = "hdx", palette = scales::div_gradient_pal( low = hdx_hex("tomato-hdx"), mid = hdx_hex("gray-white"), diff --git a/README.Rmd b/README.Rmd index ad614e6..43bc2fb 100644 --- a/README.Rmd +++ b/README.Rmd @@ -41,6 +41,8 @@ starting from the y-axis. * `gghdx()` ensures plot for the session use HDX defaults for color and fill scales, uses `theme_hdx()` for all plots, and applies `scale_color_hdx_...()` and `scale_fill_hdx_...()` +* `label_number_hdx()` supplements the `scales::label_...()` series of functions +to create labels for numbers in the HDX style. ## Installation diff --git a/README.md b/README.md index 747f693..6f3592c 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,8 @@ features. The key functionalities are: - `gghdx()` ensures plot for the session use HDX defaults for color and fill scales, uses `theme_hdx()` for all plots, and applies `scale_color_hdx_...()` and `scale_fill_hdx_...()` +- `label_number_hdx()` supplements the `scales::label_...()` series of + functions to create labels for numbers in the HDX style. ## Installation @@ -187,7 +189,8 @@ the visual guide using the theme and color scales in the package. The inbuilt data `gghdx::df_covid` has aggregated COVID data we can use to mirror this plot. To make the data start at the y-axis, we can use -`scale_y_continuous_hdx()` which sets `expand = c(0, 0)` by default. +`scale_y_continuous_hdx()` which sets `expand = c(0, 0)` by default, and +the `label_number_hdx()` function to create custom labels. ``` r p_blue <- ggplot( @@ -203,11 +206,7 @@ p_blue <- ggplot( fill = hdx_hex("sapphire-hdx") # use sapphire for fill ) + scale_y_continuous_hdx( - labels = scales::label_number( - accuracy = 1, - scale = 1 / 1000000, - suffix = "M" - ) + labels = label_number_hdx() ) + scale_x_date( date_breaks = "1 month", diff --git a/man/figures/README-covid-match-1.png b/man/figures/README-covid-match-1.png index 486baa1..819f655 100644 Binary files a/man/figures/README-covid-match-1.png and b/man/figures/README-covid-match-1.png differ diff --git a/man/figures/README-covid-match-2.png b/man/figures/README-covid-match-2.png index 7f8ac76..8fafcf8 100644 Binary files a/man/figures/README-covid-match-2.png and b/man/figures/README-covid-match-2.png differ diff --git a/man/figures/README-extrafont-1.png b/man/figures/README-extrafont-1.png index 92b19a0..37f5f6d 100644 Binary files a/man/figures/README-extrafont-1.png and b/man/figures/README-extrafont-1.png differ diff --git a/man/figures/README-gghdx-1.png b/man/figures/README-gghdx-1.png index e71702e..50ea072 100644 Binary files a/man/figures/README-gghdx-1.png and b/man/figures/README-gghdx-1.png differ diff --git a/man/figures/README-intro-hdx-1.png b/man/figures/README-intro-hdx-1.png index 77acf92..0008320 100644 Binary files a/man/figures/README-intro-hdx-1.png and b/man/figures/README-intro-hdx-1.png differ diff --git a/man/figures/README-intro-plot-1.png b/man/figures/README-intro-plot-1.png index 9e5676e..a17aca3 100644 Binary files a/man/figures/README-intro-plot-1.png and b/man/figures/README-intro-plot-1.png differ diff --git a/man/figures/README-intro-ramp-1.png b/man/figures/README-intro-ramp-1.png index 6cb65a5..1c6b035 100644 Binary files a/man/figures/README-intro-ramp-1.png and b/man/figures/README-intro-ramp-1.png differ diff --git a/man/format_number_hdx.Rd b/man/format_number_hdx.Rd new file mode 100644 index 0000000..12ddd5c --- /dev/null +++ b/man/format_number_hdx.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/label_number_hdx.R +\name{format_number_hdx} +\alias{format_number_hdx} +\title{Format numbers in HDX style} +\usage{ +format_number_hdx(x, additional_prefix) +} +\arguments{ +\item{x}{Numeric vector to format} + +\item{additional_prefix}{Additional prefix to add to string, that will come +between \code{sign_prefix} and the number. For example, \code{"$"} could produce a +return value of \verb{-$1.1K}.} +} +\value{ +Character vector of formatted strings +} +\description{ +Does the formatting found in \code{label_number_hdx}. +} +\details{ +Just for continuity, values are labeled with T for trillion, and that is the +maximum formatting available, anything above the trillions will continue to be +truncated to report in the trillions. + +Deals with negative values in case those ever need to be formatted in similar +manners. Also ensures that rounding is performed so numbers look correct. +Not to be used for percents, which should just use \code{\link[scales:label_percent]{scales::label_percent()}}. + +Designed like the \verb{scales::} family of label functions, the return value of +\code{label_number_hdx} is a function, based on the \code{additional_prefix}. So you +should pass it in to \code{scales_...()} \code{labels} parameter in the same way as +\code{scales_...()} +} diff --git a/man/geom_text_family.Rd b/man/geom_text_family.Rd index 6ca4526..d114c0e 100644 --- a/man/geom_text_family.Rd +++ b/man/geom_text_family.Rd @@ -61,19 +61,59 @@ the plot data. The return value must be a \code{data.frame}, and will be used as the layer data. A \code{function} can be created from a \code{formula} (e.g. \code{~ head(.x, 10)}).} -\item{stat}{The statistical transformation to use on the data for this -layer, either as a \code{ggproto} \code{Geom} subclass or as a string naming the -stat stripped of the \code{stat_} prefix (e.g. \code{"count"} rather than -\code{"stat_count"})} - -\item{position}{Position adjustment, either as a string, or the result of -a call to a position adjustment function. Cannot be jointly specified with -\code{nudge_x} or \code{nudge_y}.} - -\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}. These are -often aesthetics, used to set an aesthetic to a fixed value, like -\code{colour = "red"} or \code{size = 3}. They may also be parameters -to the paired geom/stat.} +\item{stat}{The statistical transformation to use on the data for this layer. +When using a \verb{geom_*()} function to construct a layer, the \code{stat} +argument can be used the override the default coupling between geoms and +stats. The \code{stat} argument accepts the following: +\itemize{ +\item A \code{Stat} ggproto subclass, for example \code{StatCount}. +\item A string naming the stat. To give the stat as a string, strip the +function name of the \code{stat_} prefix. For example, to use \code{stat_count()}, +give the stat as \code{"count"}. +\item For more information and other ways to specify the stat, see the +\link[ggplot2:layer_stats]{layer stat} documentation. +}} + +\item{position}{A position adjustment to use on the data for this layer. +Cannot be jointy specified with \code{nudge_x} or \code{nudge_y}. This +can be used in various ways, including to prevent overplotting and +improving the display. The \code{position} argument accepts the following: +\itemize{ +\item The result of calling a position function, such as \code{position_jitter()}. +\item A string nameing the position adjustment. To give the position as a +string, strip the function name of the \code{position_} prefix. For example, +to use \code{position_jitter()}, give the position as \code{"jitter"}. +\item For more information and other ways to specify the position, see the +\link[ggplot2:layer_positions]{layer position} documentation. +}} + +\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}'s \code{params} argument. These +arguments broadly fall into one of 4 categories below. Notably, further +arguments to the \code{position} argument, or aesthetics that are required +can \emph{not} be passed through \code{...}. Unknown arguments that are not part +of the 4 categories below are ignored. +\itemize{ +\item Static aesthetics that are not mapped to a scale, but are at a fixed +value and apply to the layer as a whole. For example, \code{colour = "red"} +or \code{linewidth = 3}. The geom's documentation has an \strong{Aesthetics} +section that lists the available options. The 'required' aesthetics +cannot be passed on to the \code{params}. Please note that while passing +unmapped aesthetics as vectors is technically possible, the order and +required length is not guaranteed to be parallel to the input data. +\item When constructing a layer using +a \verb{stat_*()} function, the \code{...} argument can be used to pass on +parameters to the \code{geom} part of the layer. An example of this is +\code{stat_density(geom = "area", outline.type = "both")}. The geom's +documentation lists which parameters it can accept. +\item Inversely, when constructing a layer using a +\verb{geom_*()} function, the \code{...} argument can be used to pass on parameters +to the \code{stat} part of the layer. An example of this is +\code{geom_area(stat = "density", adjust = 0.5)}. The stat's documentation +lists which parameters it can accept. +\item The \code{key_glyph} argument of \code{\link[ggplot2:layer]{layer()}} may also be passed on through +\code{...}. This can be one of the functions described as +\link[ggplot2:draw_key]{key glyphs}, to change the display of the layer in the legend. +}} \item{parse}{If \code{TRUE}, the labels will be parsed into expressions and displayed as described in \code{?plotmath}.} diff --git a/man/label_number_hdx.Rd b/man/label_number_hdx.Rd new file mode 100644 index 0000000..8281921 --- /dev/null +++ b/man/label_number_hdx.Rd @@ -0,0 +1,62 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/label_number_hdx.R +\name{label_number_hdx} +\alias{label_number_hdx} +\title{Label numbers in HDX key figures style} +\usage{ +label_number_hdx(additional_prefix = "") +} +\arguments{ +\item{additional_prefix}{Additional prefix to add to string, that will come +between \code{sign_prefix} and the number. For example, \code{"$"} could produce a +return value of \verb{-$1.1K}.} +} +\value{ +Returns a "labelling" function, in the same way as \code{scales::label_...()} +functions work, i.e. a function that takes \code{x} and returns a labelled character +vector of \code{length(x)}. +} +\description{ +Formats numeric vector in the Centre style for key figures, which abbreviates +numbers 1,000 and above to X.YK, 10,000 and above to XYK, 100,000 and above +to XYZK, and the same for 1,000,000 and above, replacing the K with an M, and +the same for B. Details of the data viz style can be found in the +\href{https://data.humdata.org/dataviz-guide/dataviz-elements/#/data-visualization/text}{visualization guidelines} +} +\details{ +Just for continuity, values are labeled with T for trillion, and that is the +maximum formatting available, anything above the trillions will continue to be +truncated to report in the trillions. + +Deals with negative values in case those ever need to be formatted in similar +manners. Also ensures that rounding is performed so numbers look correct. +Not to be used for percents, which should just use \code{\link[scales:label_percent]{scales::label_percent()}}. + +Designed like the \verb{scales::} family of label functions, the return value of +\code{label_number_hdx} is a function, based on the \code{additional_prefix}. So you +should pass it in to \code{scales_...()} \code{labels} parameter in the same way as +\code{scales_...()} +} +\examples{ +library(ggplot2) + +# discrete scaling +p <- ggplot(txhousing) + + geom_point( + aes( + x = median, + y = volume + ) + ) + +p + +p + + scale_x_continuous( + labels = label_number_hdx("$") + ) + + scale_y_continuous( + labels = label_number_hdx() + ) + +} diff --git a/man/scale_hdx.Rd b/man/scale_hdx.Rd index 8c6bf55..6d7564c 100644 --- a/man/scale_hdx.Rd +++ b/man/scale_hdx.Rd @@ -108,7 +108,7 @@ scale_fill_gradient2_hdx(...) \describe{ \item{\code{palette}}{A palette function that when called with a single integer argument (the number of levels in the scale) returns the values that -they should take (e.g., \code{\link[scales:hue_pal]{scales::hue_pal()}}).} +they should take (e.g., \code{\link[scales:pal_hue]{scales::pal_hue()}}).} \item{\code{breaks}}{One of: \itemize{ \item \code{NULL} for no breaks @@ -129,16 +129,11 @@ notation. }} \item{\code{drop}}{Should unused factor levels be omitted from the scale? The default, \code{TRUE}, uses the levels that appear in the data; -\code{FALSE} uses all the levels in the factor.} +\code{FALSE} includes the levels in the factor. Please note that to display +every level in a legend, the layer should use \code{show.legend = TRUE}.} \item{\code{na.translate}}{Unlike continuous scales, discrete scales can easily show missing values, and do so by default. If you want to remove missing values from a discrete scale, specify \code{na.translate = FALSE}.} - \item{\code{scale_name}}{The name of the scale that should be used for error messages -associated with this scale.} - \item{\code{name}}{The name of the scale. Used as the axis or legend title. If -\code{waiver()}, the default, the name of the scale is taken from the first -mapping used for that aesthetic. If \code{NULL}, the legend title will be -omitted.} \item{\code{labels}}{One of: \itemize{ \item \code{NULL} for no labels @@ -152,14 +147,7 @@ notation. }} \item{\code{guide}}{A function used to create a guide or its name. See \code{\link[ggplot2:guides]{guides()}} for more information.} - \item{\code{expand}}{For position scales, a vector of range expansion constants used to add some -padding around the data to ensure that they are placed some distance -away from the axes. Use the convenience function \code{\link[ggplot2:expansion]{expansion()}} -to generate the values for the \code{expand} argument. The defaults are to -expand the scale by 5\% on each side for continuous variables, and by -0.6 units on each side for discrete variables.} - \item{\code{position}}{For position scales, The position of the axis. -\code{left} or \code{right} for y axes, \code{top} or \code{bottom} for x axes.} + \item{\code{call}}{The \code{call} used to construct the scale for reporting messages.} \item{\code{super}}{The super class to use for the constructed scale} }} } diff --git a/vignettes/gghdx.Rmd b/vignettes/gghdx.Rmd index 35831fd..667f5eb 100644 --- a/vignettes/gghdx.Rmd +++ b/vignettes/gghdx.Rmd @@ -150,7 +150,8 @@ knitr::include_graphics( The inbuilt data `gghdx::df_covid` has aggregated COVID data we can use to mirror this plot. To make the data start at the y-axis, we can use -`scale_y_continuous_hdx()` which sets `expand = c(0, 0)` by default. +`scale_y_continuous_hdx()` which sets `expand = c(0, 0)` by default, and the +`label_number_hdx()` function to create custom labels. ```{r covid-match, fig.height = 5, fig.width = 6, out.width = "45%", fig.show = "hold", fig.align = "default"} p_blue <- ggplot( @@ -166,11 +167,7 @@ p_blue <- ggplot( fill = hdx_hex("sapphire-hdx") # use sapphire for fill ) + scale_y_continuous_hdx( - labels = scales::label_number( - accuracy = 1, - scale = 1 / 1000000, - suffix = "M" - ) + labels = label_number_hdx() ) + scale_x_date( date_breaks = "1 month",