Named vector color support for desplot & ggdesplot #16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Implements named vector support for
col.regionsandcol.textparameters in bothdesplot()andggdesplot().Closes #10
This enhancement allows users to specify colors that match factor levels by name rather than positional order, similar to
ggplot2::scale_color_manual().Problem
As described in #10, colors previously had to be provided in the same order as factor levels:
If you wanted to specify colors in a different order, you had to manually reorder them to match the factor levels.
Solution
Named vectors now allow order-independent color specification:
Features
✅ Order-independent matching: Colors match factor levels by name, not position
✅ Graceful fallback: Missing names trigger a warning and fall back to positional matching with R's standard vector recycling
✅ Backward compatible: Unnamed vectors work exactly as before
✅ Works for both: desplot() (lattice) and ggdesplot() (ggplot2)
✅ Two parameters: Supports both col.regions (fill colors) and col.text (outline colors)
Implementation Details
Modified files:
R/desplot.R: Lines 413-429 (col.regions), Lines 565-580 (col.text)
R/ggdesplot.R: Lines 262-278 (col.regions), Lines 407-422 (col.text)
test_named_colors.R: Comprehensive test suite
Edge cases handled:
✅ Extra names (ignored)
✅ Partial names (warns and falls back)
✅ NULL names (original behavior)
Reprex
Created on 2025-11-14 with reprex v2.1.1