From 31bc5fd3ae098982b9f50f710f3677e898fab264 Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Tue, 20 Aug 2024 13:31:23 +0100 Subject: [PATCH 1/3] Temp commit --- NAMESPACE | 1 + R/utils.R | 22 ++++++++++++++++++++++ man/label_pounds.Rd | 14 ++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 man/label_pounds.Rd diff --git a/NAMESPACE b/NAMESPACE index 5ef7d92..e8be313 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(label_pounds) export(mm_to_inch) export(scale_colour_continuous_af) export(scale_colour_discrete_af) diff --git a/R/utils.R b/R/utils.R index 977724a..668819a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,3 +17,25 @@ mm_to_inch <- function(x) { x / 25.4 } + + + +#' Label pounds sterling +#' +#' @param ... +#' +#' @return +#' @export +#' +#' @examples +#' scales::demo_continuous(c(0.1, 1), labels = label_pounds()) +#' scales::demo_continuous(c(1, 1e3), labels = label_pounds()) +#' scales::demo_log10(c(1, 1e12), breaks = log_breaks(5, 1e3), labels = label_pounds()) +label_pounds <- function(...) { + label_currency( + prefix = "£", + big.mark = ",", + decimal.mark = ".", + scale_cut = c(0, k = 1e3, m = 1e6, bn = 1e9, tn = 1e12), + ...) +} diff --git a/man/label_pounds.Rd b/man/label_pounds.Rd new file mode 100644 index 0000000..e233fdf --- /dev/null +++ b/man/label_pounds.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{label_pounds} +\alias{label_pounds} +\title{Label pounds sterling} +\usage{ +label_pounds(...) +} +\arguments{ +\item{...}{} +} +\description{ +Label pounds sterling +} From 28bb225a931ca1cfeba484f9c64065dc3e813167 Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Thu, 29 Aug 2024 16:27:00 +0100 Subject: [PATCH 2/3] Add label_pounds function --- R/utils.R | 44 ++++++++++++++++++++++++++++++++++---------- man/label_pounds.Rd | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/R/utils.R b/R/utils.R index 668819a..daf7eca 100644 --- a/R/utils.R +++ b/R/utils.R @@ -22,20 +22,44 @@ mm_to_inch <- function(x) { #' Label pounds sterling #' -#' @param ... +#' Label an axis or legend in pounds sterling. #' -#' @return +#' @return labelling function #' @export #' #' @examples -#' scales::demo_continuous(c(0.1, 1), labels = label_pounds()) -#' scales::demo_continuous(c(1, 1e3), labels = label_pounds()) -#' scales::demo_log10(c(1, 1e12), breaks = log_breaks(5, 1e3), labels = label_pounds()) -label_pounds <- function(...) { - label_currency( - prefix = "£", +#' library(ggplot2) +#' +#' scales::demo_continuous(c(0, 1), breaks = seq(0, 1, 0.1), labels = label_pounds()) +#' scales::demo_continuous(c(0, 5000), breaks = seq(0, 5000, 1000), labels = label_pounds()) +#' scales::demo_log10(c(1, 1e12), breaks = scales::log_breaks(5, 1e3), labels = label_pounds()) +#' +#' set.seed(123) +#' +#' price_data <- data.frame( +#' date = seq(as.Date("2024-05-12"), as.Date("2024-08-12"), by = 1), +#' price = runif(93, 0, 1000) +#' ) +#' +#' use_afcharts() +#' +#' p <- ggplot(price_data, aes(x = date, y = price)) + +#' geom_line() + +#' scale_y_continuous( +#' labels = label_pounds(), +#' expand = c(0, 0), +#' breaks = seq(0, 1000, 100) +#' ) + +#' coord_cartesian(ylim = c(0, 1000)) +#' +#' p +label_pounds <- function() { + scales::label_currency( + prefix = "\u00a3", + suffix = "", big.mark = ",", decimal.mark = ".", - scale_cut = c(0, k = 1e3, m = 1e6, bn = 1e9, tn = 1e12), - ...) + trim = TRUE, + scale_cut = c(0, k = 1e3, m = 1e6, bn = 1e9, tn = 1e12) + ) } diff --git a/man/label_pounds.Rd b/man/label_pounds.Rd index e233fdf..c9f6db4 100644 --- a/man/label_pounds.Rd +++ b/man/label_pounds.Rd @@ -4,11 +4,38 @@ \alias{label_pounds} \title{Label pounds sterling} \usage{ -label_pounds(...) +label_pounds() } -\arguments{ -\item{...}{} +\value{ +labelling function } \description{ -Label pounds sterling +Label an axis or legend in pounds sterling. +} +\examples{ +library(ggplot2) + +scales::demo_continuous(c(0, 1), breaks = seq(0, 1, 0.1), labels = label_pounds()) +scales::demo_continuous(c(0, 5000), breaks = seq(0, 5000, 1000), labels = label_pounds()) +scales::demo_log10(c(1, 1e12), breaks = scales::log_breaks(5, 1e3), labels = label_pounds()) + +set.seed(123) + +price_data <- data.frame( + date = seq(as.Date("2024-05-12"), as.Date("2024-08-12"), by = 1), + price = runif(93, 0, 1000) +) + +use_afcharts() + +p <- ggplot(price_data, aes(x = date, y = price)) + + geom_line() + + scale_y_continuous( + labels = label_pounds(), + expand = c(0, 0), + breaks = seq(0, 1000, 100) + ) + + coord_cartesian(ylim = c(0, 1000)) + +p } From 5e86136661cc1985532c1c96a94a9de4559b4f36 Mon Sep 17 00:00:00 2001 From: Olivia Box Power Date: Fri, 6 Sep 2024 16:46:29 +0100 Subject: [PATCH 3/3] Update documentation --- R/utils.R | 10 ++++++++-- man/label_pounds.Rd | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/R/utils.R b/R/utils.R index daf7eca..0589453 100644 --- a/R/utils.R +++ b/R/utils.R @@ -46,9 +46,15 @@ mm_to_inch <- function(x) { #' p <- ggplot(price_data, aes(x = date, y = price)) + #' geom_line() + #' scale_y_continuous( -#' labels = label_pounds(), +#' labels = label_pounds(), #' expand = c(0, 0), -#' breaks = seq(0, 1000, 100) +#' breaks = seq(0, 1000, 100), +#' name = "Price (£)" +#' ) + +#' scale_x_date( +#' date_breaks = "1 month", +#' date_labels = "%B %Y", +#' name = "" #' ) + #' coord_cartesian(ylim = c(0, 1000)) #' diff --git a/man/label_pounds.Rd b/man/label_pounds.Rd index c9f6db4..2520258 100644 --- a/man/label_pounds.Rd +++ b/man/label_pounds.Rd @@ -31,9 +31,15 @@ use_afcharts() p <- ggplot(price_data, aes(x = date, y = price)) + geom_line() + scale_y_continuous( - labels = label_pounds(), + labels = label_pounds(), expand = c(0, 0), - breaks = seq(0, 1000, 100) + breaks = seq(0, 1000, 100), + name = "Price (£)" + ) + + scale_x_date( + date_breaks = "1 month", + date_labels = "\%B \%Y", + name = "" ) + coord_cartesian(ylim = c(0, 1000))