Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 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,14 @@ 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)
# A leading 0 shifts terms to row (y) instead of column (x)
nms = if (facet_terms[1] == '0') 'y' else c('x', 'y')
facet_terms = facet_terms[facet_terms != '0']
enc = setNames(as.list(facet_terms), head(nms, length(facet_terms)))
if (length(enc)) facet = list(type = 'facetRect', encode = enc)
}

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
18 changes: 18 additions & 0 deletions vignettes/gglite.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,24 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |>
facet_rect(x = 'Species')
```

The formula interface supports faceting with `|`. Use `| var` for column facets,
`| 0 + var` for row facets, and `| var1 + var2` for both:

```{r}
# Column facet: panels arranged in columns by species
g2(penguins, bill_len ~ bill_dep | species)
```

```{r}
# Row facet: panels arranged in rows by island
g2(penguins, bill_len ~ bill_dep | 0 + island)
```

```{r}
# Both: columns by species, rows by island
g2(penguins, bill_len ~ bill_dep | species + island)
```

## Themes

Themes change the overall look. Built-in themes include `theme_classic()`
Expand Down
Loading