Skip to content

allow palette.qualitative specification via function#594

Merged
grantmcdermott merged 7 commits into
mainfrom
palette-theme
May 31, 2026
Merged

allow palette.qualitative specification via function#594
grantmcdermott merged 7 commits into
mainfrom
palette-theme

Conversation

@zeileis
Copy link
Copy Markdown
Collaborator

@zeileis zeileis commented May 25, 2026

Fixes #593

In order to support function specifications of palette.qualitative in themes, I think that we only need to make sure that match_palette_name(theme_palette, ...) is only used if theme_palette is a character string (but not a function).

Then both palette.qualitative and palette.sequential can be a function in a theme:

my_pal <- colorRampPalette(c("red", "yellow", "blue"))
tinytheme("clean2", palette.qualitative = my_pal, palette.sequential = my_pal)
tinyplot(1:10, by = factor(1:10)) ## qualitative
tinyplot-theme-palette2
tinyplot(1:10, by = 1:10) ## sequential
tinyplot-theme-palette1

@grantmcdermott
Copy link
Copy Markdown
Owner

Thanks @zeileis. I did some testing and it mostly looks good. The one exception was for passing palette.sequential as part of a list argument to an ephemeral theme.

tinyplot(
  1:10,
  by = 1:10,
  theme = list(palette.sequential = my_pal)
)

Would it work to change l. 304 here

pal_theme = get_tpar("palette.qualitative", default = NULL)

to

  pal_theme = if (ordered || gradient) {
    get_tpar("palette.sequential", default = NULL) %||% get_tpar("palette.qualitative", default = NULL)
  } else {
    get_tpar("palette.qualitative", default = NULL)
  }

?

(I haven't tested)

@zeileis
Copy link
Copy Markdown
Collaborator Author

zeileis commented May 31, 2026

Makes sense. I have omitted the %||% addition, though. Then the fallback is handled in resolve_palette_colors().

Should we add the analogous computation in by_bg()?

@grantmcdermott
Copy link
Copy Markdown
Owner

Should we add the analogous computation in by_bg()?

Good call. I can't think of a reason not to.

@grantmcdermott
Copy link
Copy Markdown
Owner

@zeileis once the by_bg component has been added, could you add some shapshot tests? Happy to merge after that.

@zeileis
Copy link
Copy Markdown
Collaborator Author

zeileis commented May 31, 2026

I added the same palette handling in by_bg() as in by_col(). But my impression is that this wouldn't have been necessary due to the way fallbacks are handled. At least I wasn't able to create an example where the old bg handling failed.

Also added some tinytests/snapshots.

@grantmcdermott
Copy link
Copy Markdown
Owner

At least I wasn't able to create an example where the old bg handling failed.

Thanks. If memory serves, this example was failing earlier but is working now.

my_pal = colorRampPalette(c("red", "yellow", "blue"))
i = 4*(0:10)
tinyplot(
  xmin = 100+i, ymin = 300+i, xmax = 150+i, ymax = 380+i,
  by = i, fill = 0.2,
  type = "rect",
  theme = list("default", palette.sequential = my_pal)
)

Either way, agree that having some backup/redundancy doesn't hurt.

P.S. I'm not sure why the added snapshot tests are failing on the CI. I just tested locally and everything is passing, so it might be an annoying false positive case. I have to step out for a bit, but can investigate when I"m back.

@grantmcdermott
Copy link
Copy Markdown
Owner

Figured it out; just an innocuous main branch difference.

@grantmcdermott grantmcdermott merged commit d38b4af into main May 31, 2026
3 checks passed
@zeileis
Copy link
Copy Markdown
Collaborator Author

zeileis commented May 31, 2026

Excellent, thanks for the example and for fixing the CI failure! 💪

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

Successfully merging this pull request may close these issues.

palette.qualitative does not accept a function

2 participants