Skip to content

Commit b0b5563

Browse files
committed
ColorBrewer schemes with color_scheme_set()
See #177
1 parent 3922b87 commit b0b5563

File tree

4 files changed

+139
-113
lines changed

4 files changed

+139
-113
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Suggests:
3434
gridExtra (>= 2.2.1),
3535
knitr (>= 1.16),
3636
loo (>= 2.0.0),
37+
RColorBrewer,
3738
rmarkdown (>= 1.0.0),
3839
rstan (>= 2.17.1),
3940
rstanarm (>= 2.17.4),

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
<!-- Items for next release go here* -->
88

9+
* ColorBrewer palettes are now available as color schemes via
10+
[`color_scheme_set()`](https://mc-stan.org/bayesplot/reference/bayesplot-colors.html). (#177)
11+
912
* MCMC plots now also accept objects with an `as.array` method as
1013
input (e.g., stanfit objects). (#175, #184)
1114

R/bayesplot-colors.R

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
11
#' Set, get, or view color schemes
22
#'
33
#' Set, get, or view color schemes. Choose from a preset scheme or create a
4-
#' custom scheme.
4+
#' custom scheme. See the **Available color schemes** section below for a list
5+
#' of available scheme names. The **Custom color schemes** section describes how
6+
#' to specify a custom scheme.
57
#'
8+
#' @md
69
#' @name bayesplot-colors
10+
#' @param scheme For `color_scheme_set()`, either a string naming one of the
11+
#' available color schemes or a character vector of _exactly six_ colors
12+
#' specifying a custom scheme.
713
#'
8-
#' @param scheme For \code{color_scheme_set}, either a string naming one of the
9-
#' available color schemes or a character vector of \emph{exactly six} colors
10-
#' specifying a custom scheme (see the \strong{Custom Color Schemes} section,
11-
#' below, for more on specifying a custom scheme).
12-
#'
13-
#' For \code{color_scheme_get}, \code{scheme} can be missing (to get the
14+
#' For `color_scheme_get()`, `scheme` can be missing (to get the
1415
#' current color scheme) or a string naming one of the preset schemes.
1516
#'
16-
#' For \code{color_scheme_view}, \code{scheme} can be missing (to use the
17+
#' For `color_scheme_view()`, `scheme` can be missing (to use the
1718
#' current color scheme) or a character vector containing a subset of the
1819
#' available scheme names.
1920
#'
20-
#' Currently, the available preset color schemes are:
21-
#' \itemize{
22-
#' \item \code{"blue"}, \code{"brightblue"}
23-
#' \item \code{"gray"}, \code{"darkgray"}
24-
#' \item \code{"green"}
25-
#' \item \code{"pink"}
26-
#' \item \code{"purple"}
27-
#' \item \code{"red"}
28-
#' \item \code{"teal"}
29-
#' \item \code{"yellow"}
30-
#' \item \href{https://CRAN.R-project.org/package=viridis}{\code{"viridis"}},
31-
#' \code{"viridisA"}, \code{"viridisB"}, \code{"viridisC"}
32-
#' \item \code{"mix-x-y"}, replacing \code{x} and \code{y} with any two of
33-
#' the scheme names listed above (e.g. "mix-teal-pink", "mix-blue-red",
34-
#' etc.). The order of \code{x} and \code{y} matters, i.e., the color
35-
#' schemes "mix-blue-red" and "mix-red-blue" are not identical. There is no
36-
#' guarantee that every possible mixed scheme will look good with every
37-
#' possible plot.
38-
#' }
39-
#'
40-
#' If you have a suggestion for a new color scheme please let us know via the
41-
#' \pkg{bayesplot} \href{https://github.com/stan-dev/bayesplot/issues}{issue
42-
#' tracker}.
21+
#' See the **Available color schemes** section below for a list of available
22+
#' scheme names. The **Custom color schemes** section describes how to specify
23+
#' a custom scheme.
4324
#'
44-
#' @return \code{color_scheme_set} has the side effect of setting the color
45-
#' scheme used for plotting. It also returns
46-
#' (\code{\link[=invisible]{invisibly}}) a list of the hexidecimal color
47-
#' values used in \code{scheme}.
25+
#' @return `color_scheme_set()` has the side effect of setting the color scheme
26+
#' used for plotting. It also returns ([invisibly][base::invisible]) a list of
27+
#' the hexidecimal color values used in `scheme`.
4828
#'
49-
#' \code{color_scheme_get} returns a \code{list} of the hexadecimal color
50-
#' values (without changing the current scheme). If the \code{scheme} argument
29+
#' `color_scheme_get()` returns a list of the hexadecimal color
30+
#' values (without changing the current scheme). If the `scheme` argument
5131
#' is not specified the returned values correspond to the current color
52-
#' scheme. If the optional argument \code{i} is specified then the returned
53-
#' list only contains \code{length(i)} elements.
32+
#' scheme. If the optional argument `i` is specified then the returned
33+
#' list only contains `length(i)` elements.
5434
#'
55-
#' \code{color_scheme_view} returns a ggplot object if only a single scheme is
35+
#' `color_scheme_view()` returns a ggplot object if only a single scheme is
5636
#' specified and a gtable object if multiple schemes names are specified.
5737
#'
58-
#' @section Custom Color Schemes: A \pkg{bayesplot} color scheme consists of six
38+
#' @section Available color schemes: Currently, the available preset color
39+
#' schemes are:
40+
#' * `"blue"`, `"brightblue"`
41+
#' * `"gray"`, `"darkgray"`
42+
#' * `"green"`
43+
#' * `"pink"`
44+
#' * `"purple"`
45+
#' * `"red"`
46+
#' * `"teal"`
47+
#' * `"yellow"`
48+
#' * [`"viridis"`](https://CRAN.R-project.org/package=viridis), `"viridisA"`,
49+
#' `"viridisB"`, `"viridisC"`
50+
#' * `"mix-x-y"`, replacing `x` and `y` with any two of
51+
#' the scheme names listed above (e.g. "mix-teal-pink", "mix-blue-red",
52+
#' etc.). The order of `x` and `y` matters, i.e., the color schemes
53+
#' `"mix-blue-red"` and `"mix-red-blue"` are not identical. There is no
54+
#' guarantee that every possible mixed scheme will look good with every
55+
#' possible plot.
56+
#' * `"brewer-x"`, replacing `x` with the name of a palette available from
57+
#' [RColorBrewer::brewer.pal()] (e.g., `brewer-PuBuGn`).
58+
#'
59+
#' If you have a suggestion for a new color scheme please let us know via the
60+
#' **bayesplot** [issue tracker](https://github.com/stan-dev/bayesplot/issues).
61+
#'
62+
#' @section Custom color schemes: A **bayesplot** color scheme consists of six
5963
#' colors. To specify a custom color scheme simply pass a character vector
60-
#' containing either the names of six \code{\link[grDevices]{colors}} or six
64+
#' containing either the names of six [colors][grDevices::colors] or six
6165
#' hexidecimal color values (or a mix of names and hex values). The colors
6266
#' should be in order from lightest to darkest. See the end of the
63-
#' \strong{Examples} section for a demonstration.
67+
#' **Examples** section for a demonstration.
6468
#'
6569
#' @template seealso-theme
6670
#'
@@ -128,10 +132,11 @@ color_scheme_set <- function(scheme = "blue") {
128132
}
129133

130134
#' @rdname bayesplot-colors
135+
#' @md
131136
#' @export
132-
#' @param i For \code{color_scheme_get}, a subset of the integers from \code{1}
133-
#' (lightest) to \code{6} (darkest) indicating which of the colors in the
134-
#' scheme to return. If \code{i} is not specified then all six colors in the
137+
#' @param i For `color_scheme_get()`, a subset of the integers from `1`
138+
#' (lightest) to `6` (darkest) indicating which of the colors in the
139+
#' scheme to return. If `i` is not specified then all six colors in the
135140
#' scheme are included.
136141
#'
137142
color_scheme_get <- function(scheme, i) {
@@ -229,11 +234,19 @@ scheme_from_string <- function(scheme) {
229234
if (identical(substr(scheme, 1, 4), "mix-")) {
230235
to_mix <- unlist(strsplit(scheme, split = "-"))[2:3]
231236
x <- setNames(mixed_scheme(to_mix[1], to_mix[2]), scheme_level_names())
232-
structure(x, mixed = TRUE, scheme_name = scheme)
237+
return(structure(x, mixed = TRUE, scheme_name = scheme))
238+
} else if (identical(substr(scheme, 1, 7), "brewer-")) {
239+
if (!requireNamespace("RColorBrewer", quietly = TRUE)) {
240+
stop("Please install the 'RColorBrewer' package to use a ColorBrewer scheme.",
241+
call.=FALSE)
242+
}
243+
clrs <- RColorBrewer::brewer.pal(n = 6, name = gsub("brewer-", "", scheme))
244+
x <- setNames(as.list(clrs), scheme_level_names())
245+
return(structure(x, mixed = FALSE, scheme_name = scheme))
233246
} else {
234247
scheme <- match.arg(scheme, choices = names(master_color_list))
235248
x <- prepare_colors(scheme)
236-
structure(x, mixed = FALSE, scheme_name = scheme)
249+
return(structure(x, mixed = FALSE, scheme_name = scheme))
237250
}
238251
}
239252

@@ -244,13 +257,14 @@ is_mixed_scheme <- function(x) {
244257
isTRUE(attr(x, "mixed"))
245258
}
246259

247-
# Access a subset of the scheme colors
248-
#
249-
# @param level A character vector of level names (see scheme_level_names()). The
250-
# abbreviations "l", "lh", "m", "mh", "d", and "dh" can also be used instead
251-
# of the full names.
252-
# @return A character vector of color values.
253-
#
260+
#' Access a subset of the scheme colors
261+
#'
262+
#' @noRd
263+
#' @param level A character vector of level names (see `scheme_level_names()`).
264+
#' The abbreviations "l", "lh", "m", "mh", "d", and "dh" can also be used
265+
#' instead of the full names.
266+
#' @return A character vector of color values.
267+
#'
254268
get_color <- function(levels) {
255269
sel <- which(!levels %in% scheme_level_names())
256270
if (length(sel))

man/bayesplot-colors.Rd

Lines changed: 66 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)