-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add built-in support for viridis color scales #2178
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what to do about the names. Maybe just _c
and _d
?
R/scale-viridis.r
Outdated
#' @export | ||
scale_colour_viridis_discrete <- function(..., alpha = 1, begin = 0, end = 1, | ||
direction = 1, option = "D") { | ||
discrete_scale("colour", "viridis_discrete", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please use tidyverse style here: http://style.tidyverse.org/syntax.html#long-lines
R/scale-viridis.r
Outdated
#' The `viridis` scales provide color maps that are perceptually uniform in both | ||
#' color and black-and-white. They are also designed to be perceived by viewers | ||
#' with common forms of color blindness. See also | ||
#' \url{https://bids.github.io/colormap/}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use the markdown <https://bids.github.io/colormap/>
R/scale-viridis.r
Outdated
@@ -0,0 +1,68 @@ | |||
#' Viridis colour scales from viridisLite | |||
#' | |||
#' @description |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use @description
here
R/scale-viridis.r
Outdated
#' @inheritParams viridisLite::viridis | ||
#' @export | ||
#' @rdname scale_viridis | ||
viridis_pal <- function(alpha = 1, begin = 0, end = 1, direction = 1, option= "D") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this should probably go into scales. You'll need to do a PR to scales, including a bump to the version number. Then in this PR, you'll need to adjust the version of scales required in the description, and add Remotes: hadley/scales
.
I need to add some examples to the documentation here. Should I test these scales as well? |
Added a couple tests, one of which very awkwardly (!) checks that scales named |
tests/testthat/test-viridis.R
Outdated
|
||
df <- data.frame(x = 1, y = 1, z = "a") | ||
|
||
test_that("Viridis color scale gets used", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can drop this one
tests/testthat/test-viridis.R
Outdated
geom_point() | ||
p2 <- p1 + scale_colour_viridis_d() | ||
|
||
expect_false(layer_data(p1)$colour == layer_data(p2)$colour) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And can you test the specific colour here?
#' # The above are equivalent to | ||
#' v + scale_fill_gradient() | ||
#' v + scale_fill_viridis_c() | ||
scale_colour_continuous <- function(..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now think it's probably better form to use the second argument to getOption()
, rather than setting on load.
R/scale-viridis.r
Outdated
gradient_n_pal( | ||
viridis_pal(alpha, begin, end, direction, option)(6), | ||
values, | ||
space), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
)
should go on new line
NEWS.md
Outdated
@@ -1,5 +1,7 @@ | |||
# ggplot2 2.2.1.9000 | |||
|
|||
* Add built-in support for `viridis` and related color maps (@karawoo). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a brief comment about the option too?
* Begin viridis scales * Add to NEWS.md * Add documentation for continuous_scale parameters na.value and guide * Remove @description * tidyverse style for long calls to discrete_scale/continuous_scale oops * Use markdown url syntax * viridis_pal is now in the scales package * Shorten names to viridis_c and viridis_d * Add viridis examples based on the brewer examples * Add viridis tests * Update viridis tests * Use global options to control default continuous colour/fill scales * Don't set options in .onLoad() * Put closing paren on new line * Update NEWS.md
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Related to 1526. This is a first stab at adding built-in support for
viridis
color scales using theviridisLite
package. The function names are kind of long and awkward right now, but I'm not sure what better names would be. Also, should theviridis_pal
function go as a PR to scales instead of ggplot2?