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

[Suggestion] Three colour gradient: compute colours through the scales package #61

zeehio opened this issue May 12, 2016 · 2 comments


Copy link

zeehio commented May 12, 2016

Hi, nice work on the package!

I recently discovered your package. I have a similar package named condformat with much less features (I can only change background gradients) but with PDF output in knitr.

Looking at your source code, I found that you compute the gradient colours with the normalize function. While I was working on my package I found really useful the set of gradients used in ggplot2, that are created using the scales package. It offers easy to build color scales:

# Three colour gradient
data_vector <- c(-10, -4, -2, -1, 1, 4, 10)
# Create palette: A gradient going from red to blue through white:
colour_scale <- scales::div_gradient_pal(low = "red", mid = "white", high = "blue")
# Rescale data:
scaled_data <- scales::rescale_mid(data, mid = 0)
# Apply colour scale to scaled data:
data_colours <- colour_scale(scaled_data)
# Print colour names:
# [1] "#FF0000" "#FFB29A" "#FFD9CB" "#FFECE5" "#F1E7FF" "#C4A2FF" "#0000FF"

The default palettes and options are well chosen, for instance they offer palettes for colorblind people (through the dichromat_pal function).

Feel free to use it, if you like it. It saved me a lot of work and I feel it provides a well thought interface.

Copy link

Hi zeehio,

In order for your example to work correctly I wonder if your line...

# Rescale data:
scaled_data <- scales::rescale_mid(data, mid = 0)

should read...

# Rescale data:
scaled_data <- scales::rescale_mid(data_vector, mid = 0)

ie. replace data with data_vector

Copy link

zeehio commented May 17, 2016

Yes, sorry!

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

No branches or pull requests

2 participants