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

Improve scales behavior for ordered factors #2179

Merged
merged 9 commits into from
Jul 18, 2017

Conversation

karawoo
Copy link
Member

@karawoo karawoo commented Jun 23, 2017

Related to 1526. Ordered factors and regular factors currently behave pretty much the same, but this is not always appropriate. I've added new ordinal scales for size, alpha, and shape so that

  • scale_shape_ordinal throws a warning (but scale_shape_discrete still does not)
  • scale_size_ordinal does not throw a warning (but scale_size_discrete still does)
  • scale_alpha_ordinal still does not throw a warning (but scale_alpha_discrete does)

Still to do:

  • new colour/fill scales for ordinal variables (possibly with viridis)
  • test the new behavior

@karawoo
Copy link
Member Author

karawoo commented Jun 23, 2017

Some of this could be DRY-er, e.g.

scale_size_ordinal <- function(...) {
  suppressWarnings(scale_size_discrete(...))
}

might be better, or change the discrete version to

scale_size_discrete <- function(...) {
  warning("Using size for a discrete variable is not advised.", call. = FALSE)
  scale_size_ordinal(...)
}

@karawoo
Copy link
Member Author

karawoo commented Jun 24, 2017

If we're going to go with viridis for ordinal scales then we may not need any additional functions, but if not then perhaps something like this should go in the scales package:

seq_ordinal_pal <- function(low, high) {
  function(n) {
    v <- seq(0, 1, length.out = n)
    scales::colour_ramp(c(low, high))(v)  
  }
}

Then scale_colour_ordinal would be:

scale_colour_ordinal <- function(..., low = "#132B43", high = "#56B1F7",
                                 na.value = "grey50") {
  discrete_scale(
    "colour",
    "sequence",
    seq_ordinal_pal(low, high),
    na.value = na.value,
    ...
    )
}

which produces output like this:

d <- diamonds[sample(nrow(diamonds), 1000), ]

ggplot(d, aes(carat, price)) +
  geom_point(aes(colour = cut))

diamonds

@has2k1
Copy link
Contributor

has2k1 commented Jun 24, 2017

You need to be careful (or document the aberrations), depending on how you interpolate between the high and low colours of a colourmap, not all generated colours may be part of the continuous version of the colourmap.

This will definitely affect colourmaps that aim to perceptually-uniform, like viridis.

@karawoo
Copy link
Member Author

karawoo commented Jun 28, 2017

@hadley GitHub isn't letting me request a review but can you tell me what you think of this? I can't remember if we decided on Monday what to do about the ordinal color scales.

@karawoo karawoo requested a review from hadley June 29, 2017 19:04
@hadley
Copy link
Member

hadley commented Jul 3, 2017

Looks good so far. We should make a decision about ordinal colour scales too. Using viridis seems like a reasonable choice to me.

@karawoo
Copy link
Member Author

karawoo commented Jul 3, 2017

Trying @jennybc's edits from #2189 in this PR to see if it makes codecov chill out.

Edit: wait is it now saying that my last commit (before merging the new changes) passed all the checks? I swear codecov was failing before...

@karawoo
Copy link
Member Author

karawoo commented Jul 5, 2017

Using viridis sounds good to me, so once #2178 gets merged I can add that here.

@karawoo
Copy link
Member Author

karawoo commented Jul 14, 2017

I removed Jenny's codecov-related commits and added scale_fill_ordinal and scale_colour_ordinal, which both default to viridis.

@karawoo
Copy link
Member Author

karawoo commented Jul 17, 2017

@hadley one quick question before I merge -- since we're making viridis the default for ordered factors, should viridisLite go in Imports rather than Suggests?

@hadley
Copy link
Member

hadley commented Jul 18, 2017

Yes!

@karawoo karawoo merged commit 00ecd36 into tidyverse:master Jul 18, 2017
@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.

3 participants