Skip to content

Commit 4c18fcb

Browse files
Copilotyihui
andauthored
Expand formula interface to aesthetics, facets, labels, and encode (#36)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: yihui <163582+yihui@users.noreply.github.com>
1 parent 0053929 commit 4c18fcb

92 files changed

Lines changed: 440 additions & 365 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: gglite
22
Title: Lightweight Data Visualization via the Grammar of Graphics
3-
Version: 0.0.19
3+
Version: 0.0.20
44
Authors@R: person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name",
55
comment = c(ORCID = "0000-0003-0645-5666"))
66
Description: A lightweight R interface to the AntV G2 JavaScript visualization

R/animate.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
#' @export
1313
#' @examples
1414
#' # Fade-in animation on bars
15-
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), x = 'x', y = 'y') |>
15+
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), y ~ x) |>
1616
#' animate(enter = list(type = 'fadeIn', duration = 1000))
1717
#'
1818
#' # Wave-in animation
19-
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), x = 'x', y = 'y') |>
19+
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), y ~ x) |>
2020
#' animate(enter = list(type = 'waveIn', duration = 800))
2121
#'
2222
#' # Disable animation
23-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
23+
#' g2(mtcars, hp ~ mpg) |>
2424
#' animate(FALSE)
2525
animate = function(chart = NULL, ...) {
2626
mod = check_chart(animate, chart, list(...))

R/component.R

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
#' @export
1515
#' @examples
1616
#' # Chart-level axis titles (no marks yet)
17-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
17+
#' g2(mtcars, hp ~ mpg) |>
1818
#' axis_('x', title = 'Miles per Gallon') |>
1919
#' axis_('y', title = 'Horsepower')
2020
#'
2121
#' # Mark-level axis for dual-axis chart
2222
#' df = data.frame(x = 1:5, a = c(1, 4, 2, 5, 3), b = c(100, 200, 150, 300, 250))
23-
#' g2(df, x = 'x') |>
23+
#' g2(df, ~ x) |>
2424
#' mark_interval(encode = list(y = 'a')) |>
2525
#' mark_line(encode = list(y = 'b')) |>
2626
#' scale_y(independent = TRUE) |>
@@ -44,7 +44,7 @@ axis_ = function(chart = NULL, channel, ...) {
4444
#' @inheritParams axis_
4545
#' @export
4646
#' @examples
47-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
47+
#' g2(mtcars, hp ~ mpg) |>
4848
#' axis_x(title = 'Miles per Gallon')
4949
axis_x = function(chart = NULL, ...) axis_(chart, 'x', ...)
5050

@@ -53,7 +53,7 @@ axis_x = function(chart = NULL, ...) axis_(chart, 'x', ...)
5353
#' @inheritParams axis_
5454
#' @export
5555
#' @examples
56-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
56+
#' g2(mtcars, hp ~ mpg) |>
5757
#' axis_y(title = 'Horsepower')
5858
axis_y = function(chart = NULL, ...) axis_(chart, 'y', ...)
5959

@@ -69,7 +69,7 @@ axis_y = function(chart = NULL, ...) axis_(chart, 'y', ...)
6969
#' @return The modified `g2` object.
7070
#' @export
7171
#' @examples
72-
#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |>
72+
#' g2(iris, Sepal.Length ~ Sepal.Width, color = ~ Species) |>
7373
#' legend_('color', position = 'right')
7474
legend_ = function(chart = NULL, channel, ...) {
7575
mod = check_chart(legend_, chart, c(if (!missing(channel)) list(channel), list(...)))
@@ -96,7 +96,7 @@ legend_ = function(chart = NULL, channel, ...) {
9696
#' @inheritParams legend_
9797
#' @export
9898
#' @examples
99-
#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |>
99+
#' g2(iris, Sepal.Length ~ Sepal.Width, color = ~ Species) |>
100100
#' legend_color(position = 'right')
101101
legend_color = function(chart = NULL, ...) legend_(chart, 'color', ...)
102102

@@ -105,7 +105,7 @@ legend_color = function(chart = NULL, ...) legend_(chart, 'color', ...)
105105
#' @inheritParams legend_
106106
#' @export
107107
#' @examples
108-
#' g2(mtcars, x = 'mpg', y = 'hp', size = 'wt') |>
108+
#' g2(mtcars, hp ~ mpg, size = ~ wt) |>
109109
#' legend_size(position = 'bottom')
110110
legend_size = function(chart = NULL, ...) legend_(chart, 'size', ...)
111111

@@ -114,7 +114,7 @@ legend_size = function(chart = NULL, ...) legend_(chart, 'size', ...)
114114
#' @inheritParams legend_
115115
#' @export
116116
#' @examples
117-
#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', shape = 'Species') |>
117+
#' g2(iris, Sepal.Length ~ Sepal.Width, shape = ~ Species) |>
118118
#' legend_shape(position = 'bottom')
119119
legend_shape = function(chart = NULL, ...) legend_(chart, 'shape', ...)
120120

@@ -123,7 +123,7 @@ legend_shape = function(chart = NULL, ...) legend_(chart, 'shape', ...)
123123
#' @inheritParams legend_
124124
#' @export
125125
#' @examples
126-
#' g2(mtcars, x = 'mpg', y = 'hp', opacity = 'wt') |>
126+
#' g2(mtcars, hp ~ mpg, opacity = ~ wt) |>
127127
#' legend_opacity(position = 'bottom')
128128
legend_opacity = function(chart = NULL, ...) legend_(chart, 'opacity', ...)
129129

@@ -135,7 +135,7 @@ legend_opacity = function(chart = NULL, ...) legend_(chart, 'opacity', ...)
135135
#' @return The modified `g2` object.
136136
#' @export
137137
#' @examples
138-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
138+
#' g2(mtcars, hp ~ mpg) |>
139139
#' title_('Motor Trend Cars', subtitle = 'mpg vs hp')
140140
title_ = function(chart = NULL, text, ...) {
141141
mod = check_chart(title_, chart, c(if (!missing(text)) list(text), list(...)))
@@ -160,7 +160,7 @@ title_ = function(chart = NULL, text, ...) {
160160
#' @return The modified `g2` object.
161161
#' @export
162162
#' @examples
163-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
163+
#' g2(mtcars, hp ~ mpg) |>
164164
#' tooltip_(crosshairs = TRUE)
165165
tooltip_ = function(chart = NULL, ...) {
166166
mod = check_chart(tooltip_, chart, list(...))
@@ -180,21 +180,21 @@ tooltip_ = function(chart = NULL, ...) {
180180
#' multiple times to add several label layers.
181181
#'
182182
#' @param chart A `g2` object.
183-
#' @param ... Label options such as `text` (channel name), `position`,
184-
#' `formatter`, `style`.
183+
#' @param ... Label options such as `text` (channel name as `~col` or
184+
#' `'col'`), `position`, `formatter`, `style`.
185185
#' @return The modified `g2` object.
186186
#' @export
187187
#' @examples
188188
#' df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2))
189-
#' g2(df, x = 'x', y = 'y') |>
190-
#' labels_(text = 'y', position = 'inside')
189+
#' g2(df, y ~ x) |>
190+
#' labels_(text = ~ y, position = 'inside')
191191
labels_ = function(chart = NULL, ...) {
192192
mod = check_chart(labels_, chart, list(...))
193193
if (!is.null(mod)) return(mod)
194194
was_empty = !length(chart$layers)
195195
if (was_empty) chart = ensure_mark(chart)
196196
n = if (was_empty) 1L else length(chart$layers)
197-
chart$layers[[n]]$labels = c(chart$layers[[n]]$labels, list(list(...)))
197+
chart$layers[[n]]$labels = c(chart$layers[[n]]$labels, list(as_vars(list(...))))
198198
chart
199199
}
200200

@@ -206,7 +206,7 @@ labels_ = function(chart = NULL, ...) {
206206
#' @return The modified `g2` object.
207207
#' @export
208208
#' @examples
209-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
209+
#' g2(mtcars, hp ~ mpg) |>
210210
#' style_mark(fill = 'steelblue', stroke = 'white', lineWidth = 1)
211211
style_mark = function(chart = NULL, ...) {
212212
mod = check_chart(style_mark, chart, list(...))
@@ -228,7 +228,7 @@ style_mark = function(chart = NULL, ...) {
228228
#' @return The modified `g2` object.
229229
#' @export
230230
#' @examples
231-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
231+
#' g2(mtcars, hp ~ mpg) |>
232232
#' slider_('x')
233233
slider_ = function(chart = NULL, channel, ...) {
234234
mod = check_chart(slider_, chart, c(if (!missing(channel)) list(channel), list(...)))
@@ -244,7 +244,7 @@ slider_ = function(chart = NULL, channel, ...) {
244244
#' @inheritParams slider_
245245
#' @export
246246
#' @examples
247-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
247+
#' g2(mtcars, hp ~ mpg) |>
248248
#' slider_x()
249249
slider_x = function(chart = NULL, ...) slider_(chart, 'x', ...)
250250

@@ -253,7 +253,7 @@ slider_x = function(chart = NULL, ...) slider_(chart, 'x', ...)
253253
#' @inheritParams slider_
254254
#' @export
255255
#' @examples
256-
#' g2(mtcars, x = 'mpg', y = 'hp') |>
256+
#' g2(mtcars, hp ~ mpg) |>
257257
#' slider_y()
258258
slider_y = function(chart = NULL, ...) slider_(chart, 'y', ...)
259259

@@ -266,7 +266,7 @@ slider_y = function(chart = NULL, ...) slider_(chart, 'y', ...)
266266
#' @export
267267
#' @examples
268268
#' df = data.frame(x = 1:100, y = cumsum(rnorm(100)))
269-
#' g2(df, x = 'x', y = 'y') |>
269+
#' g2(df, y ~ x) |>
270270
#' mark_line() |>
271271
#' scrollbar_('x')
272272
scrollbar_ = function(chart = NULL, channel, ...) {
@@ -284,7 +284,7 @@ scrollbar_ = function(chart = NULL, channel, ...) {
284284
#' @export
285285
#' @examples
286286
#' df = data.frame(x = 1:100, y = cumsum(rnorm(100)))
287-
#' g2(df, x = 'x', y = 'y') |>
287+
#' g2(df, y ~ x) |>
288288
#' mark_line() |>
289289
#' scrollbar_x()
290290
scrollbar_x = function(chart = NULL, ...) scrollbar_(chart, 'x', ...)
@@ -295,7 +295,7 @@ scrollbar_x = function(chart = NULL, ...) scrollbar_(chart, 'x', ...)
295295
#' @export
296296
#' @examples
297297
#' df = data.frame(x = 1:100, y = cumsum(rnorm(100)))
298-
#' g2(df, x = 'x', y = 'y') |>
298+
#' g2(df, y ~ x) |>
299299
#' mark_line() |>
300300
#' scrollbar_y()
301301
scrollbar_y = function(chart = NULL, ...) scrollbar_(chart, 'y', ...)

R/coordinate.R

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
#' @examples
2020
#' # Polar coordinate (rose chart)
2121
#' df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2))
22-
#' g2(df, x = 'x', y = 'y') |>
22+
#' g2(df, y ~ x) |>
2323
#' coord_polar()
2424
#'
2525
#' # Theta coordinate (pie / donut chart)
26-
#' g2(df, x = 'x', y = 'y', color = 'x') |>
26+
#' g2(df, y ~ x, color = ~ x) |>
2727
#' transform_('stackY') |>
2828
#' coord_theta()
2929
#'
3030
#' # Radial coordinate (radial bar chart)
31-
#' g2(df, x = 'x', y = 'y', color = 'x') |>
31+
#' g2(df, y ~ x, color = ~ x) |>
3232
#' coord_radial()
3333
#'
3434
#' # Parallel coordinate (uses position encoding)
35-
#' g2(iris, position = names(iris)[-5], color = 'Species',
35+
#' g2(iris, position = names(iris)[-5], color = ~ Species,
3636
#' padding = c(30, NA, NA, NA)) |>
3737
#' coord_parallel() |>
3838
#' legend_color(position = 'bottom')
@@ -43,7 +43,7 @@
4343
#' score = c(80, 90, 65, 75, 85, 60, 70, 85, 80, 70),
4444
#' team = rep(c('A', 'B'), each = 5)
4545
#' )
46-
#' g2(df2, x = 'item', y = 'score', color = 'team') |>
46+
#' g2(df2, score ~ item, color = ~ team) |>
4747
#' mark_area(style = list(fillOpacity = 0.5)) |>
4848
#' mark_line(style = list(lineWidth = 2)) |>
4949
#' coord_polar() |>
@@ -68,7 +68,7 @@ coord_ = function(chart = NULL, type, ...) {
6868
#' @export
6969
#' @examples
7070
#' # Horizontal bar chart (coord_flip equivalent)
71-
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), x = 'x', y = 'y') |>
71+
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), y ~ x) |>
7272
#' coord_transpose()
7373
coord_transpose = function(chart = NULL) {
7474
mod = check_chart(coord_transpose, chart, list())
@@ -88,8 +88,7 @@ coord_transpose = function(chart = NULL) {
8888
#' @inheritParams coord_
8989
#' @export
9090
#' @examples
91-
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)),
92-
#' x = 'x', y = 'y', color = 'x') |>
91+
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), y ~ x, color = ~ x) |>
9392
#' coord_polar()
9493
coord_polar = function(chart = NULL, ...) coord_(chart, 'polar', ...)
9594

@@ -101,8 +100,7 @@ coord_polar = function(chart = NULL, ...) coord_(chart, 'polar', ...)
101100
#' @inheritParams coord_
102101
#' @export
103102
#' @examples
104-
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)),
105-
#' x = 'x', y = 'y', color = 'x') |>
103+
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), y ~ x, color = ~ x) |>
106104
#' transform_('stackY') |>
107105
#' coord_theta(innerRadius = 0.5)
108106
coord_theta = function(chart = NULL, ...) coord_(chart, 'theta', ...)
@@ -115,8 +113,7 @@ coord_theta = function(chart = NULL, ...) coord_(chart, 'theta', ...)
115113
#' @inheritParams coord_
116114
#' @export
117115
#' @examples
118-
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)),
119-
#' x = 'x', y = 'y', color = 'x') |>
116+
#' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), y ~ x, color = ~ x) |>
120117
#' coord_radial()
121118
coord_radial = function(chart = NULL, ...) coord_(chart, 'radial', ...)
122119

R/facet.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
#' `y` arguments to specify faceting variables.
66
#'
77
#' @param chart A `g2` object.
8-
#' @param ... Facet encoding and options. Pass `x = 'var'` and/or `y = 'var'`
9-
#' to specify the faceting variable(s).
8+
#' @param ... Facet encoding and options. Pass `x = ~var` and/or `y = ~var`
9+
#' to specify the faceting variable(s). Character strings are also accepted.
1010
#' @return The modified `g2` object.
1111
#' @export
1212
#' @examples
13-
#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length') |>
14-
#' facet_rect(x = 'Species')
13+
#' g2(iris, Sepal.Length ~ Sepal.Width) |>
14+
#' facet_rect(x = ~ Species)
1515
facet_rect = function(chart = NULL, ...) {
1616
mod = check_chart(facet_rect, chart, list(...))
1717
if (!is.null(mod)) return(mod)
1818
chart$facet = list(type = 'facetRect')
19-
enc = list(...)
19+
enc = as_vars(list(...))
2020
if (length(enc)) chart$facet$encode = enc
2121
chart
2222
}
2323

2424
#' Facet in a Circular Layout
2525
#'
2626
#' @param chart A `g2` object.
27-
#' @param ... Facet encoding and options. Pass `position = 'var'` to specify
28-
#' the faceting variable.
27+
#' @param ... Facet encoding and options. Pass `position = ~var` to specify
28+
#' the faceting variable. Character strings are also accepted.
2929
#' @return The modified `g2` object.
3030
#' @export
3131
#' @examples
32-
#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length') |>
33-
#' facet_circle(position = 'Species')
32+
#' g2(iris, Sepal.Length ~ Sepal.Width) |>
33+
#' facet_circle(position = ~ Species)
3434
facet_circle = function(chart = NULL, ...) {
3535
mod = check_chart(facet_circle, chart, list(...))
3636
if (!is.null(mod)) return(mod)
3737
chart$facet = list(type = 'facetCircle')
38-
enc = list(...)
38+
enc = as_vars(list(...))
3939
if (length(enc)) chart$facet$encode = enc
4040
chart
4141
}

0 commit comments

Comments
 (0)