From f163b47722cfbb542e74dd6959b37ad7408e1cf9 Mon Sep 17 00:00:00 2001 From: Victor Navarro Date: Sun, 7 Apr 2024 12:47:28 +0100 Subject: [PATCH] added palette manipulation --- DESCRIPTION | 3 ++- NAMESPACE | 1 + NEWS.md | 3 +++ R/assertions.R | 11 ++++++++ R/maps.R | 2 +- R/plotting_functions.R | 39 ++++++++++++++++++---------- R/set_calmr_palette.R | 17 ++++++++++++ docs/404.html | 2 +- docs/LICENSE.html | 2 +- docs/articles/calmr_app.html | 2 +- docs/articles/calmr_basics.html | 2 +- docs/articles/calmr_fits.html | 2 +- docs/articles/heidi_similarity.html | 2 +- docs/articles/index.html | 2 +- docs/authors.html | 2 +- docs/index.html | 6 ++--- docs/news/index.html | 6 ++++- docs/pkgdown.yml | 2 +- docs/reference/CalmrFit-class.html | 2 +- docs/reference/CalmrRSA-class.html | 2 +- docs/reference/RSA.html | 2 +- docs/reference/compare_models.html | 2 +- docs/reference/fit_model.html | 2 +- docs/reference/index.html | 7 ++++- docs/reference/parse_design.html | 2 +- docs/reference/pati.html | 2 +- docs/reference/run_experiment.html | 2 +- docs/search.json | 2 +- docs/sitemap.xml | 3 +++ man/plotting_functions.Rd | 2 +- man/set_calmr_palette.Rd | 22 ++++++++++++++++ tests/testthat/test-RSA.R | 4 ++- tests/testthat/test-global_options.R | 21 +++++++++++++++ vignettes/RAND.Rmd | 2 +- vignettes/parallelism_in_calmr.Rmd | 13 +++++----- 35 files changed, 150 insertions(+), 48 deletions(-) create mode 100644 R/set_calmr_palette.R create mode 100644 man/set_calmr_palette.Rd create mode 100644 tests/testthat/test-global_options.R diff --git a/DESCRIPTION b/DESCRIPTION index 3c928ca..b7082d7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: calmr Title: Canonical Associative Learning Models and their Representations -Version: 0.6.2 +Version: 0.6.3 Authors@R: person("Victor", "Navarro", , "navarrov@cardiff.ac.uk", role = c("aut", "cre", "cph")) Description: Implementations of canonical associative learning models, @@ -78,6 +78,7 @@ Collate: 'calmr_verbosity.R' 'parallel_helpers.R' 'maps.R' + 'set_calmr_palette.R' 'class_model.R' 'class_design.R' 'class_result.R' diff --git a/NAMESPACE b/NAMESPACE index 7fd6056..b0aa06e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,6 +28,7 @@ export(plot_tbins) export(plot_trials) export(rsa) export(run_experiment) +export(set_calmr_palette) export(set_reward_parameters) export(supported_families) export(supported_models) diff --git a/NEWS.md b/NEWS.md index cee3f17..4a5d1a2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# calmr 0.6.3 +* Added `set_calmr_palette()` function to control the colour/fill scales used to plot results ([#1](https://github.com/victor-navarro/calmr/issues/1)). + # calmr 0.6.2 * Aggregation of ANCCR data now ignores time; time entries are averaged. * Added the Temporal Difference model under the name "TD". The model is in an experimental state. diff --git a/R/assertions.R b/R/assertions.R index b7128fc..3c1439b 100644 --- a/R/assertions.R +++ b/R/assertions.R @@ -147,3 +147,14 @@ is_design <- function(object) { length(experiment@design@mapping$unique_nominal_stimuli) > 1 ) } + +.assert_valid_palette <- function(palette) { + if (is.null(palette)) { + return("viridis") + } + stopifnot( + "Palette must be one returned by `set_calmr_palette()`" = + palette %in% set_calmr_palette() + ) + palette +} diff --git a/R/maps.R b/R/maps.R index 916c602..29cc4fe 100644 --- a/R/maps.R +++ b/R/maps.R @@ -260,7 +260,7 @@ plots_map <- function() { "elegibilities" = "Elegibility Trace", "probabilities" = "Response Probability" ) - ggplot2::labs(y = prettynames[output]) + prettynames[output] } .get_scale_prettyname <- function(output) { diff --git a/R/plotting_functions.R b/R/plotting_functions.R index 4cf53b0..1d5509d 100644 --- a/R/plotting_functions.R +++ b/R/plotting_functions.R @@ -155,18 +155,29 @@ plot_targetted_complex_trials <- function(data, col) { #' @return A 'ggplot2' scale for colour or fill. #' @noRd .calmr_scales <- function(which, ...) { - switch(which, - "colour_d" = { - ggplot2::scale_colour_viridis_d(begin = .1, end = .9, ...) - }, - "fill_d" = { - ggplot2::scale_fill_viridis_d(begin = .1, end = .9, ...) - }, - "colour_c" = { - ggplot2::scale_colour_viridis_c(begin = .1, end = .9, ...) # nocov - }, - "fill_c" = { - ggplot2::scale_fill_viridis_c(begin = .1, end = .9, ...) - } - ) + current_scale <- getOption("calmr_palette", default = "viridis") + if (current_scale == "viridis") { + return(switch(which, + "colour_d" = ggplot2::scale_colour_viridis_d( + begin = .1, end = .9, ... + ), + "fill_d" = ggplot2::scale_fill_viridis_d( + begin = .1, end = .9, ... + ), + "colour_c" = ggplot2::scale_colour_viridis_c( + begin = .1, end = .9, ... + ), + "fill_c" = ggplot2::scale_fill_viridis_c( + begin = .1, end = .9, ... + ) + )) + } + if (current_scale == "hue") { + return(switch(which, + "colour_d" = ggplot2::scale_colour_discrete(...), + "fill_d" = ggplot2::scale_fill_discrete(...), + "colour_c" = ggplot2::scale_colour_continuous(...), # nocov + "fill_c" = ggplot2::scale_fill_continuous(...) + )) + } } diff --git a/R/set_calmr_palette.R b/R/set_calmr_palette.R new file mode 100644 index 0000000..e688c63 --- /dev/null +++ b/R/set_calmr_palette.R @@ -0,0 +1,17 @@ +#' Get/set the colour/fill palette for plots +#' @param palette A string specifying the available palettes. +#' If NULL, returns available palettes. +#' @return The old palette (invisibly) if palette is not NULL. +#' Otherwise, a character vector of available palettes. +#' @note Changes here do not affect the palette used in graphs. +#' @export + +set_calmr_palette <- function(palette = NULL) { + avail_palettes <- c("viridis", "hue") + if (is.null(palette)) { + return(avail_palettes) + } + palette <- .assert_valid_palette(palette) + old <- options("calmr_palette" = palette) + invisible(old) +} diff --git a/docs/404.html b/docs/404.html index b96d19e..31ea054 100644 --- a/docs/404.html +++ b/docs/404.html @@ -31,7 +31,7 @@ calmr - 0.6.2 + 0.6.3