Skip to content

Support | 0 + var for row faceting in formula syntax#19

Merged
yihui-bot merged 3 commits intomainfrom
copilot/implement-g2-facet-feature
Apr 2, 2026
Merged

Support | 0 + var for row faceting in formula syntax#19
yihui-bot merged 3 commits intomainfrom
copilot/implement-g2-facet-feature

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

Extends the formula interface in g2() so that | 0 + var produces a row facet (encode$y), mirroring the R convention where 0 suppresses the default (column) term.

  • | var → column facet (encode$x) — unchanged
  • | 0 + var → row facet (encode$y) — new
  • | var1 + var2 → both — unchanged
# Column facet (existing)
g2(mtcars, hp ~ mpg | cyl)

# Row facet (new)
g2(mtcars, hp ~ mpg | 0 + cyl)

Changes

  • R/utils.R: parse_formula() detects leading '0' in facet terms and shifts encoding from x to y. Bare | 0 (no variable after) is handled gracefully — no facet is created.
  • tests/testit/test-utils.R: Tests for | 0 + var and the | 0 edge case.
  • vignettes/gglite.Rmd: Added formula faceting examples to the Facets section using penguins data — column facet (| species), row facet (| 0 + island), and both (| species + island).
  • DESCRIPTION: Version bump to 0.0.11.

Copilot AI and others added 2 commits April 2, 2026 06:33
Agent-Logs-Url: https://github.com/yihui/gglite/sessions/3a6cd822-fa4d-4ac0-b226-eee4af9726f3

Co-authored-by: yihui-bot <264330240+yihui-bot@users.noreply.github.com>
Comment thread R/utils.R
Comment on lines 118 to 123
#' - `y ~ x` maps to `list(x = 'x', y = 'y')`
#' - `~ x` maps to `list(x = 'x')`
#' - `~ x1 + x2 + x3` maps to `list(position = c('x1', 'x2', 'x3'))`
#' - `y ~ x | z` adds faceting by `z`
#' - `y ~ x | z` adds faceting by `z` (columns)
#' - `y ~ x | 0 + z` adds faceting by `z` (rows)
#' - `y ~ x | z1 + z2` adds faceting by `z1` (columns) and `z2` (rows)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these need to be mentioned in the vignette, and ideally also give examples for each case (can use penguins data since it has multiple categorical variables)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added formula faceting examples to the vignette's Facets section using penguins data — column facet (| species), row facet (| 0 + island), and both (| species + island). See 807ffb1.

Comment thread NEWS.md Outdated
Comment on lines +3 to +5
- The formula syntax in `g2()` now supports `| 0 + var` for row faceting (e.g.,
`y ~ x | 0 + var`), in addition to the existing `| var` for column faceting.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow copilot instructions: don't include news for v0.1 after each PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted NEWS.md to original state in 807ffb1.

Comment thread R/utils.R Outdated
Comment on lines +155 to +161
row_only = facet_terms[1] == '0'
if (row_only) facet_terms = facet_terms[-1]
if (length(facet_terms) >= 1) {
if (row_only) enc$y = facet_terms[1] else enc$x = facet_terms[1]
}
if (!row_only && length(facet_terms) >= 2) enc$y = facet_terms[2]
if (length(enc)) facet = list(type = 'facetRect', encode = enc)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this code more compact? the logic is not easy to digest

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified to 4 lines using setNames + head — a leading '0' sets names to 'y', otherwise c('x', 'y'), then terms are matched to names by position. See 807ffb1.

…e examples

Agent-Logs-Url: https://github.com/yihui/gglite/sessions/a4f72763-bd38-4357-afa0-7214c0c67f75

Co-authored-by: yihui-bot <264330240+yihui-bot@users.noreply.github.com>
Copilot AI requested a review from yihui-bot April 2, 2026 06:59
@yihui-bot yihui-bot marked this pull request as ready for review April 2, 2026 07:00
@yihui-bot yihui-bot merged commit f3eecfa into main Apr 2, 2026
5 checks passed
@yihui-bot yihui-bot deleted the copilot/implement-g2-facet-feature branch April 2, 2026 07:00
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.

2 participants