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 a theme_palette() function #16

Open
Tracked by #25
mccarthy-m-g opened this issue Nov 25, 2022 · 2 comments
Open
Tracked by #25

Add a theme_palette() function #16

mccarthy-m-g opened this issue Nov 25, 2022 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@mccarthy-m-g
Copy link
Owner

mccarthy-m-g commented Nov 25, 2022

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

@mccarthy-m-g mccarthy-m-g added the enhancement New feature or request label Nov 25, 2022
@mccarthy-m-g mccarthy-m-g mentioned this issue Dec 28, 2022
7 tasks
@EllaKaye
Copy link

Ooh, this is an interesting idea! I'm about to use {palettes} to make a package with my university's brand colours (The University of Warwick), and was planning on adding a theme_warwick() function to improve the standard look of plots (like Cara Thompson demonstrates in this talk. I hadn't thought about passing a palette argument to it, but it would be an interesting way to generalise it. If I head down that route and it works well, maybe it could be a starting point for something that would work here. Can you say more about what you had in mind for how this would work?

@mccarthy-m-g
Copy link
Owner Author

Goal

I’m pretty open on what this will look like, but my original thought was a function like:

theme_palette(palette, …)

Or maybe several theme_palette_x() functions would be nicer, one for each of the theme types provided by ggplot2 (e.g., grey, classic, minimal, etc.).

Regardless, the basic idea is that you supply a palette to change the colour of multiple elements of a theme together. It’s a convenience function so you don’t need to write out all the theme() and element_x() code just to change some colours.

Example

ggprism does this, but there they provide premade palettes and the user just picks the one they want by name: https://cran.r-project.org/web/packages/ggprism/vignettes/themes.html#theme-palettes. I wanted to generalize this idea—where the user provides a palette and the function “smartly” colours elements based on the number of colours in the palette.

For example, if a palette had one colour, then that colour would be applied to all theme elements (axis lines, ticks, etc.); but if it had more than one colour then colours would be applied more selectively to certain elements (maybe the first colour would go to axis elements, the second to gridlines, or something like that). Which elements get grouped together and given the same colour would be an opinionated decision. There would also be an upper limit on the maximum number of colours from a palette that would actually be used, probably in the 2-4 range.

API

I think it would be a good idea to handle the background and text colour separately from the palette argument with (optional?) bg and fg arguments. If they’re optional, they’d default to “white” and “black”, respectively. These arguments would accept character strings or classed colour vectors.

Other inspiration

thematic provides an interesting approach to overriding ggplot themes based on bootstrap theme elements. I haven’t dug into how it works, but some of the ideas used there might work nicely for deciding which theme elements to group together with the same colour, a max colour limit, etc.

It also works with custom themes: https://rstudio.github.io/thematic/articles/custom.html

It may also mean a theme_palette() function is somewhat redundant in the scope of things 🤷‍♂️


Cool talk! And let me know how your package goes 😄

@mccarthy-m-g mccarthy-m-g added this to the Future milestone Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants