You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Smarter palette choice / recycling with many groups #352
Updating lecture slides and a year ago I made (screenshotting from the pdf here)
whereas right now with 0.3.0 (under R 4.4.3, everything else CRAN current) I get
which isn't fair. Moments ago I used the branch @grantmcdermott worked on for violins, and that filled seven or so of the categories. Code is a pretty plain and possibly even from your documentation:
changed the title [-]datasaurus malfunctions when fed a character variable instead of a factor[/-][+]Faceting malfunctions when fed a character variable instead of a factor[/+]on Mar 29, 2025
I seem to recall that Grant recently did some improvements regarding this - but maybe I'm wrong. In any case, with the current R-universe version, you can use the character variable dataset directly. It's just that the default qualitative palette does not provide enough colors but changing to "Viridis" or "Dark 3" are alternatives that work.
That's likely what I saw re-rendering the slide: by not adding an appropriate palette, I 'ran out of colours', got some empty boxes and it all went downhill from there.
Do as you please: close as non-issue, keep open as reminder to nag user when not a factor, or more factor levels that colors, or ...
I think the problem here is actually that something got overwritten or missed when we added our tinytheme logic. E.g., the following works because the palette.qualitiative correctly upgrades to palette.sequential when a high number of groups are detected.
Thanks, Grant, I was wondering about the same when playing around with the example today:
## data and package
dd = datasauRus::datasaurus_dozen
library("tinyplot")
## default: partially empty, no warning
plt(y ~ x | dataset, facet = "by", data = dd, pch = 18, legend = FALSE)
## insufficient palette: partially empty, with warning
plt(y ~ x | dataset, facet = "by", data = dd, pch = 18, legend = FALSE, palette = "Okabe-Ito")
## sufficient palette: ok
plt(y ~ x | dataset, facet = "by", data = dd, pch = 18, legend = FALSE, palette = "Dark 3")
## theme with palette.qualitative (insufficient) & palette.sequential: ok (failover to sequential)
tinytheme("minimal")
plt(y ~ x | dataset, facet = "by", data = dd, pch = 18, legend = FALSE)
## theme with palette.qualitative (sufficient by name) & palette.sequential: still failover to sequential
tinytheme("minimal", palette.qualitative = "Dark 3")
plt(y ~ x | dataset, facet = "by", data = dd, pch = 18, legend = FALSE)
## theme with palette.qualitative (sufficient by vector) & palette.sequential: ok
tinytheme("minimal", palette.qualitative = hcl.colors(13, "Dark 3"))
plt(y ~ x | dataset, facet = "by", data = dd, pch = 18, legend = FALSE)
## theme with palette.qualitative (insufficient) & palette.sequential (too short): recycling, with warning
tinytheme("minimal", palette.sequential = hcl.colors(9))
plt(y ~ x | dataset, facet = "by", data = dd, pch = 18, legend = FALSE)
So the situation seems to be:
The default is partially empty because the default theme does not set any palette.qualitative or palette.sequential.
The warning is only triggered when going through the palette argument of tinyplot() but not when the theme sets something insufficient.
The heuristics with which the number of colors is determined, as provided by palette.qualitative, only work when this is a color vector but not a color name.
Too short sequential palettes are recycled rather than interpolated via color ramp.
Potential improvements:
If no palette.qualitative and no palette.sequential are set, use recycling rather than empty colors, possibly with a warning.
Set palette.qualitative and palette.sequential also for default theme.
If a palette.colors() palette is specified, determine its number of colors via length(palette.colors(palette = palette)). (For a hcl.colors() palette, any number of colors can be obtained.)
Avoid hard-coded 8 in some computations. This comes from length(palette()) with the default R4 palette. But the user could change this, of course.
If palette.sequential is a color vector, generally use interpolation via color ramp, no matter whether there are enough or too few colors in the color vector.
Reacted by Grant McDermott and Vincent Arel-Bundock
changed the title [-]Faceting malfunctions when fed a character variable instead of a factor[/-][+]Smarter palette choice / recycling with many groups[/+]on Apr 21, 2025
(Note slightly different call to your original, leveraging the facet = "by" syntax, which also automatically converts the grouping variable to a factor.)
Activity
[-]datasaurus malfunctions when fed a character variable instead of a factor[/-][+]Faceting malfunctions when fed a character variable instead of a factor[/+]zeileis commentedon Mar 29, 2025
I seem to recall that Grant recently did some improvements regarding this - but maybe I'm wrong. In any case, with the current R-universe version, you can use the character variable
datasetdirectly. It's just that the default qualitative palette does not provide enough colors but changing to"Viridis"or"Dark 3"are alternatives that work.eddelbuettel commentedon Mar 29, 2025
That's likely what I saw re-rendering the slide: by not adding an appropriate palette, I 'ran out of colours', got some empty boxes and it all went downhill from there.
Do as you please: close as non-issue, keep open as reminder to nag user when not a factor, or more factor levels that colors, or ...
grantmcdermott commentedon Mar 30, 2025
I think the problem here is actually that something got overwritten or missed when we added our tinytheme logic. E.g., the following works because the palette.qualitiative correctly upgrades to palette.sequential when a high number of groups are detected.
Created on 2025-03-30 with reprex v2.1.1
Hopefully an easy fix. I'll take a look after I get to some other issues.
zeileis commentedon Mar 31, 2025
Thanks, Grant, I was wondering about the same when playing around with the example today:
So the situation seems to be:
palette.qualitativeorpalette.sequential.paletteargument oftinyplot()but not when the theme sets something insufficient.palette.qualitative, only work when this is a color vector but not a color name.Potential improvements:
palette.qualitativeand nopalette.sequentialare set, use recycling rather than empty colors, possibly with a warning.palette.qualitativeandpalette.sequentialalso for default theme.palette.colors()palette is specified, determine its number of colors vialength(palette.colors(palette = palette)). (For ahcl.colors()palette, any number of colors can be obtained.)8in some computations. This comes fromlength(palette())with the defaultR4palette. But the user could change this, of course.palette.sequentialis a color vector, generally use interpolation via color ramp, no matter whether there are enough or too few colors in the color vector.[-]Faceting malfunctions when fed a character variable instead of a factor[/-][+]Smarter palette choice / recycling with many groups[/+]grantmcdermott commentedon May 23, 2025
@eddelbuettel Thanks again for flagging. This is fixed in
tinyplotv0.4.0, which just landed on CRAN.Created on 2025-05-22 with reprex v2.1.1
(Note slightly different call to your original, leveraging the
facet = "by"syntax, which also automatically converts the grouping variable to a factor.)