Skip to content

Commit

Permalink
Update color system
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Jan 27, 2024
1 parent b604b51 commit 5bfb73f
Show file tree
Hide file tree
Showing 31 changed files with 610 additions and 93 deletions.
19 changes: 17 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ message: 'To cite package "tidyBdE" in publications use:'
type: software
license: GPL-3.0-or-later
title: 'tidyBdE: Download Data from Bank of Spain'
version: 0.3.4
version: 0.3.4.9000
doi: 10.5281/zenodo.4673496
abstract: Tools to download data series from 'Banco de España' ('BdE') on 'tibble'
format. 'Banco de España' is the national central bank and, within the framework
Expand All @@ -32,7 +32,7 @@ preferred-citation:
affiliation: rOpenSpain
doi: 10.5281/zenodo.4673496
year: '2024'
version: 0.3.4
version: 0.3.4.9000
url: https://ropenspain.github.io/tidyBdE/
abstract: Tools to download data series from Banco de España (BdE) on tibble format.
Banco de España is the national central bank and, within the framework of the
Expand Down Expand Up @@ -134,6 +134,21 @@ references:
orcid: https://orcid.org/0000-0002-9415-4582
year: '2024'
version: '>= 3.3.0'
- type: software
title: lifecycle
abstract: 'lifecycle: Manage the Life Cycle of your Package Functions'
notes: Imports
url: https://lifecycle.r-lib.org/
repository: https://CRAN.R-project.org/package=lifecycle
authors:
- family-names: Henry
given-names: Lionel
email: lionel@posit.co
- family-names: Wickham
given-names: Hadley
email: hadley@posit.co
orcid: https://orcid.org/0000-0003-4757-117X
year: '2024'
- type: software
title: readr
abstract: 'readr: Read Rectangular Text Data'
Expand Down
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tidyBdE
Title: Download Data from Bank of Spain
Version: 0.3.4
Version: 0.3.4.9000
Authors@R:
person("Diego", "H. Herrero", , "dev.dieghernan@gmail.com", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-8457-4658", affiliation = "rOpenSpain"))
Expand All @@ -19,6 +19,7 @@ Depends:
Imports:
dplyr (>= 0.7.0),
ggplot2 (>= 3.3.0),
lifecycle,
readr (>= 1.0.0),
scales (>= 1.1.0),
tibble (>= 3.0.0),
Expand All @@ -37,6 +38,6 @@ Config/testthat/parallel: true
Copyright: See file inst/COPYRIGHTS
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
X-schema.org-applicationCategory: Macroeconomics
X-schema.org-isPartOf: https://ropenspain.es/
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ export(bde_ind_ibex_daily)
export(bde_ind_ibex_monthly)
export(bde_ind_population)
export(bde_ind_unemployment_rate)
export(bde_palettes)
export(bde_parse_dates)
export(bde_rose_pal)
export(bde_series_full_load)
export(bde_series_load)
export(bde_vivid_pal)
export(scale_color_bde_c)
export(scale_color_bde_d)
export(scale_colour_bde_c)
export(scale_colour_bde_d)
export(scale_fill_bde_c)
export(scale_fill_bde_d)
export(theme_tidybde)
Expand All @@ -32,5 +35,7 @@ importFrom(ggplot2,element_text)
importFrom(ggplot2,margin)
importFrom(ggplot2,rel)
importFrom(ggplot2,unit)
importFrom(lifecycle,deprecated)
importFrom(tibble,tibble)
importFrom(utils,download.file)
importFrom(utils,read.csv2)
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# tidyBdE (development version)

- `bde_vivid_pal()` and `bde_rose_pal()` have been deprecated. Use the new
function `bde_palettes()` instead.

# tidyBdE 0.3.4

- Update API entry points.
Expand Down
59 changes: 59 additions & 0 deletions R/bde_palettes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' BdE color palettes
#'
#' Custom palettes based on the publications of BdE. These are manual palettes
#' with a maximum of 6 colors.
#'
#' @family bde_plot
#'
#'
#' @return A vector of colors.
#'
#' @export
#' @param n The number of colors (`>= 1`) to be in the palette.
#' @param palette A valid palette name.
#' @param alpha An alpha-transparency level in the range `[0,1]` (`0` means
#' transparent and `1` means opaque). A missing, i.e., `alpha = NULL`, does
#' not add opacity codes (`"FF"`) to the individual color hex codes. See
#' [ggplot2::alpha()].
#' @param rev Logical indicating whether the ordering of the colors should be
#' reversed.
#' @examples
#'
#' # BdE vivid pal
#' scales::show_col(bde_palettes(palette = "bde_vivid_pal"), labels = FALSE)
#'
#' # BdE rose pal
#' scales::show_col(bde_palettes(palette = "bde_rose_pal"), labels = FALSE)
bde_palettes <- function(n = 6, palette = c("bde_vivid_pal", "bde_rose_pal"),
alpha = NULL, rev = FALSE) {
palette <- match.arg(palette)

cols <- switch(palette,
"bde_vivid_pal" = c(
"#4180C2", "#D86E7B", "#F89E63",
"#5FBD6A", "#62C8D0", "#AC8771"
),
"bde_rose_pal" = c(
"#b7365c", "#cb6e8a", "#db9aad",
"#0a50a1", "#5385bd", "#89AEDA"
)
)

n_col <- length(cols)
if (n > n_col) {
message(
"tidyBdE> ", palette, " has ", n_col, ", requested ", n,
". Returning ", n_col, " colors."
)

n <- n_col
}

endcols <- cols[seq_len(n)]

# Options
if (rev) endcols <- rev(endcols)
if (!is.null(alpha)) endcols <- ggplot2::alpha(endcols, alpha)

return(endcols)
}
6 changes: 4 additions & 2 deletions R/catalogs.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#' @return A `tibble`.
#'
#'
#' @source [Time-series bulk data
#' @source
#' [Time-series bulk data
#' download](https://www.bde.es/webbe/en/estadisticas/recursos/descargas-completas.html)

Check warning on line 15 in R/catalogs.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/catalogs.R,line=15,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 88 characters.
#'
#' @param catalog A single value indicating the catalogs to be updated
Expand Down Expand Up @@ -208,7 +209,8 @@ bde_catalog_load <-
#'
#' @return None. Downloads the catalog file(s) to the local machine.
#'
#' @source [Time-series bulk data
#' @source
#' [Time-series bulk data
#' download](https://www.bde.es/webbe/en/estadisticas/recursos/descargas-completas.html)

Check warning on line 214 in R/catalogs.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/catalogs.R,line=214,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 88 characters.
#'
#' @param catalog A vector of characters indicating the catalogs to be updated
Expand Down
31 changes: 18 additions & 13 deletions R/bde_pals.R → R/deprecated.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#' BdE color palettes
#' BdE superseeded palettes
#'
#' Custom palettes based on the publications of BdE.
#' @description
#' `r lifecycle::badge('deprecated')` See [bde_palettes()]
#'
#' @family bde_plot
#'
#' @rdname bde_pals
#' @name bde_pals
#'
#' @return A palette of colors.
#'
#' @export
#'
#' @param ... Further arguments of the functions.
#'
#' @name bde_vivid_pal
#' @rdname bde_pals
#'
#'
#' @export
#' @keywords internal
#' @examples
#'
#' # BdE vivid pal
Expand All @@ -23,18 +22,24 @@
#' # BdE rose pal
#' scales::show_col(bde_rose_pal()(6), labels = FALSE)
bde_vivid_pal <- function(...) {
# nocov start
lifecycle::deprecate_warn(
"0.3.4", "bde_vivid_pal()",
"bde_palettes()"
)

pal <- c("#4180C2", "#D86E7B", "#F89E63", "#5FBD6A", "#62C8D0", "#AC8771")
return(scales::manual_pal(pal))
# nocov end
}

#' @rdname bde_pals
#'
#' @keywords internal
#' @export
bde_rose_pal <- function(...) {
# nocov start
lifecycle::deprecate_warn(
"0.3.4", "bde_rose_pal()",
"bde_palettes()"
)

pal <- c("#b7365c", "#cb6e8a", "#db9aad", "#0a50a1", "#5385bd", "#89AEDA")
return(scales::manual_pal(pal))
# nocov end
}
78 changes: 32 additions & 46 deletions R/scales.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#' BdE scales for `ggplot2`.
#' BdE scales for \CRANpkg{ggplot2}.
#'
#' @description
#'
#' Scales to be used with the \CRANpkg{ggplot2} package. Discrete palettes are
#' named as `scale_*_bde_d` while continuous palettes are named `scale_*_bde_c`.
#'
#' @seealso [bde_vivid_pal()], [ggplot2::discrete_scale()],
#' [ggplot2::continuous_scale()]
#' @seealso [ggplot2::discrete_scale()], [ggplot2::continuous_scale()]
#'
#' @family bde_plot
#'
#' @export
#'
#' @return A `ggplot2` color scale.
#' @return A \CRANpkg{ggplot2} color scale.
#'
#' @rdname scales_bde
#'
#' @name scales_bde
#'
#' @param palette Name of the BdE palette to apply. One of `"bde_vivid_pal"`,
#' `"bde_rose_pal"`. See [bde_pals()] for details.
#' `"bde_rose_pal"`. See [bde_palettes()] for details.
#'
#' @param ... Further arguments of [ggplot2::discrete_scale()] or
#' [ggplot2::continuous_scale()].
Expand Down Expand Up @@ -48,100 +47,87 @@
#' theme_minimal()
#'
scale_color_bde_d <- function(palette = c("bde_vivid_pal", "bde_rose_pal"),
...) { # nocov start

...) {
palette <- match.arg(palette)
valid_pals <- c("bde_vivid_pal", "bde_rose_pal")

if (!palette %in% valid_pals) {
stop("palette should be one of: ", paste0(valid_pals, collapse = ", "))
}
cols_v <- bde_palettes(palette = palette)
pal <- scales::manual_pal(cols_v)

cols <- switch(palette,
"bde_vivid_pal" = bde_vivid_pal(),
"bde_rose_pal" = bde_rose_pal()
)
ggplot2::discrete_scale(
aesthetics = "color",
palette = pal,
scale_name = palette,
palette = cols,
...
)
# nocov end
}

#' @rdname scales_bde
#' @name scales_bde
#' @export
#' @usage NULL
scale_colour_bde_d <- scale_color_bde_d

#' @rdname scales_bde
#' @name scales_bde
#' @export
scale_fill_bde_d <- function(palette = c("bde_vivid_pal", "bde_rose_pal"),
...) { # nocov start
...) {
palette <- match.arg(palette)
valid_pals <- c("bde_vivid_pal", "bde_rose_pal")

if (!palette %in% valid_pals) {
stop("palette should be one of :", paste0(valid_pals, collapse = ", "))
}
cols_v <- bde_palettes(palette = palette)
pal <- scales::manual_pal(cols_v)

cols <- switch(palette,
"bde_vivid_pal" = bde_vivid_pal(),
"bde_rose_pal" = bde_rose_pal()
)
ggplot2::discrete_scale(
aesthetics = "fill",
palette = pal,
scale_name = palette,
palette = cols,
...
)
# nocov end
}


#' @rdname scales_bde
#' @name scales_bde
#' @export
scale_color_bde_c <- function(palette = c("bde_vivid_pal", "bde_rose_pal"),
...) { # nocov start
scale_color_bde_c <- function(palette = c("bde_rose_pal", "bde_vivid_pal"),
...) {
palette <- match.arg(palette)
valid_pals <- c("bde_vivid_pal", "bde_rose_pal")

if (!palette %in% valid_pals) {
stop("palette should be one of :", paste0(valid_pals, collapse = ", "))
}

cols <- switch(palette,
"bde_vivid_pal" = bde_vivid_pal()(6),
"bde_rose_pal" = c(bde_rose_pal()(6)[1:3], bde_rose_pal()(6)[6:4])
"bde_vivid_pal" = bde_palettes(6, "bde_vivid_pal"),
"bde_rose_pal" = bde_palettes(6, "bde_rose_pal")[c(1, 2, 3, 6, 5, 4)]
)
ggplot2::continuous_scale(
aesthetics = "color",
scale_name = palette,
palette = scales::gradient_n_pal(cols),
...
)
# nocov end
}

#' @rdname scales_bde
#' @name scales_bde
#' @export
scale_fill_bde_c <- function(palette = c("bde_vivid_pal", "bde_rose_pal"),
...) { # nocov start
#' @usage NULL
scale_colour_bde_c <- scale_color_bde_c

#' @rdname scales_bde
#' @name scales_bde
#' @export
scale_fill_bde_c <- function(palette = c("bde_rose_pal", "bde_vivid_pal"),
...) {
palette <- match.arg(palette)
valid_pals <- c("bde_vivid_pal", "bde_rose_pal")

if (!palette %in% valid_pals) {
stop("palette should be one of :", paste0(valid_pals, collapse = ", "))
}

cols <- switch(palette,
"bde_vivid_pal" = bde_vivid_pal()(6),
"bde_rose_pal" = c(bde_rose_pal()(6)[1:3], bde_rose_pal()(6)[6:4])
"bde_vivid_pal" = bde_palettes(6, "bde_vivid_pal"),
"bde_rose_pal" = bde_palettes(6, "bde_rose_pal")[c(1, 2, 3, 6, 5, 4)]
)
ggplot2::continuous_scale(
aesthetics = "fill",
scale_name = palette,
palette = scales::gradient_n_pal(cols),
...
)
# nocov end
}
Loading

0 comments on commit 5bfb73f

Please sign in to comment.