Skip to content

Feature: Allow mapping data attributes to color (closes #20) - #37

Merged
shosaco merged 10 commits into
masterfrom
feature/color-attribute-mapping
Mar 26, 2026
Merged

shosaco merged 10 commits into
masterfrom
feature/color-attribute-mapping

Conversation

@shosaco

@shosaco shosaco commented Mar 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

Allows col.color argument to reference categorical columns, not just color codes. The package then automatically assigns colors from a palette based on the unique values in that column.

Changes

  • R/set_colors.R: Added color detection logic using col2rgb() to distinguish between RGB/hex colors and categorical data. When categorical data is detected, maps categories to colors from RColorBrewer's Set3 palette.
  • R/fix_columns.R: Preserves color_is_categorical attribute through the data processing pipeline and maintains .col_category for reference.
  • R/plot_ggplot.R: Updated to use colors directly with I() or without I(), depending if color names/codes given or not.
  • R/plot_plotly.R: Updated to use colors directly with I() or without I(), depending if color names/codes given or not.
  • R/plot_highchart.R: Updated to use colors directly with I() or without I(), depending if color names/codes given or not.
  • tests/testthat/test-set_colors.R: Added 11 unit tests covering valid colors, named colors, categorical data, NA values, and edge cases.

Benefits

  • Users can now color plots by categorical attributes without providing explicit color codes
  • Automatic color palette generation for categorical data
  • Consistent behavior across all three plotting engines (plotly, ggplot2, highcharts)

Example

pres <- data.frame(
  Position = c("President", "Vice", "President"),
  Name = c("Washington", "Adams", "Jefferson"),
  start = c("1789-03-29", "1797-02-03", "1801-02-03"),
  end = c("1797-02-03", "1801-02-03", "1809-02-03")
)

# Color by Position category - colors auto-assigned
vistime(pres, col.event = "Position", col.group = "Name", 
        col.color = "Position", title = "Presidents")

shosaco added 10 commits March 26, 2026 20:57
…/test_set_colors.R

git commit -m "feat: Allow mapping data attributes to color (issue #20)

- Add color detection logic in set_colors() to distinguish between RGB/named colors and categorical data
- Store color_is_categorical attribute through data processing pipeline
- Update plot_ggplot() to conditionally map colors as aesthetic or use directly
- Add 11 comprehensive unit tests for set_colors()"
git push origin feature/color-attribute-mapping
@shosaco
shosaco merged commit d885a5a into master Mar 26, 2026
6 checks passed
@shosaco
shosaco deleted the feature/color-attribute-mapping branch March 26, 2026 21:01
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.

1 participant