Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gglite
Title: Lightweight Data Visualization via the Grammar of Graphics
Version: 0.0.10
Version: 0.0.11
Authors@R: person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name",
comment = c(ORCID = "0000-0003-0645-5666"))
Description: A lightweight R interface to the AntV G2 JavaScript visualization
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# gglite 0.1

- 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.

A lightweight R interface to the
[AntV G2](https://g2.antv.antgroup.com/) JavaScript visualization library with
a ggplot2-style API. Create interactive charts using the Grammar of Graphics
Expand Down
15 changes: 10 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ extract_terms = function(expr) {
#' - `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)
Comment on lines 118 to 123
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.

#'
#' @param f A formula object.
Expand Down Expand Up @@ -147,13 +148,17 @@ parse_formula = function(f) {
aesthetics$position = rhs_terms
}

# Build facet
# Build facet: | var → column; | 0 + var → row; | var1 + var2 → both
facet = NULL
if (length(facet_terms)) {
enc = list()
if (length(facet_terms) >= 1) enc$x = facet_terms[1]
if (length(facet_terms) >= 2) enc$y = facet_terms[2]
facet = list(type = 'facetRect', encode = enc)
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.

}

list(aesthetics = aesthetics, facet = facet)
Expand Down
14 changes: 14 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ assert('parse_formula: y ~ x | z1 + z2', {
(res$facet$encode$y %==% 'b')
})

assert('parse_formula: y ~ x | 0 + z (row facet)', {
res = parse_formula(hp ~ mpg | 0 + cyl)
(res$aesthetics$x %==% 'mpg')
(res$aesthetics$y %==% 'hp')
(res$facet$type %==% 'facetRect')
(is.null(res$facet$encode$x))
(res$facet$encode$y %==% 'cyl')
})

assert('parse_formula: y ~ x | 0 (bare 0, no facet)', {
res = parse_formula(hp ~ mpg | 0)
(is.null(res$facet))
})

assert('ts_to_df converts univariate ts', {
res = ts_to_df(sunspot.year)
(is.data.frame(res$data))
Expand Down
Loading