Skip to content

Jitter layering fails for boxplot/violin #559

@grantmcdermott

Description

@grantmcdermott

Adding a jitter layer on top of a boxplot or violin plot errors with a row mismatch:

Error in `[[<-.data.frame`:
! replacement has 123 rows, data has 150

Originally spotted in: joshuaulrich/microbenchmark#52 (comment). But here's a manual reprex:

library(tinyplot)

# boxplot + jitter
tinyplot(Sepal.Length ~ Species, data = iris, type = "boxplot")

tinyplot_add(type = "jitter", cex = 0.5, alpha = 0.3)
#> Error in `[[<-.data.frame`:
#> ! replacement has 123 rows, data has 150
# violin + jitter (flipped)
tinyplot(Sepal.Length ~ Species, data = iris, type = "violin", flip = TRUE)

tinyplot_add(type = "jitter", cex = 0.5, alpha = 0.3)
#> Error in `[[<-.data.frame`:
#> ! replacement has 123 rows, data has 150

Created on 2026-03-23 with reprex v2.1.1

The bug is in align_layer(). When the added layer'sxlabs mapping is identical to the original layer's, the remapping logic should be skipped. Instead, it tries to index names(xlabs_layer) with non-integer (jittered) x values, which causes names()[0] to return character(0) and silently shrink the result vector.

Hopefully a simple fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions