-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Scale palettes from theme #5946
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
The default colour/fill scales like devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
theme(
palette.colour.discrete = pal_brewer("qual")
)
p The old way of specifying default scales through
options("ggplot2.discrete.colour" = scale_colour_viridis_d)
p Created on 2024-06-19 with reprex v2.1.0 |
Now supports all vanilla non-position aesthetics. devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
register_theme_elements(
palette.foobar.discrete = function(n) seq(0.1, 1, length.out = n),
element_tree = list(
palette.foobar.discrete = el_def(c("character", "numeric", "integer", "function"))
)
)
fallback_palette("foobar", discrete = TRUE)
#> function(n) seq(0.1, 1, length.out = n) Created on 2024-06-21 with reprex v2.1.0 I'll bump this from POC to WIP. |
Merge branch 'main' into scale_palettes # Conflicts: # R/utilities.R
Should now work with the new |
Merge branch 'main' into scale_palettes # Conflicts: # tests/testthat/test-guides.R
R/scales-.R
Outdated
|
||
# Resolve palette theme setting for this scale | ||
type <- if (scale$is_discrete()) "discrete" else "continuous" | ||
elem <- paste0("palette.", scale$aesthetics[1], ".", type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case the scale has 2 aesthetics, say c("colour", "fill")
, it will only look for palette.colour.<type>
. Shouldn't it look for the first match instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that is a good idea, thanks!
This is a proof-of-concept PR for a part of #2239 and fix #4696.
It explores setting scale palettes from the theme. When a scale has a
NULL
palette, the palette will be retrieved from the theme. In the theme, palettes can be provided for different aesthetic/discreteness combinations. Currently, only aesthetics for which this is implemented arecolour
andfill
.Some demos of this mechanism:
Created on 2024-06-18 with reprex v2.1.0
The idea is that we'd have the default scales, e.g.
scale_colour_discrete()
,scale_colour_continuous()
etc. haveNULL
palettes so that this mechanism kicks in. I'd still have to figure out how to do this in a backwards compatible manner though.