Skip to content

Density singletons - #687

Merged
grantmcdermott merged 9 commits into
mainfrom
density-singletons
Aug 22, 2026
Merged

Density singletons#687
grantmcdermott merged 9 commits into
mainfrom
density-singletons

Conversation

@grantmcdermott

@grantmcdermott grantmcdermott commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Fixes #300

As note in the issue thread, a simple fix for this was just filtering out the singletons preemptively:

datapoints = Filter(function(k) nrow(k) > 1, datapoints)

... But I worry that the silent dropping might be misleading. So I ended up rolling a more sophisticated version in order to allow the possibility of emitting a nice user warning, which is controllable through the new type-level singletons argument. By default, the warning is on:

pkgload::load_all("~/Documents/Projects/tinyplot/")
#> ℹ Loading tinyplot
tinytheme("clean", facet.prefix = TRUE, facet.axes = "outer")
tinyplot(~ mpg, facet = vs ~ gear, data = mtcars, type = "density")
#> Warning: Dropped 1 singleton group(s). Densities require at least 2 observations.

Example with another density type (here: violin plots):

tinyplot(mpg ~ cyl, facet = ~ gear, data = mtcars, type = "violin")
#> Warning: Dropped 2 singleton group(s). Densities require at least 2 observations.

It's also possible to silence the warning with singletons = "drop"). Our first example again:

tinyplot(~ mpg, facet = cyl ~ gear, data = mtcars,
        type = type_density(singletons = "drop"))

tinytheme()

Created on 2026-08-21 with reprex v2.1.1

- fancier version with warnings etc.
Singleton groups are dropped either way, but staying silent about it
risks the missing panel (or violin, or ridge line) reading as "no data
here" rather than "only one observation here". A survey of the standard
datasets suggests this fires rarely -- only on small, unbalanced data
like mtcars -- so the warning should be high-signal rather than noise.
It is also emitted once per call, not on replay, so window resizes do
not repeat it.

Pass singletons = "drop" for the old quiet behaviour.

Claude-Session: https://claude.ai/code/session_014YKk45eRcv5MkVuu9LkDeW
@grantmcdermott
grantmcdermott merged commit 2c28001 into main Aug 22, 2026
3 checks passed
@grantmcdermott
grantmcdermott deleted the density-singletons branch August 22, 2026 03:03
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.

Singleton (empty) groups could resolve more gracefully for density plots

1 participant