mccarthy-m-g / palettes Goto Github PK
View Code? Open in Web Editor NEWMethods for working with colour palettes in R
Home Page: https://mccarthy-m-g.github.io/palettes/
License: Other
Methods for working with colour palettes in R
Home Page: https://mccarthy-m-g.github.io/palettes/
License: Other
Since colours in colour vectors can be named, it might be nice to also output those names when they’re there (maybe as a muted grey colour in parentheses next to the hex code?).
I would probably exclude this when printing in tibbles though, because the output would be too wide there.
For developers to create a new palettes package with all the boilerplate set up.
Can probably wrap usethis::create_from_github()
and hard code it to a template repository. Only catch is this would require an Internet connection.
Or I can try to build everything manually using the appropriate use_
functions. use_template()
looks like it will fit the bill.
Add a ggpalette()
function that takes a palette or color as input and returns a ggplot themed like a swatch as output. Something like: https://images.app.goo.gl/j9Vu9NRJuC5vvMHr7
Doing this would also require creating as.dataframe()
methods for the palette and color classes.
As far as handling multiple colours, facets would probably work well
Hi Michael,
I'm working through the code in the Creating a Colour Package vignette and you make use of a pal_brewer
function. When I run the code, I get an error: could not find function "pal_brewer"
. I can't find the function anywhere in the palettes
code base nor in any other R package. What am I missing?
Thanks!
Since coloured cli output doesn’t just work in R Markdown and Quarto, it might be nice to show how to get coloured colour previews in these outputs using {fansi}, like I did here: https://tidytales.ca/posts/2022-12-20_palettes/
Additive vs subtractive mixing https://www.mathscareers.org.uk/colour-calculations/ (maybe I should use these methods separately for + and – operators)
Modifying RGB colour space to an additive space https://stackoverflow.com/a/41598885/16844576
Function to invert a colour, and round it to black or white https://stackoverflow.com/a/35970186
Function to mix colours
Colour quantization https://www.r-bloggers.com/2016/01/color-quantization-in-r/
Add function to check the accessibility of a palette. The cvd
argument in colorspace::swatchplot()
does this. See http://colorspace.r-forge.r-project.org/articles/palette_visualization.html
Maybe also consider extending the S3 classes to contain info on whether a palette is accessible (as an attribute or something)
e.g., pal_color()
I notice that the code base has functions for lightening/darkening colour palettes and vectors but these are not exported. Any reason why not? They would be nice to have!
New function for creating bivariate colour palettes that can be used with the biscale package. Main points of consideration are:
Right now the output in the README only shows the colour symbol and hex code, but it doesn’t show the <palettes_colour[1]>
class above that like it’s supposed to (e.g., it should look how the pal_palette()
output looks).
Prepare for release:
git pull
devtools::build_readme()
urlchecker::url_check()
devtools::check(remote = TRUE, manual = TRUE)
devtools::check_win_devel()
devtools::check_mac_release()
rhub::check_for_cran()
revdepcheck::revdep_check(num_workers = 4)
cran-comments.md
git push
Submit to CRAN:
usethis::use_version('minor')
devtools::submit_cran()
Wait for CRAN...
git push
usethis::use_github_release()
usethis::use_dev_version()
git push
For example, pal_palette()
instead of palette()
to avoid masking base R functions
and add alias for "color" instead of that being the default
This can be accomplished using the crayon package; however, this may be unnecessary given that RStudio 1.6 might support this natively rstudio/rstudio#5424.
Edit: RStudio support seems to be for the editor pane, not output, so maybe still worth implementing myself
{palettes} could use a better hex sticker! Open to contributions for this 😄
I made the current one using some lightly modified free to use vector graphics. It’s okay, but could be a lot better.
With testthat: https://vctrs.r-lib.org/articles/s3-vector.html#testing
Where you supply a palette and it colours theme elements based on the number of colours in the palette (e.g., if 1 colour, all elements take on that colour, if 2 then colour 1 is the primary and colour 2 is the secondary, etc.).
theme_prism
from ggprism
does something similar (picking a theme based on a palette): https://cran.r-project.org/web/packages/ggprism/vignettes/themes.html, https://github.com/csdaw/ggprism/blob/master/R/theme_prism.R
The skittle/smartie swatches for colour palettes in this paper look nice and it would be cool to have the option to print palettes in the console like on page 8, for users who might want a compact display.
It would be controlled with a new option (palettes.print_style
?), and the implementation would basically just involve not printing the hex codes—only symbols—and restricting the number of colours to 10 per line before a line break.
The print styles could have the following names: “default” (the current style), “compact”. Or the default could just be NULL
rather than an explicit value. Either way, using strings here would be a good idea rather than a boolean in case I want to add new print styles in the future.
https://www.tidyverse.org/blog/2022/12/purrr-1-0-0/
map_dfc() and map_dfr() (and their map2 and pmap variants) have been superseded in favour of using the appropriate map function along with list_rbind() or list_cbind().
There’s also a new map_vec()
function that might be helpful
The info text would provide a hint on how to fix the error. Something like (as separate “i” bullets):
grDevices::colors()
?It would be nice to make this context aware, so only the relevant hint shows up if invalid colours are specified in a single way, and both show up if there are both invalid hex strings and colour names.
https://cli.r-lib.org/reference/num_ansi_colors.html
In RStudio and pkgdown colours just work, but for other formats (e.g., Quarto) the number of colours needs to be increased from the default of 8. It’s easy for a user to do this, but it might be nice to set these options/environment variables up internally.
The documentation says it can be error prone, so will need some testing to avoid problems.
e.g., lighten.palettes_palette()
Example here https://blog.earo.me/2019/11/03/practical-guide-to-s3/
e.g., rgb_to_colour()
, hsl_to_colour()
Like in https://davidchall.github.io/ipaddress/articles/ipaddress-classes.html
Edit: Need to decide if this is necessary, or if it’s unnecessary bloat
Some examples here:
https://meghan.rbind.io/blog/2022-10-11-creating-custom-color-palettes-with-ggplot2/
https://www.garrickadenbuie.com/blog/custom-discrete-color-scales-for-ggplot2/
https://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2
These can also be used as default options with:
options(
ggplot2.discrete.colour = function(...) scale_colour_viridis_d(..., begin = 0.15, end = 0.85),
ggplot2.discrete.fill = function(...) scale_fill_viridis_d(..., begin = 0.15, end = 0.85),
ggplot2.continuous.colour = function(...) scale_colour_viridis_c(..., begin = 0.15, end = 0.85),
ggplot2.continuous.fill = function(...) scale_fill_viridis_c(..., begin = 0.15, end = 0.85)
)
TODO
I’m just using the symbols from cli: https://cli.r-lib.org/reference/symbol.html. So any of these would be available.
Code-wise it could be as simple as using getOption()
with the bullet as a default. I would also need to add something in the creating a package vignette to show developers how to change the symbol their package uses though.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.