Skip to content

Add ColorBrewer palettes #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 15, 2019
Merged

Add ColorBrewer palettes #190

merged 12 commits into from
May 15, 2019

Conversation

jgabry
Copy link
Member

@jgabry jgabry commented May 14, 2019

Closes #177

This PR allows ColorBrewer palettes to be used with bayesplot::color_scheme_set().

Basically, if a user specifies color_scheme_set("brewer-X"), where X is the name of a ColorBrewer palette recognized by RColorBrewer, then bayesplot will use RColorBrewer::brewer.pal(n = 6, name="X") to get the colors.

I also cleaned up and rearranged some internals in the bayesplot-colors.R file and changed the roxygen to use markdown, so it may seem like more changed than actually did. The only change that changes any behavior is in these lines in the internal scheme_from_string() function:

} else if (identical(substr(scheme, 1, 7), "brewer-")) {
# user specified a ColorBrewer scheme (e.g., "brewer-Blues")
if (!requireNamespace("RColorBrewer", quietly = TRUE)) {
stop("Please install the 'RColorBrewer' package to use a ColorBrewer scheme.",
call.=FALSE)
}
clrs <- RColorBrewer::brewer.pal(n = 6, name = gsub("brewer-", "", scheme))
x <- setNames(as.list(clrs), scheme_level_names())

Example Usage

color_scheme_set("brewer-Spectral")
mcmc_trace(example_mcmc_draws(), pars = "sigma")

brewer-example

@codecov-io
Copy link

codecov-io commented May 14, 2019

Codecov Report

Merging #190 into master will decrease coverage by 0.04%.
The diff coverage is 97.1%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #190      +/-   ##
==========================================
- Coverage   99.36%   99.31%   -0.05%     
==========================================
  Files          30       30              
  Lines        4083     4088       +5     
==========================================
+ Hits         4057     4060       +3     
- Misses         26       28       +2
Impacted Files Coverage Δ
R/bayesplot-ggplot-themes.R 94.59% <ø> (ø) ⬆️
R/bayesplot-colors.R 98.51% <97.1%> (-1.49%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3922b87...d44b9e5. Read the comment docs.

[ci skip]
@@ -310,34 +357,8 @@ is_hex_color <- function(x) {
isTRUE(nchar(x) == 7)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ggplot2 will take nine character ones where the last two set the alpha level. Some palettes emit colors with these extra characters.

scales::viridis_pal()(4)
#> [1] "#440154FF" "#31688EFF" "#35B779FF" "#FDE725FF"

Not sure we need to accommodate this though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't know that. I'm not sure we need to accommodate it either, although I'm not opposed. I suppose adding support for that can be a separate issue if we every want to do it or get a request.

tjmahr added 2 commits May 15, 2019 09:57
- use `is.null()` and `NULL` default instead of `missing()` to make interactive testing easier
- refactor full_level_name to use a look-up table and get vectorization for free
@tjmahr
Copy link
Collaborator

tjmahr commented May 15, 2019

Take a look at my changes and feel free to merge.

@jgabry
Copy link
Member Author

jgabry commented May 15, 2019

Thanks @tjmahr. Merging this now. (Travis checks are good except for a note about the hexbin package but that’s taken care of by my other PR #187)

@jgabry jgabry merged commit 39e36f5 into master May 15, 2019
@jgabry jgabry deleted the brewer-palettes branch May 15, 2019 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add colorblind safe and printer friendly options to color_scheme_set
3 participants