Skip to content
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

Merged
merged 15 commits into from
Jul 13, 2017

Conversation

karawoo
Copy link
Member

@karawoo karawoo commented Jun 22, 2017

Related to 1526. This is a first stab at adding built-in support for viridis color scales using the viridisLite package. The function names are kind of long and awkward right now, but I'm not sure what better names would be. Also, should the viridis_pal function go as a PR to scales instead of ggplot2?

@karawoo karawoo requested a review from hadley June 22, 2017 16:17
Copy link
Member

@hadley hadley left a 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?

#' @export
scale_colour_viridis_discrete <- function(..., alpha = 1, begin = 0, end = 1,
direction = 1, option = "D") {
discrete_scale("colour", "viridis_discrete",
Copy link
Member

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

#' 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/}.
Copy link
Member

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/>

@@ -0,0 +1,68 @@
#' Viridis colour scales from viridisLite
#'
#' @description
Copy link
Member

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

#' @inheritParams viridisLite::viridis
#' @export
#' @rdname scale_viridis
viridis_pal <- function(alpha = 1, begin = 0, end = 1, direction = 1, option= "D") {
Copy link
Member

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.

@karawoo
Copy link
Member Author

karawoo commented Jun 23, 2017

I need to add some examples to the documentation here. Should I test these scales as well?

@karawoo
Copy link
Member Author

karawoo commented Jun 26, 2017

Added a couple tests, one of which very awkwardly (!) checks that scales named "viridis_d" or "viridis_c" have been added. The second test checks that point color changes when adding a viridis scale.


df <- data.frame(x = 1, y = 1, z = "a")

test_that("Viridis color scale gets used", {
Copy link
Member

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

geom_point()
p2 <- p1 + scale_colour_viridis_d()

expect_false(layer_data(p1)$colour == layer_data(p2)$colour)
Copy link
Member

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(...,
Copy link
Member

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.

gradient_n_pal(
viridis_pal(alpha, begin, end, direction, option)(6),
values,
space),
Copy link
Member

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).
Copy link
Member

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?

@karawoo karawoo merged commit 77fc5c7 into tidyverse:master Jul 13, 2017
karawoo added a commit to karawoo/ggplot2 that referenced this pull request Jul 14, 2017
* 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
@lock
Copy link

lock bot commented Jan 18, 2019

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/

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants