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

create ggplot2 compatible wrappers? #5

Closed
japhir opened this issue Nov 13, 2020 · 15 comments
Closed

create ggplot2 compatible wrappers? #5

japhir opened this issue Nov 13, 2020 · 15 comments

Comments

@japhir
Copy link
Contributor

japhir commented Nov 13, 2020

The package viridis has some helper functions to allow the colour scale to be used effectively in ggplot2.

It would be great if this package would have some as well!

I just wrote one for the fill scale:

library(cmocean)
library(ggplot2)
library(tibble)

scale_fill_cmocean <- function(..., alpha = 1, begin = 0, end = 1,
                               direction = 1, discrete = FALSE,
                               option = "thermal") {
  if (discrete) {
    discrete_scale("fill", "cmocean", cmocean::cmocean(name = option, start = begin, end = end), ...)
  } else {
    scale_fill_gradientn(colours = cmocean::cmocean(name = option, start = begin, end = end)(n = 256), ...)
  }
}

dat <- tibble::tibble(a = 1:10, b = 11:20, c = stats::rnorm(10), d = sample(letters[1:3], 10, TRUE))

dat %>%
  ggplot(aes(x = a, y = b, fill = d)) +
  geom_raster() +
  scale_fill_cmocean(discrete = TRUE)

dat %>%
  ggplot(aes(x = a, y = b, fill = c)) +
  geom_raster() +
  scale_fill_cmocean()

Created on 2020-11-13 by the reprex package (v0.3.0)

This works, but doesn't yet incorporate an alpha parameter yet.

@aitap
Copy link
Owner

aitap commented Nov 13, 2020 via email

@japhir
Copy link
Contributor Author

japhir commented Nov 13, 2020

I would be happy if you could do this! I'm in the last year of my PhD and shouldn't be doing these kinds of fun programming exercises ;-).

Adding the dependency check sounds great.

aitap added a commit that referenced this issue Nov 13, 2020
The functions scale_color_cmocean, scale_colour_cmocean, scale_fill_cmocean
are supposed to mimic similarly-named functions from the viridis package.
@aitap
Copy link
Owner

aitap commented Nov 13, 2020

What do you think about the API we have as of e55f9bb? (there's a package file ready for install.packages at https://github.com/aitap/cmocean/releases/download/v0.3pre/cmocean_0.2.tar.gz). If you think it's good, I will prepare some minor changes I've been planning, increment the version and submit it to CRAN in the following days.

@japhir
Copy link
Contributor Author

japhir commented Nov 15, 2020

That looks great to me!

In viridis they do implement the two col[o]ur functions separately, probably so that roxygen notices them both and creates docs for both? I haven't checked by trying to install the package to see if it does it correctly.

#' @rdname scale_viridis
#'
#' @importFrom ggplot2 scale_fill_gradientn scale_color_gradientn discrete_scale
#'
#' @export
scale_color_viridis <- function(..., alpha = 1, begin = 0, end = 1, direction = 1,
                                discrete = FALSE, option = "D") {
  if (discrete) {
    discrete_scale("colour", "viridis", viridis_pal(alpha, begin, end, direction, option), ...)
  } else {
    scale_color_gradientn(colours = viridisLite::viridis(256, alpha, begin, end, direction, option), ...)
  }
}

#' @rdname scale_viridis
#' @aliases scale_color_viridis
#' @export
scale_colour_viridis <- scale_color_viridis

Also, and this is probably not related to this issue but might be easy to implement, it may be good to do some input checking in the base cmocean fuction, like in viridisLite::viridis: https://github.com/sjmgarnier/viridisLite/blob/b44c8792478c5406256760f734b47ba70bcce904/R/viridis.R#L145

@japhir
Copy link
Contributor Author

japhir commented Nov 15, 2020

Ah never mind, I just saw that you're not using roxygen but writing the docs in a separate Rd file by hand. :)

@japhir
Copy link
Contributor Author

japhir commented Nov 15, 2020

looks like you already check the others, so I added a name check :).

Wasn't sure about the indentation you use.

@aitap
Copy link
Owner

aitap commented Nov 15, 2020 via email

@richardsc
Copy link
Contributor

I second being consistent about allowing version specification. There's not a super high chance that the colours might change, but small tweaks are possible (maybe I'll check in with the cmocean creator to get a sense of what future plans might be coming down the pipeline).

@japhir
Copy link
Contributor Author

japhir commented Nov 15, 2020

Hmm I guess that it would be important for future updates and reproducible figures... I agree that including it with the proper defaults is the right approach.

@aitap
Copy link
Owner

aitap commented Nov 16, 2020 via email

@aitap
Copy link
Owner

aitap commented Nov 17, 2020 via email

@japhir
Copy link
Contributor Author

japhir commented Nov 17, 2020

looks good to me. It might be useful to write some tests at some point to make sure that future updates don't break any of the intended functionality, but for now it's relatively simple so I would postpone that until the next release. 👍

@aitap
Copy link
Owner

aitap commented Nov 17, 2020

Thanks, submitted v0.3 to CRAN.

@aitap aitap closed this as completed Nov 17, 2020
@richardsc
Copy link
Contributor

Sorry, was a bit behind checking up on this, but it looks great to me. Thanks!

@aitap
Copy link
Owner

aitap commented Nov 18, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants