diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 9f369d4..7be349e 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -121,17 +121,17 @@ you cannot solve it. - `gglite.R` - Core: package doc, CDN URLs, `g2()`, `encode()`, `annotate_df()` - `mark.R` - All 35 mark (geometry) functions -- `scale.R` - `scale_of()` -- `coordinate.R` - `coordinate()`, `coord_transpose()` +- `scale.R` - `scale_()` and helpers (`scale_x()`, `scale_y()`, etc.) +- `coordinate.R` - `coord_()`, `coord_transpose()` - `interact.R` - `interact()` -- `theme.R` - `theme_of()` -- `transform.R` - `transform_of()` +- `theme.R` - `theme_()` and theme shortcuts +- `transform.R` - `transform_()` - `facet.R` - `facet_rect()`, `facet_circle()` - `animate.R` - `animate()` -- `component.R` - `axis_of()`, `legend_of()`, `title_of()`, `tooltip_of()`, - `labels_of()`, `style_mark()`, `slider_of()`, `scrollbar_of()` -- `render.R` - `build_config()`, `chart_html()`, `preview()`, `print.g2()`, - `knit_print.g2()`, `record_print.g2()`, `render_shiny()` +- `component.R` - `axis_()`, `legend_()`, `title_()`, `tooltip_()`, + `labels_()`, `style_mark()`, `slider_()`, `scrollbar_()` and helpers +- `render.R` - `build_config()`, `chart_html()`, `print.g2()`, + `knit_print.g2()`, `record_print.g2()` **Tests** (`tests/`): - `test-all.R` - Entry point @@ -226,7 +226,7 @@ component functions: ```r g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('x', type = 'log') |> - theme_of('dark') |> - title_of('Motor Trend Cars') + scale_x(type = 'log') |> + theme_('dark') |> + title_('Motor Trend Cars') ``` diff --git a/.gitignore b/.gitignore index df1eeca..6751920 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ package.json package-lock.json *.Rcheck/ test-all.mjs -gglite_*.tar.gz +*.tar.gz .Rproj.user diff --git a/DESCRIPTION b/DESCRIPTION index 714809a..bf77f95 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: gglite Title: Lightweight Data Visualization via the Grammar of Graphics -Version: 0.0.4 +Version: 0.0.5 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 diff --git a/NAMESPACE b/NAMESPACE index 481fd7e..87f735f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,8 +3,11 @@ S3method(print,g2) S3method(record_print,g2) export(animate) -export(axis_of) +export(axis_) +export(axis_x) +export(axis_y) export(chart_html) +export(coord_) export(coord_helix) export(coord_parallel) export(coord_polar) @@ -12,15 +15,19 @@ export(coord_radar) export(coord_radial) export(coord_theta) export(coord_transpose) -export(coordinate) export(encode) export(facet_circle) export(facet_rect) export(g2) export(g2Output) export(interact) -export(labels_of) -export(legend_of) +export(labels_) +export(legend_) +export(legend_color) +export(legend_opacity) +export(legend_shape) +export(legend_size) +export(mark_) export(mark_area) export(mark_box) export(mark_boxplot) @@ -56,20 +63,29 @@ export(mark_tree) export(mark_treemap) export(mark_vector) export(mark_word_cloud) -export(padding_of) export(renderG2) -export(scale_of) -export(scrollbar_of) -export(slider_of) +export(scale_) +export(scale_color) +export(scale_opacity) +export(scale_shape) +export(scale_size) +export(scale_x) +export(scale_y) +export(scrollbar_) +export(scrollbar_x) +export(scrollbar_y) +export(slider_) +export(slider_x) +export(slider_y) export(style_mark) +export(theme_) export(theme_academy) export(theme_classic) export(theme_classicDark) export(theme_dark) export(theme_light) -export(theme_of) -export(title_of) -export(tooltip_of) -export(transform_of) +export(title_) +export(tooltip_) +export(transform_) importFrom(utils,modifyList) importFrom(xfun,record_print) diff --git a/R/component.R b/R/component.R index 1f5be52..62a79c6 100644 --- a/R/component.R +++ b/R/component.R @@ -1,25 +1,3 @@ -#' Set Chart Padding -#' -#' Set padding around the chart plotting area. This is useful when axis -#' labels or titles are cut off. -#' -#' @param chart A `g2` object. -#' @param top,right,bottom,left Padding in pixels for each side. -#' @return The modified `g2` object. -#' @export -#' @examples -#' g2(mtcars, x = 'mpg', y = 'hp') |> -#' mark_point() |> -#' padding_of(top = 30) -padding_of = function(chart, top = NULL, right = NULL, bottom = NULL, - left = NULL) { - if (!is.null(top)) chart$padding$paddingTop = top - if (!is.null(right)) chart$padding$paddingRight = right - if (!is.null(bottom)) chart$padding$paddingBottom = bottom - if (!is.null(left)) chart$padding$paddingLeft = left - chart -} - #' Configure an Axis #' #' Customise the axis for a positional channel (`'x'` or `'y'`). Set to @@ -34,9 +12,9 @@ padding_of = function(chart, top = NULL, right = NULL, bottom = NULL, #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> -#' axis_of('x', title = 'Miles per Gallon') |> -#' axis_of('y', title = 'Horsepower') -axis_of = function(chart, channel, ...) { +#' axis_('x', title = 'Miles per Gallon') |> +#' axis_('y', title = 'Horsepower') +axis_ = function(chart, channel, ...) { args = list(...) if (length(args) == 1 && is.logical(args[[1]])) { chart$axes[[channel]] = args[[1]] @@ -46,6 +24,26 @@ axis_of = function(chart, channel, ...) { chart } +#' Configure the X Axis +#' +#' @inheritParams axis_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp') |> +#' mark_point() |> +#' axis_x(title = 'Miles per Gallon') +axis_x = function(chart, ...) axis_(chart, 'x', ...) + +#' Configure the Y Axis +#' +#' @inheritParams axis_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp') |> +#' mark_point() |> +#' axis_y(title = 'Horsepower') +axis_y = function(chart, ...) axis_(chart, 'y', ...) + #' Configure a Legend #' #' Customise the legend for a visual channel (`'color'`, `'size'`, `'shape'`, @@ -60,8 +58,8 @@ axis_of = function(chart, channel, ...) { #' @examples #' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> #' mark_point() |> -#' legend_of('color', position = 'right') -legend_of = function(chart, channel, ...) { +#' legend_('color', position = 'right') +legend_ = function(chart, channel, ...) { args = list(...) if (length(args) == 1 && is.logical(args[[1]])) { chart$legends[[channel]] = args[[1]] @@ -71,6 +69,46 @@ legend_of = function(chart, channel, ...) { chart } +#' Configure the Color Legend +#' +#' @inheritParams legend_ +#' @export +#' @examples +#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> +#' mark_point() |> +#' legend_color(position = 'right') +legend_color = function(chart, ...) legend_(chart, 'color', ...) + +#' Configure the Size Legend +#' +#' @inheritParams legend_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp', size = 'wt') |> +#' mark_point() |> +#' legend_size(position = 'bottom') +legend_size = function(chart, ...) legend_(chart, 'size', ...) + +#' Configure the Shape Legend +#' +#' @inheritParams legend_ +#' @export +#' @examples +#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', shape = 'Species') |> +#' mark_point() |> +#' legend_shape(position = 'bottom') +legend_shape = function(chart, ...) legend_(chart, 'shape', ...) + +#' Configure the Opacity Legend +#' +#' @inheritParams legend_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp', opacity = 'wt') |> +#' mark_point() |> +#' legend_opacity(position = 'bottom') +legend_opacity = function(chart, ...) legend_(chart, 'opacity', ...) + #' Set the Chart Title #' #' @param chart A `g2` object. @@ -81,8 +119,8 @@ legend_of = function(chart, channel, ...) { #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> -#' title_of('Motor Trend Cars', subtitle = 'mpg vs hp') -title_of = function(chart, text, ...) { +#' title_('Motor Trend Cars', subtitle = 'mpg vs hp') +title_ = function(chart, text, ...) { dots = list(...) if (length(dots)) { chart$chart_title = c(list(title = text), dots) @@ -105,8 +143,8 @@ title_of = function(chart, text, ...) { #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> -#' tooltip_of(crosshairs = TRUE) -tooltip_of = function(chart, ...) { +#' tooltip_(crosshairs = TRUE) +tooltip_ = function(chart, ...) { args = list(...) if (length(args) == 1 && is.logical(args[[1]])) { chart$tooltip_config = args[[1]] @@ -130,8 +168,8 @@ tooltip_of = function(chart, ...) { #' df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) #' g2(df, x = 'x', y = 'y') |> #' mark_interval() |> -#' labels_of(text = 'y', position = 'inside') -labels_of = function(chart, ...) { +#' labels_(text = 'y', position = 'inside') +labels_ = function(chart, ...) { n = length(chart$layers) if (n == 0) stop('add a mark before setting labels') chart$layers[[n]]$labels = c(chart$layers[[n]]$labels, list(list(...))) @@ -168,14 +206,34 @@ style_mark = function(chart, ...) { #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> -#' slider_of('x') -slider_of = function(chart, channel, ...) { +#' slider_('x') +slider_ = function(chart, channel, ...) { if (is.null(chart$sliders)) chart$sliders = list() args = list(...) chart$sliders[[channel]] = if (length(args)) args else TRUE chart } +#' Add an X Slider +#' +#' @inheritParams slider_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp') |> +#' mark_point() |> +#' slider_x() +slider_x = function(chart, ...) slider_(chart, 'x', ...) + +#' Add a Y Slider +#' +#' @inheritParams slider_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp') |> +#' mark_point() |> +#' slider_y() +slider_y = function(chart, ...) slider_(chart, 'y', ...) + #' Add a Scrollbar #' #' @param chart A `g2` object. @@ -187,10 +245,32 @@ slider_of = function(chart, channel, ...) { #' df = data.frame(x = 1:100, y = cumsum(rnorm(100))) #' g2(df, x = 'x', y = 'y') |> #' mark_line() |> -#' scrollbar_of('x') -scrollbar_of = function(chart, channel, ...) { +#' scrollbar_('x') +scrollbar_ = function(chart, channel, ...) { if (is.null(chart$scrollbars)) chart$scrollbars = list() args = list(...) chart$scrollbars[[channel]] = if (length(args)) args else TRUE chart } + +#' Add an X Scrollbar +#' +#' @inheritParams scrollbar_ +#' @export +#' @examples +#' df = data.frame(x = 1:100, y = cumsum(rnorm(100))) +#' g2(df, x = 'x', y = 'y') |> +#' mark_line() |> +#' scrollbar_x() +scrollbar_x = function(chart, ...) scrollbar_(chart, 'x', ...) + +#' Add a Y Scrollbar +#' +#' @inheritParams scrollbar_ +#' @export +#' @examples +#' df = data.frame(x = 1:100, y = cumsum(rnorm(100))) +#' g2(df, x = 'x', y = 'y') |> +#' mark_line() |> +#' scrollbar_y() +scrollbar_y = function(chart, ...) scrollbar_(chart, 'y', ...) diff --git a/R/coordinate.R b/R/coordinate.R index 330c270..0a73cab 100644 --- a/R/coordinate.R +++ b/R/coordinate.R @@ -21,26 +21,26 @@ #' df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) #' g2(df, x = 'x', y = 'y') |> #' mark_interval() |> -#' coordinate('polar') +#' coord_polar() #' #' # Theta coordinate (pie / donut chart) #' g2(df, x = 'x', y = 'y', color = 'x') |> #' mark_interval() |> -#' transform_of('stackY') |> -#' coordinate('theta') +#' transform_('stackY') |> +#' coord_theta() #' #' # Radial coordinate (radial bar chart) #' g2(df, x = 'x', y = 'y', color = 'x') |> #' mark_interval() |> -#' coordinate('radial') +#' coord_radial() #' #' # Parallel coordinate (uses position encoding) #' g2(iris, position = c('Sepal.Length', 'Sepal.Width', -#' 'Petal.Length', 'Petal.Width'), color = 'Species') |> +#' 'Petal.Length', 'Petal.Width'), color = 'Species', +#' padding = c(30, NA, NA, NA)) |> #' mark_line() |> -#' coordinate('parallel') |> -#' legend_of('color', position = 'bottom') |> -#' padding_of(top = 30) +#' coord_parallel() |> +#' legend_color(position = 'bottom') #' #' # Radar coordinate (polar with long-format data) #' df2 = data.frame( @@ -51,11 +51,11 @@ #' g2(df2, x = 'item', y = 'score', color = 'team') |> #' mark_area(style = list(fillOpacity = 0.5)) |> #' mark_line(style = list(lineWidth = 2)) |> -#' coordinate('polar') |> -#' scale_of('x', padding = 0.5, align = 0) |> -#' scale_of('y', domainMin = 0, domainMax = 100) |> -#' axis_of('x', grid = TRUE) -coordinate = function(chart, type, ...) { +#' coord_polar() |> +#' scale_x(padding = 0.5, align = 0) |> +#' scale_y(domainMin = 0, domainMax = 100) |> +#' axis_x(grid = TRUE) +coord_ = function(chart, type, ...) { chart$coords = c(list(type = type), list(...)) chart } @@ -83,65 +83,65 @@ coord_transpose = function(chart) { #' Polar Coordinate System #' -#' Shortcut for `coordinate(chart, 'polar', ...)`. +#' Shortcut for `coord_(chart, 'polar', ...)`. #' -#' @inheritParams coordinate +#' @inheritParams coord_ #' @export #' @examples #' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), #' x = 'x', y = 'y', color = 'x') |> #' mark_interval() |> coord_polar() -coord_polar = function(chart, ...) coordinate(chart, 'polar', ...) +coord_polar = function(chart, ...) coord_(chart, 'polar', ...) #' Theta Coordinate System #' -#' Shortcut for `coordinate(chart, 'theta', ...)`. Used for pie and donut +#' Shortcut for `coord_(chart, 'theta', ...)`. Used for pie and donut #' charts. #' -#' @inheritParams coordinate +#' @inheritParams coord_ #' @export #' @examples #' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), #' x = 'x', y = 'y', color = 'x') |> -#' mark_interval() |> transform_of('stackY') |> +#' mark_interval() |> transform_('stackY') |> #' coord_theta(innerRadius = 0.5) -coord_theta = function(chart, ...) coordinate(chart, 'theta', ...) +coord_theta = function(chart, ...) coord_(chart, 'theta', ...) #' Radial Coordinate System #' -#' Shortcut for `coordinate(chart, 'radial', ...)`. Suitable for radial bar +#' Shortcut for `coord_(chart, 'radial', ...)`. Suitable for radial bar #' charts. #' -#' @inheritParams coordinate +#' @inheritParams coord_ #' @export #' @examples #' g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), #' x = 'x', y = 'y', color = 'x') |> #' mark_interval() |> coord_radial() -coord_radial = function(chart, ...) coordinate(chart, 'radial', ...) +coord_radial = function(chart, ...) coord_(chart, 'radial', ...) #' Radar Coordinate System #' -#' Shortcut for `coordinate(chart, 'radar', ...)`. Used with `position` +#' Shortcut for `coord_(chart, 'radar', ...)`. Used with `position` #' encoding for radar (spider) charts. #' -#' @inheritParams coordinate +#' @inheritParams coord_ #' @export -coord_radar = function(chart, ...) coordinate(chart, 'radar', ...) +coord_radar = function(chart, ...) coord_(chart, 'radar', ...) #' Helix Coordinate System #' -#' Shortcut for `coordinate(chart, 'helix', ...)`. +#' Shortcut for `coord_(chart, 'helix', ...)`. #' -#' @inheritParams coordinate +#' @inheritParams coord_ #' @export -coord_helix = function(chart, ...) coordinate(chart, 'helix', ...) +coord_helix = function(chart, ...) coord_(chart, 'helix', ...) #' Parallel Coordinate System #' -#' Shortcut for `coordinate(chart, 'parallel', ...)`. Used with `position` +#' Shortcut for `coord_(chart, 'parallel', ...)`. Used with `position` #' encoding for parallel coordinate plots. #' -#' @inheritParams coordinate +#' @inheritParams coord_ #' @export -coord_parallel = function(chart, ...) coordinate(chart, 'parallel', ...) +coord_parallel = function(chart, ...) coord_(chart, 'parallel', ...) diff --git a/R/gglite.R b/R/gglite.R index 13690e4..c235580 100644 --- a/R/gglite.R +++ b/R/gglite.R @@ -13,13 +13,39 @@ #' release. #' #' @return A character string. -#' @keywords internal +#' @noRd g2_cdn = function() { getOption('gglite.g2_cdn', 'https://unpkg.com/@antv/g2@5/dist/g2.min.js') } g2_col_cdn = 'https://cdn.jsdelivr.net/npm/@xiee/utils/js/g2-column.min.js' +#' Process a layout argument (padding, margin, or inset) +#' +#' Convert a scalar or length-4 vector into named G2 layout options. +#' A scalar sets the property directly (e.g., `padding = 20`). A length-4 +#' vector sets `Top`, `Right`, `Bottom`, `Left` variants; `NA` values are +#' omitted. +#' +#' @param name Base name: `'padding'`, `'margin'`, or `'inset'`. +#' @param value `NULL`, a scalar, or a length-4 numeric vector. +#' @return A named list of layout options. +#' @noRd +process_layout = function(name, value) { + if (is.null(value)) return(list()) + if (length(value) == 1) { + res = list(value) + names(res) = name + return(res) + } + if (length(value) != 4) stop( + "'", name, "' must be a scalar or a length-4 vector (top, right, bottom, left)" + ) + sides = c('Top', 'Right', 'Bottom', 'Left') + res = stats::setNames(as.list(value), paste0(name, sides)) + dropNulls(lapply(res, function(v) if (is.na(v)) NULL else v)) +} + #' Create a G2 Chart Object #' #' Construct a base chart object, optionally with data and aesthetic mappings. @@ -28,12 +54,19 @@ g2_col_cdn = 'https://cdn.jsdelivr.net/npm/@xiee/utils/js/g2-column.min.js' #' @param data A data frame (or `NULL`). #' @param ... Aesthetic mappings as `name = 'column'` pairs (character strings). #' @param width,height Width and height of the chart in pixels. +#' @param padding,margin,inset Layout spacing in pixels. Each can be a scalar +#' (applied to all sides) or a length-4 vector `c(top, right, bottom, left)`; +#' use `NA` to skip individual sides. `NULL` (the default) leaves the value +#' unset. #' @return A `g2` object (S3 class). #' @importFrom utils modifyList #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() -g2 = function(data = NULL, ..., width = 640, height = 480) { +g2 = function( + data = NULL, ..., width = 640, height = 480, + padding = NULL, margin = NULL, inset = NULL +) { chart = structure(list( data = data, options = list(width = width, height = height, autoFit = TRUE), @@ -47,7 +80,11 @@ g2 = function(data = NULL, ..., width = 640, height = 480) { legends = list(), chart_title = NULL, facet = NULL, - padding = list() + layout = c( + process_layout('padding', padding), + process_layout('margin', margin), + process_layout('inset', inset) + ) ), class = 'g2') dots = list(...) if (length(dots)) chart$aesthetics = modifyList(chart$aesthetics, dots) @@ -79,7 +116,7 @@ encode = function(chart, ...) { #' #' @param x A nested list. #' @return The annotated list. -#' @keywords internal +#' @noRd annotate_df = function(x) { if (is.data.frame(x) || !is.list(x)) return(x) nms = names(x) @@ -97,5 +134,5 @@ annotate_df = function(x) { } #' Remove NULL elements from a list -#' @keywords internal +#' @noRd dropNulls = function(x) x[!vapply(x, is.null, logical(1))] diff --git a/R/mark.R b/R/mark.R index b372389..9e79928 100644 --- a/R/mark.R +++ b/R/mark.R @@ -8,8 +8,10 @@ #' @param ... Mark-level options passed to G2, such as `data`, `encode`, #' `transform`, `style`, `animate`, `labels`, `tooltip`, `axis`, `legend`. #' @return The modified `g2` object. -#' @keywords internal -mark = function(chart, type, ...) { +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp') |> mark_('point') +mark_ = function(chart, type, ...) { layer = list(type = type) opts = list(...) if (length(opts)) layer = modifyList(layer, opts) @@ -21,7 +23,7 @@ mark = function(chart, type, ...) { #' Add an Interval Mark (Bar / Column Chart) #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' # Bar chart @@ -34,40 +36,40 @@ mark = function(chart, type, ...) { #' color = rep(c('a', 'b'), 2) #' ) #' g2(df, x = 'x', y = 'y', color = 'color') |> -#' mark_interval() |> transform_of('stackY') -mark_interval = function(chart, ...) mark(chart, 'interval', ...) +#' mark_interval() |> transform_('stackY') +mark_interval = function(chart, ...) mark_(chart, 'interval', ...) #' Add a Line Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(data.frame(x = 1:5, y = c(3, 1, 4, 1, 5)), x = 'x', y = 'y') |> #' mark_line() -mark_line = function(chart, ...) mark(chart, 'line', ...) +mark_line = function(chart, ...) mark_(chart, 'line', ...) #' Add a Point Mark (Scatter Plot) #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp', color = 'cyl') |> mark_point() -mark_point = function(chart, ...) mark(chart, 'point', ...) +mark_point = function(chart, ...) mark_(chart, 'point', ...) #' Add an Area Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(data.frame(x = 1:5, y = c(3, 1, 4, 1, 5)), x = 'x', y = 'y') |> #' mark_area() -mark_area = function(chart, ...) mark(chart, 'area', ...) +mark_area = function(chart, ...) mark_(chart, 'area', ...) #' Add a Rect Mark #' #' Draws rectangles. Commonly used with a `bin` transform for 2-D histograms. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' # 2-D histogram using bin transform @@ -75,36 +77,36 @@ mark_area = function(chart, ...) mark(chart, 'area', ...) #' mark_rect( #' transform = list(list(type = 'bin', thresholdsX = 10, thresholdsY = 10)) #' ) -mark_rect = function(chart, ...) mark(chart, 'rect', ...) +mark_rect = function(chart, ...) mark_(chart, 'rect', ...) #' Add a Cell Mark #' #' Draws rectangular cells, commonly used for heatmaps and calendar charts. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = expand.grid(x = LETTERS[1:4], y = LETTERS[1:4]) #' df$value = seq_len(nrow(df)) #' g2(df, x = 'x', y = 'y', color = 'value') |> mark_cell() -mark_cell = function(chart, ...) mark(chart, 'cell', ...) +mark_cell = function(chart, ...) mark_(chart, 'cell', ...) #' Add a Text Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) #' g2(df, x = 'x', y = 'y') |> #' mark_interval() |> #' mark_text(encode = list(text = 'y')) -mark_text = function(chart, ...) mark(chart, 'text', ...) +mark_text = function(chart, ...) mark_(chart, 'text', ...) #' Add a Path Mark #' #' Connects points in data order (unlike line, which sorts by x). #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' # A spiral path @@ -112,22 +114,22 @@ mark_text = function(chart, ...) mark(chart, 'text', ...) #' t = seq(0, 4 * pi, length.out = n) #' df = data.frame(x = t * cos(t), y = t * sin(t)) #' g2(df, x = 'x', y = 'y') |> mark_path() -mark_path = function(chart, ...) mark(chart, 'path', ...) +mark_path = function(chart, ...) mark_(chart, 'path', ...) #' Add a Polygon Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame(x = c(0, 1, 0.5), y = c(0, 0, 1)) #' g2(df, x = 'x', y = 'y') |> mark_polygon() -mark_polygon = function(chart, ...) mark(chart, 'polygon', ...) +mark_polygon = function(chart, ...) mark_(chart, 'polygon', ...) #' Add an Image Mark #' #' Places images at data coordinates. Requires an `src` encoding for image URLs. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame(x = 1:2, y = 1:2) @@ -135,47 +137,47 @@ mark_polygon = function(chart, ...) mark(chart, 'polygon', ...) #' mark_image(style = list( #' src = 'https://gw.alipayobjects.com/mdn/rms_dfc253/afts/img/A*SZGfRaFPkIoAAAAAAAAAAAAAARQnAQ' #' )) -mark_image = function(chart, ...) mark(chart, 'image', ...) +mark_image = function(chart, ...) mark_(chart, 'image', ...) #' Add a Link Mark #' #' Draws links (lines) between pairs of points. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame(x = c(0, 1), y = c(0, 0), x1 = c(1, 2), y1 = c(1, 1)) #' g2(df) |> #' mark_link(encode = list(x = c('x', 'x1'), y = c('y', 'y1'))) -mark_link = function(chart, ...) mark(chart, 'link', ...) +mark_link = function(chart, ...) mark_(chart, 'link', ...) # ---- Reference / annotation marks ---- #' Add a Vertical Reference Line (lineX) #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> #' mark_line_x(data = list(list(x = 20)), #' style = list(stroke = 'red', lineDash = c(4, 4))) -mark_line_x = function(chart, ...) mark(chart, 'lineX', ...) +mark_line_x = function(chart, ...) mark_(chart, 'lineX', ...) #' Add a Horizontal Reference Line (lineY) #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> #' mark_line_y(data = list(list(y = 150)), #' style = list(stroke = 'red', lineDash = c(4, 4))) -mark_line_y = function(chart, ...) mark(chart, 'lineY', ...) +mark_line_y = function(chart, ...) mark_(chart, 'lineY', ...) #' Add a Range Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> @@ -184,35 +186,35 @@ mark_line_y = function(chart, ...) mark(chart, 'lineY', ...) #' data = list(list(x = c(15, 25), y = c(100, 200))), #' style = list(fill = 'steelblue', fillOpacity = 0.15) #' ) -mark_range = function(chart, ...) mark(chart, 'range', ...) +mark_range = function(chart, ...) mark_(chart, 'range', ...) #' Add a Horizontal Range (rangeX) #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> #' mark_range_x(data = list(list(x = c(15, 25))), #' style = list(fill = 'steelblue', fillOpacity = 0.15)) -mark_range_x = function(chart, ...) mark(chart, 'rangeX', ...) +mark_range_x = function(chart, ...) mark_(chart, 'rangeX', ...) #' Add a Vertical Range (rangeY) #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> #' mark_range_y(data = list(list(y = c(100, 200))), #' style = list(fill = 'orange', fillOpacity = 0.15)) -mark_range_y = function(chart, ...) mark(chart, 'rangeY', ...) +mark_range_y = function(chart, ...) mark_(chart, 'rangeY', ...) #' Add a Connector Mark #' #' Draws a connector line with optional labels between two data points. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame(x = c('A', 'B'), y = c(3, 7)) @@ -223,7 +225,7 @@ mark_range_y = function(chart, ...) mark(chart, 'rangeY', ...) #' encode = list(x = 'x', x1 = 'x1'), #' labels = list(list(text = '+133%')) #' ) -mark_connector = function(chart, ...) mark(chart, 'connector', ...) +mark_connector = function(chart, ...) mark_(chart, 'connector', ...) # ---- Statistical / composite marks ---- @@ -231,20 +233,20 @@ mark_connector = function(chart, ...) mark(chart, 'connector', ...) #' #' Draws pre-computed box elements (for custom box plots). #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_box = function(chart, ...) mark(chart, 'box', ...) +mark_box = function(chart, ...) mark_(chart, 'box', ...) #' Add a Box Plot Mark #' #' A composite mark that automatically computes box plot statistics (median, #' quartiles, whiskers) from raw data. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(iris, x = 'Species', y = 'Sepal.Width') |> mark_boxplot() -mark_boxplot = function(chart, ...) mark(chart, 'boxplot', ...) +mark_boxplot = function(chart, ...) mark_(chart, 'boxplot', ...) #' Add a Density Mark #' @@ -252,28 +254,28 @@ mark_boxplot = function(chart, ...) mark(chart, 'boxplot', ...) #' G2 v5 does not support the `kde` transform; use [mark_heatmap()] as an #' alternative for 2-D density visualizations. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_density = function(chart, ...) mark(chart, 'density', ...) +mark_density = function(chart, ...) mark_(chart, 'density', ...) #' Add a Heatmap Mark #' #' A composite mark for rendering heatmaps from point data. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Petal.Length') |> #' mark_heatmap() -mark_heatmap = function(chart, ...) mark(chart, 'heatmap', ...) +mark_heatmap = function(chart, ...) mark_(chart, 'heatmap', ...) #' Add a Vector Mark #' #' Draws arrows or vectors. Useful for wind or flow field visualizations. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_vector = function(chart, ...) mark(chart, 'vector', ...) +mark_vector = function(chart, ...) mark_(chart, 'vector', ...) # ---- Graph marks ---- @@ -281,17 +283,17 @@ mark_vector = function(chart, ...) mark(chart, 'vector', ...) #' #' Used in graph visualizations together with [mark_edge()]. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_node = function(chart, ...) mark(chart, 'node', ...) +mark_node = function(chart, ...) mark_(chart, 'node', ...) #' Add an Edge Mark #' #' Used in graph visualizations together with [mark_node()]. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_edge = function(chart, ...) mark(chart, 'edge', ...) +mark_edge = function(chart, ...) mark_(chart, 'edge', ...) # ---- Layout marks (complex / composite) ---- @@ -300,7 +302,7 @@ mark_edge = function(chart, ...) mark(chart, 'edge', ...) #' Draws a Sankey diagram. Data should have `source`, `target`, and `value` #' columns. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame( @@ -312,14 +314,14 @@ mark_edge = function(chart, ...) mark(chart, 'edge', ...) #' encode = list(source = 'source', target = 'target', value = 'value'), #' layout = list(nodeAlign = 'center') #' ) -mark_sankey = function(chart, ...) mark(chart, 'sankey', ...) +mark_sankey = function(chart, ...) mark_(chart, 'sankey', ...) #' Add a Chord Mark #' #' Draws a chord diagram. Data should have `source`, `target`, and `value` #' columns. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame( @@ -330,14 +332,14 @@ mark_sankey = function(chart, ...) mark(chart, 'sankey', ...) #' mark_chord( #' encode = list(source = 'source', target = 'target', value = 'value') #' ) -mark_chord = function(chart, ...) mark(chart, 'chord', ...) +mark_chord = function(chart, ...) mark_(chart, 'chord', ...) #' Add a Treemap Mark #' #' Draws a treemap layout. Data should be hierarchical (a nested list with #' `name`, `value`, and optionally `children` fields). #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' tree_data = list( @@ -352,11 +354,11 @@ mark_chord = function(chart, ...) mark(chart, 'chord', ...) #' data = list(value = tree_data), #' encode = list(value = 'value') #' ) -mark_treemap = function(chart, ...) mark(chart, 'treemap', ...) +mark_treemap = function(chart, ...) mark_(chart, 'treemap', ...) #' Add a Pack (Circle Packing) Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' tree_data = list( @@ -371,27 +373,27 @@ mark_treemap = function(chart, ...) mark(chart, 'treemap', ...) #' data = list(value = tree_data), #' encode = list(value = 'value', color = 'name') #' ) -mark_pack = function(chart, ...) mark(chart, 'pack', ...) +mark_pack = function(chart, ...) mark_(chart, 'pack', ...) #' Add a Force Graph Mark #' #' Draws a force-directed graph layout. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_force_graph = function(chart, ...) mark(chart, 'forceGraph', ...) +mark_force_graph = function(chart, ...) mark_(chart, 'forceGraph', ...) #' Add a Tree Mark #' #' Draws a tree layout. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_tree = function(chart, ...) mark(chart, 'tree', ...) +mark_tree = function(chart, ...) mark_(chart, 'tree', ...) #' Add a Word Cloud Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' df = data.frame( @@ -400,40 +402,40 @@ mark_tree = function(chart, ...) mark(chart, 'tree', ...) #' ) #' g2(df) |> #' mark_word_cloud(encode = list(text = 'text', value = 'value', color = 'text')) -mark_word_cloud = function(chart, ...) mark(chart, 'wordCloud', ...) +mark_word_cloud = function(chart, ...) mark_(chart, 'wordCloud', ...) #' Add a Gauge Mark #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_gauge = function(chart, ...) mark(chart, 'gauge', ...) +mark_gauge = function(chart, ...) mark_(chart, 'gauge', ...) #' Add a Liquid Mark #' #' Draws a liquid fill gauge. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export #' @examples #' g2() |> #' mark_liquid(data = list(list(value = 0.3)), #' encode = list(y = 'value'), #' style = list(textContent = '30%')) -mark_liquid = function(chart, ...) mark(chart, 'liquid', ...) +mark_liquid = function(chart, ...) mark_(chart, 'liquid', ...) #' Add a Shape Mark #' #' A custom mark whose rendering is controlled by a JavaScript render function. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_shape = function(chart, ...) mark(chart, 'shape', ...) +mark_shape = function(chart, ...) mark_(chart, 'shape', ...) #' Add a Partition (Sunburst) Mark #' #' Note: the `partition` mark may not work correctly in G2 v5. Consider using #' [mark_treemap()] as an alternative for hierarchical data visualization. #' -#' @inheritParams mark +#' @inheritParams mark_ #' @export -mark_partition = function(chart, ...) mark(chart, 'partition', ...) +mark_partition = function(chart, ...) mark_(chart, 'partition', ...) diff --git a/R/render.R b/R/render.R index fab202a..b049768 100644 --- a/R/render.R +++ b/R/render.R @@ -9,7 +9,7 @@ #' #' @param chart A `g2` object. #' @return A list suitable for JSON serialization. -#' @keywords internal +#' @noRd build_config = function(chart) { config = list() @@ -40,7 +40,7 @@ build_config = function(chart) { if (!is.null(chart$tooltip_config)) config$tooltip = chart$tooltip_config if (!is.null(chart$sliders)) config$slider = chart$sliders if (!is.null(chart$scrollbars)) config$scrollbar = chart$scrollbars - if (length(chart$padding)) config = modifyList(config, chart$padding) + if (length(chart$layout)) config = modifyList(config, chart$layout) # Faceting wraps the spec as a facet view if (!is.null(chart$facet)) { diff --git a/R/scale.R b/R/scale.R index 3478a1f..f4b37ce 100644 --- a/R/scale.R +++ b/R/scale.R @@ -16,17 +16,75 @@ #' @examples #' # Log-scaled x axis #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> -#' scale_of('x', type = 'log') +#' scale_('x', type = 'log') #' #' # Square-root scale on y #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> -#' scale_of('y', type = 'sqrt') +#' scale_('y', type = 'sqrt') #' #' # Ordinal colour palette #' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> #' mark_point() |> -#' scale_of('color', palette = 'category10') -scale_of = function(chart, field, ...) { +#' scale_('color', palette = 'category10') +scale_ = function(chart, field, ...) { chart$scales[[field]] = list(...) chart } + +#' Configure the X Scale +#' +#' @inheritParams scale_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> +#' scale_x(type = 'log') +scale_x = function(chart, ...) scale_(chart, 'x', ...) + +#' Configure the Y Scale +#' +#' @inheritParams scale_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> +#' scale_y(type = 'sqrt') +scale_y = function(chart, ...) scale_(chart, 'y', ...) + +#' Configure the Color Scale +#' +#' @inheritParams scale_ +#' @export +#' @examples +#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> +#' mark_point() |> +#' scale_color(palette = 'category10') +scale_color = function(chart, ...) scale_(chart, 'color', ...) + +#' Configure the Size Scale +#' +#' @inheritParams scale_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp', size = 'wt') |> +#' mark_point() |> +#' scale_size(range = c(2, 10)) +scale_size = function(chart, ...) scale_(chart, 'size', ...) + +#' Configure the Shape Scale +#' +#' @inheritParams scale_ +#' @export +#' @examples +#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', shape = 'Species') |> +#' mark_point() |> +#' scale_shape(range = c('circle', 'square', 'triangle')) +scale_shape = function(chart, ...) scale_(chart, 'shape', ...) + +#' Configure the Opacity Scale +#' +#' @inheritParams scale_ +#' @export +#' @examples +#' g2(mtcars, x = 'mpg', y = 'hp', opacity = 'wt') |> +#' mark_point() |> +#' scale_opacity(range = c(0.2, 1)) +scale_opacity = function(chart, ...) scale_(chart, 'opacity', ...) diff --git a/R/theme.R b/R/theme.R index 7ff8778..8712c6d 100644 --- a/R/theme.R +++ b/R/theme.R @@ -12,13 +12,13 @@ #' # Dark theme #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> -#' theme_of('dark') +#' theme_('dark') #' #' # Academy theme #' g2(mtcars, x = 'mpg', y = 'hp') |> #' mark_point() |> -#' theme_of('academy') -theme_of = function(chart, type, ...) { +#' theme_('academy') +theme_ = function(chart, type, ...) { if (is.character(type)) { chart$theme = c(list(type = type), list(...)) } else { @@ -29,50 +29,50 @@ theme_of = function(chart, type, ...) { #' Classic Theme #' -#' Shortcut for `theme_of(chart, 'classic', ...)`. This is the default theme. +#' Shortcut for `theme_(chart, 'classic', ...)`. This is the default theme. #' -#' @inheritParams theme_of +#' @inheritParams theme_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_classic() -theme_classic = function(chart, ...) theme_of(chart, 'classic', ...) +theme_classic = function(chart, ...) theme_(chart, 'classic', ...) #' Classic Dark Theme #' -#' Shortcut for `theme_of(chart, 'classicDark', ...)`. +#' Shortcut for `theme_(chart, 'classicDark', ...)`. #' -#' @inheritParams theme_of +#' @inheritParams theme_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_classicDark() -theme_classicDark = function(chart, ...) theme_of(chart, 'classicDark', ...) +theme_classicDark = function(chart, ...) theme_(chart, 'classicDark', ...) #' Light Theme #' -#' Shortcut for `theme_of(chart, 'light', ...)`. +#' Shortcut for `theme_(chart, 'light', ...)`. #' -#' @inheritParams theme_of +#' @inheritParams theme_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_light() -theme_light = function(chart, ...) theme_of(chart, 'light', ...) +theme_light = function(chart, ...) theme_(chart, 'light', ...) #' Dark Theme #' -#' Shortcut for `theme_of(chart, 'dark', ...)`. +#' Shortcut for `theme_(chart, 'dark', ...)`. #' -#' @inheritParams theme_of +#' @inheritParams theme_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_dark() -theme_dark = function(chart, ...) theme_of(chart, 'dark', ...) +theme_dark = function(chart, ...) theme_(chart, 'dark', ...) #' Academy Theme #' -#' Shortcut for `theme_of(chart, 'academy', ...)`. +#' Shortcut for `theme_(chart, 'academy', ...)`. #' -#' @inheritParams theme_of +#' @inheritParams theme_ #' @export #' @examples #' g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_academy() -theme_academy = function(chart, ...) theme_of(chart, 'academy', ...) +theme_academy = function(chart, ...) theme_(chart, 'academy', ...) diff --git a/R/transform.R b/R/transform.R index beb0df2..ff6c609 100644 --- a/R/transform.R +++ b/R/transform.R @@ -27,29 +27,29 @@ #' ) #' g2(df, x = 'x', y = 'y', color = 'color') |> #' mark_interval() |> -#' transform_of('stackY') +#' transform_('stackY') #' #' # Grouped (dodged) bar chart #' g2(df, x = 'x', y = 'y', color = 'color') |> #' mark_interval() |> -#' transform_of('dodgeX') +#' transform_('dodgeX') #' #' # Percent stacked bar (normalizeY + stackY) #' g2(df, x = 'x', y = 'y', color = 'color') |> #' mark_interval() |> -#' transform_of('stackY') |> -#' transform_of('normalizeY') +#' transform_('stackY') |> +#' transform_('normalizeY') #' #' # Jitter on a scatter plot #' g2(mtcars, x = 'cyl', y = 'hp') |> #' mark_point() |> -#' transform_of('jitterX') +#' transform_('jitterX') #' #' # Histogram using binX #' g2(mtcars, x = 'mpg') |> #' mark_interval(encode = list(y = 'count')) |> -#' transform_of('binX', thresholds = 15) -transform_of = function(chart, type, ...) { +#' transform_('binX', thresholds = 15) +transform_ = function(chart, type, ...) { n = length(chart$layers) if (n == 0) stop('add a mark before setting transforms') t = c(list(type = type), list(...)) diff --git a/examples/axes-legends.Rmd b/examples/axes-legends.Rmd index 6c57141..78426d1 100644 --- a/examples/axes-legends.Rmd +++ b/examples/axes-legends.Rmd @@ -13,8 +13,8 @@ library(gglite) ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - axis_of('x', title = 'Miles per Gallon') |> - axis_of('y', title = 'Horsepower') + axis_x(title = 'Miles per Gallon') |> + axis_y(title = 'Horsepower') ``` ### Hide an axis @@ -22,7 +22,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - axis_of('y', FALSE) + axis_y(FALSE) ``` ### Hide both axes @@ -30,8 +30,8 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - axis_of('x', FALSE) |> - axis_of('y', FALSE) + axis_x(FALSE) |> + axis_y(FALSE) ``` ### Axis with grid @@ -39,8 +39,8 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - axis_of('x', grid = TRUE) |> - axis_of('y', grid = TRUE) + axis_x(grid = TRUE) |> + axis_y(grid = TRUE) ``` ### Axis tick count @@ -48,8 +48,8 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - axis_of('x', tickCount = 5) |> - axis_of('y', tickCount = 8) + axis_x(tickCount = 5) |> + axis_y(tickCount = 8) ``` ## Legend Customization @@ -59,7 +59,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - legend_of('color', position = 'right') + legend_color(position = 'right') ``` ### Legend at the top @@ -67,7 +67,7 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - legend_of('color', position = 'top') + legend_color(position = 'top') ``` ### Legend at the bottom @@ -75,7 +75,7 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - legend_of('color', position = 'bottom') + legend_color(position = 'bottom') ``` ### Legend at the left @@ -83,7 +83,7 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - legend_of('color', position = 'left') + legend_color(position = 'left') ``` ### Hide a legend @@ -91,7 +91,7 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - legend_of('color', FALSE) + legend_color(FALSE) ``` ### Legend with title @@ -99,5 +99,5 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - legend_of('color', position = 'right', title = 'Iris Species') + legend_color(position = 'right', title = 'Iris Species') ``` diff --git a/examples/coordinates.Rmd b/examples/coordinates.Rmd index fd69828..89d379e 100644 --- a/examples/coordinates.Rmd +++ b/examples/coordinates.Rmd @@ -7,7 +7,9 @@ library(gglite) ``` Coordinate systems control how the positional channels (x and y) are -interpreted. Use `coordinate()` to set the type and options. +interpreted. Use helpers like `coord_polar()`, `coord_theta()`, +`coord_radial()`, `coord_helix()`, and `coord_parallel()` to set the +coordinate system. ## Cartesian (default) @@ -18,7 +20,7 @@ explicitly, but you can do so for clarity. df = data.frame(x = c('A', 'B', 'C', 'D'), y = c(3, 7, 2, 5)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - coordinate('cartesian') + coord_('cartesian') ``` ## Polar @@ -29,7 +31,7 @@ displays. ```{r} g2(df, x = 'x', y = 'y') |> mark_interval() |> - coordinate('polar') + coord_polar() ``` ### Polar with innerRadius (donut rose) @@ -37,7 +39,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - coordinate('polar', innerRadius = 0.4) + coord_polar(innerRadius = 0.4) ``` ### Polar with custom start/end angles @@ -45,7 +47,7 @@ g2(df, x = 'x', y = 'y', color = 'x') |> ```{r} g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - coordinate('polar', startAngle = -pi / 2, endAngle = pi / 2) + coord_polar(startAngle = -pi / 2, endAngle = pi / 2) ``` ## Theta @@ -55,8 +57,8 @@ Maps data values to angular extent. Used for pie and donut charts. ```{r} g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - transform_of('stackY') |> - coordinate('theta') + transform_('stackY') |> + coord_theta() ``` ### Donut chart (theta with innerRadius) @@ -64,8 +66,8 @@ g2(df, x = 'x', y = 'y', color = 'x') |> ```{r} g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - transform_of('stackY') |> - coordinate('theta', innerRadius = 0.5) + transform_('stackY') |> + coord_theta(innerRadius = 0.5) ``` ## Radial @@ -75,7 +77,7 @@ Maps y to the radial direction. Suitable for radial bar charts. ```{r} g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - coordinate('radial') + coord_radial() ``` ### Radial with innerRadius @@ -83,7 +85,7 @@ g2(df, x = 'x', y = 'y', color = 'x') |> ```{r} g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - coordinate('radial', innerRadius = 0.3) + coord_radial(innerRadius = 0.3) ``` ## Parallel @@ -93,11 +95,11 @@ encoding (a character vector of column names) instead of `x`/`y`. ```{r} g2(iris, position = c('Sepal.Length', 'Sepal.Width', - 'Petal.Length', 'Petal.Width'), color = 'Species') |> + 'Petal.Length', 'Petal.Width'), color = 'Species', + padding = c(30, NA, NA, NA)) |> mark_line() |> - coordinate('parallel') |> - legend_of('color', position = 'bottom') |> - padding_of(top = 30) + coord_parallel() |> + legend_color(position = 'bottom') ``` ## Radar @@ -117,10 +119,10 @@ g2(df_radar, x = 'item', y = 'score', color = 'team') |> mark_area(style = list(fillOpacity = 0.5)) |> mark_line(style = list(lineWidth = 2)) |> mark_point() |> - coordinate('polar') |> - scale_of('x', padding = 0.5, align = 0) |> - scale_of('y', domainMin = 0, domainMax = 100) |> - axis_of('y', grid = TRUE, title = FALSE) + coord_polar() |> + scale_x(padding = 0.5, align = 0) |> + scale_y(domainMin = 0, domainMax = 100) |> + axis_y(grid = TRUE, title = FALSE) ``` ## Helix @@ -131,7 +133,7 @@ Arranges data along a helix spiral. Works best with `mark_interval()`. df_helix = data.frame(x = paste0('D', 1:50), y = abs(sin(1:50 / 5))) g2(df_helix, x = 'x', y = 'y', color = 'y') |> mark_interval() |> - coordinate('helix') + coord_helix() ``` ## Transpose (coord_flip) @@ -151,6 +153,6 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(iris, position = names(iris)[-5], color = 'Species') |> mark_line() |> - coordinate('parallel') |> + coord_parallel() |> coord_transpose() ``` diff --git a/examples/interactions.Rmd b/examples/interactions.Rmd index b6cd201..cd1b426 100644 --- a/examples/interactions.Rmd +++ b/examples/interactions.Rmd @@ -38,7 +38,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('dodgeX') |> + transform_('dodgeX') |> interact('elementHighlightByX') ``` @@ -47,7 +47,7 @@ g2(df, x = 'x', y = 'y', color = 'color') |> ```{r} g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('dodgeX') |> + transform_('dodgeX') |> interact('elementHighlightByColor') ``` diff --git a/examples/labels-styles.Rmd b/examples/labels-styles.Rmd index a771c95..447a522 100644 --- a/examples/labels-styles.Rmd +++ b/examples/labels-styles.Rmd @@ -8,7 +8,7 @@ library(gglite) ## Labels -Labels add text annotations to marks. Use `labels_of()` to configure them. +Labels add text annotations to marks. Use `labels_()` to configure them. ### Basic labels on bars @@ -16,7 +16,7 @@ Labels add text annotations to marks. Use `labels_of()` to configure them. df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - labels_of(text = 'y') + labels_(text = 'y') ``` ### Label position: inside @@ -24,7 +24,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_interval() |> - labels_of(text = 'y', position = 'inside') + labels_(text = 'y', position = 'inside') ``` ### Label position: top @@ -32,7 +32,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_interval() |> - labels_of(text = 'y', position = 'top') + labels_(text = 'y', position = 'top') ``` ### Labels on a scatter plot @@ -41,7 +41,7 @@ g2(df, x = 'x', y = 'y') |> df = data.frame(x = 1:5, y = c(3, 7, 2, 9, 5), label = c('a', 'b', 'c', 'd', 'e')) g2(df, x = 'x', y = 'y') |> mark_point() |> - labels_of(text = 'label') + labels_(text = 'label') ``` ### Multiple label layers @@ -50,8 +50,8 @@ g2(df, x = 'x', y = 'y') |> df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - labels_of(text = 'x', position = 'inside') |> - labels_of(text = 'y', position = 'top') + labels_(text = 'x', position = 'inside') |> + labels_(text = 'y', position = 'top') ``` ## Mark Styles diff --git a/examples/marks.Rmd b/examples/marks.Rmd index 526ee2d..46047f0 100644 --- a/examples/marks.Rmd +++ b/examples/marks.Rmd @@ -76,14 +76,14 @@ df = data.frame( color = rep(c('a', 'b'), 3) ) g2(df, x = 'x', y = 'y', color = 'color') |> - mark_interval() |> transform_of('stackY') + mark_interval() |> transform_('stackY') ``` ### Grouped (dodged) bar ```{r} g2(df, x = 'x', y = 'y', color = 'color') |> - mark_interval() |> transform_of('dodgeX') + mark_interval() |> transform_('dodgeX') ``` ## Area Mark @@ -101,7 +101,7 @@ df = data.frame( group = rep(c('A', 'B'), each = 5) ) g2(df, x = 'x', y = 'y', color = 'group') |> - mark_area() |> transform_of('stackY') + mark_area() |> transform_('stackY') ``` ## Rect Mark diff --git a/examples/scales.Rmd b/examples/scales.Rmd index b8ff06d..761c24a 100644 --- a/examples/scales.Rmd +++ b/examples/scales.Rmd @@ -6,15 +6,16 @@ title: Scales library(gglite) ``` -Scales control how data values map to visual properties. Use `scale_of()` to -configure the scale for a given channel. +Scales control how data values map to visual properties. Use helpers like +`scale_x()`, `scale_y()`, `scale_color()`, and `scale_size()` to configure +the scale for a given channel. ## Linear Scale (default for numeric) ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('x', type = 'linear') + scale_x(type = 'linear') ``` ## Log Scale @@ -22,7 +23,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('y', type = 'log') + scale_y(type = 'log') ``` ## Power Scale @@ -30,7 +31,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('y', type = 'pow') + scale_y(type = 'pow') ``` ## Square Root Scale @@ -38,7 +39,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('y', type = 'sqrt') + scale_y(type = 'sqrt') ``` ## Ordinal Scale @@ -47,7 +48,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> df = data.frame(x = c('A', 'B', 'C', 'D'), y = c(3, 7, 2, 5)) g2(df, x = 'x', y = 'y') |> mark_point() |> - scale_of('x', type = 'ordinal') + scale_x(type = 'ordinal') ``` ## Band Scale @@ -55,7 +56,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_interval() |> - scale_of('x', type = 'band') + scale_x(type = 'band') ``` ## Time Scale @@ -67,7 +68,7 @@ df = data.frame( ) g2(df, x = 'date', y = 'value') |> mark_line() |> - scale_of('x', type = 'time') + scale_x(type = 'time') ``` ## Custom Domain and Range @@ -75,8 +76,8 @@ g2(df, x = 'date', y = 'value') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('x', domain = c(10, 35)) |> - scale_of('y', domain = c(0, 400)) + scale_x(domain = c(10, 35)) |> + scale_y(domain = c(0, 400)) ``` ## Nice Rounding @@ -84,8 +85,8 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('x', nice = TRUE) |> - scale_of('y', nice = TRUE) + scale_x(nice = TRUE) |> + scale_y(nice = TRUE) ``` ## Zero Inclusion @@ -93,7 +94,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('y', zero = TRUE) + scale_y(zero = TRUE) ``` ## Color Palette @@ -101,7 +102,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - scale_of('color', palette = 'category10') + scale_color(palette = 'category10') ``` ### Custom color range @@ -109,7 +110,7 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> ```{r} g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - scale_of('color', range = c('#e41a1c', '#377eb8', '#4daf4a')) + scale_color(range = c('#e41a1c', '#377eb8', '#4daf4a')) ``` ## Identity Scale @@ -118,7 +119,7 @@ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2), col = c('red', 'green', 'blue')) g2(df, x = 'x', y = 'y', color = 'col') |> mark_interval() |> - scale_of('color', type = 'identity') + scale_color(type = 'identity') ``` ## Multiple Scales @@ -126,8 +127,8 @@ g2(df, x = 'x', y = 'y', color = 'col') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp', color = 'wt', size = 'qsec') |> mark_point() |> - scale_of('x', nice = TRUE) |> - scale_of('y', type = 'sqrt') |> - scale_of('color', palette = 'viridis') |> - scale_of('size', range = c(2, 10)) + scale_x(nice = TRUE) |> + scale_y(type = 'sqrt') |> + scale_color(palette = 'viridis') |> + scale_size(range = c(2, 10)) ``` diff --git a/examples/shiny.Rmd b/examples/shiny.Rmd index aac83dc..ba0ec68 100644 --- a/examples/shiny.Rmd +++ b/examples/shiny.Rmd @@ -49,7 +49,7 @@ server = function(input, output, session) { output$chart1 = renderG2({ g2(mtcars, x = input$xvar, y = 'mpg') |> mark_point() |> - title_of(paste('mpg vs', input$xvar)) + title_(paste('mpg vs', input$xvar)) }) } diff --git a/examples/sliders-scrollbars.Rmd b/examples/sliders-scrollbars.Rmd index 0783159..eb06407 100644 --- a/examples/sliders-scrollbars.Rmd +++ b/examples/sliders-scrollbars.Rmd @@ -14,7 +14,7 @@ Sliders and scrollbars allow users to zoom and pan through data. df = data.frame(x = 1:100, y = cumsum(rnorm(100))) g2(df, x = 'x', y = 'y') |> mark_line() |> - slider_of('x') + slider_x() ``` ## Slider on y @@ -22,7 +22,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_line() |> - slider_of('y') + slider_y() ``` ## Slider on both axes @@ -30,8 +30,8 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_point() |> - slider_of('x') |> - slider_of('y') + slider_x() |> + slider_y() ``` ## Scrollbar on x @@ -39,7 +39,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_line() |> - scrollbar_of('x') + scrollbar_x() ``` ## Scrollbar on y @@ -47,7 +47,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_line() |> - scrollbar_of('y') + scrollbar_y() ``` ## Scrollbar on both axes @@ -55,8 +55,8 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(df, x = 'x', y = 'y') |> mark_point() |> - scrollbar_of('x') |> - scrollbar_of('y') + scrollbar_x() |> + scrollbar_y() ``` ## Slider with a bar chart @@ -65,5 +65,5 @@ g2(df, x = 'x', y = 'y') |> df = data.frame(x = paste0('Item ', 1:50), y = sample(1:100, 50)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - slider_of('x') + slider_x() ``` diff --git a/examples/themes.Rmd b/examples/themes.Rmd index 46f6ce8..bc5e8e4 100644 --- a/examples/themes.Rmd +++ b/examples/themes.Rmd @@ -6,15 +6,16 @@ title: Themes library(gglite) ``` -Themes control the overall visual appearance of the chart. Use `theme_of()` to -set a built-in theme or provide custom options. +Themes control the overall visual appearance of the chart. Use helpers like +`theme_classic()`, `theme_dark()`, `theme_light()`, `theme_classicDark()`, +and `theme_academy()` to set a built-in theme. ## Classic Theme (default) ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('classic') + theme_classic() ``` ## Classic Dark Theme @@ -22,7 +23,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('classicDark') + theme_classicDark() ``` ## Light Theme @@ -30,7 +31,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('light') + theme_light() ``` ## Dark Theme @@ -38,7 +39,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('dark') + theme_dark() ``` ## Academy Theme @@ -46,7 +47,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('academy') + theme_academy() ``` ## Theme on a bar chart @@ -55,7 +56,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> df = data.frame(x = c('A', 'B', 'C', 'D'), y = c(3, 7, 2, 5)) g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - theme_of('dark') + theme_dark() ``` ## Theme on a multi-series line chart @@ -67,5 +68,5 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'group') |> mark_line() |> - theme_of('classicDark') + theme_classicDark() ``` diff --git a/examples/titles-tooltips.Rmd b/examples/titles-tooltips.Rmd index 0c48aa0..83a4ce7 100644 --- a/examples/titles-tooltips.Rmd +++ b/examples/titles-tooltips.Rmd @@ -13,7 +13,7 @@ library(gglite) ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - title_of('Motor Trend Cars') + title_('Motor Trend Cars') ``` ### Title with subtitle @@ -21,7 +21,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - title_of('Motor Trend Cars', subtitle = 'mpg vs horsepower') + title_('Motor Trend Cars', subtitle = 'mpg vs horsepower') ``` ### Title alignment @@ -29,7 +29,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - title_of('Right-aligned Title', align = 'right') + title_('Right-aligned Title', align = 'right') ``` ### Title on a bar chart @@ -38,7 +38,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> df = data.frame(x = c('A', 'B', 'C', 'D'), y = c(3, 7, 2, 5)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - title_of('Simple Bar Chart', subtitle = 'Categories A-D') + title_('Simple Bar Chart', subtitle = 'Categories A-D') ``` ## Tooltip Configuration @@ -48,7 +48,7 @@ g2(df, x = 'x', y = 'y') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - tooltip_of(crosshairs = TRUE) + tooltip_(crosshairs = TRUE) ``` ### Shared tooltip (useful for multi-series) @@ -60,7 +60,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'group') |> mark_line() |> - tooltip_of(shared = TRUE) + tooltip_(shared = TRUE) ``` ### Disable tooltip @@ -68,5 +68,5 @@ g2(df, x = 'x', y = 'y', color = 'group') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - tooltip_of(FALSE) + tooltip_(FALSE) ``` diff --git a/examples/transforms.Rmd b/examples/transforms.Rmd index b03e664..54597b3 100644 --- a/examples/transforms.Rmd +++ b/examples/transforms.Rmd @@ -7,7 +7,7 @@ library(gglite) ``` Transforms are data manipulations applied to marks, similar to ggplot2's -`stat_*()` and `position_*()` functions. Use `transform_of()` to add them. +`stat_*()` and `position_*()` functions. Use `transform_()` to add them. ## stackY (position_stack) @@ -20,7 +20,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') + transform_('stackY') ``` ### Stacked area chart @@ -32,7 +32,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'group') |> mark_area() |> - transform_of('stackY') + transform_('stackY') ``` ## dodgeX (position_dodge) @@ -46,7 +46,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('dodgeX') + transform_('dodgeX') ``` ## normalizeY (position_fill) @@ -56,8 +56,8 @@ Normalize to 100% (percent-stacked chart). ```{r} g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') |> - transform_of('normalizeY') + transform_('stackY') |> + transform_('normalizeY') ``` ## jitterX / jitterY @@ -67,7 +67,7 @@ Add random jitter to reduce overplotting. ```{r} g2(mtcars, x = 'cyl', y = 'hp') |> mark_point() |> - transform_of('jitterX') + transform_('jitterX') ``` ### Jitter in both directions @@ -75,8 +75,8 @@ g2(mtcars, x = 'cyl', y = 'hp') |> ```{r} g2(mtcars, x = 'cyl', y = 'gear') |> mark_point() |> - transform_of('jitterX') |> - transform_of('jitterY') + transform_('jitterX') |> + transform_('jitterY') ``` ## binX (stat_bin) @@ -86,7 +86,7 @@ Bin data along x for histograms. ```{r} g2(mtcars, x = 'mpg') |> mark_interval(encode = list(y = 'count')) |> - transform_of('binX', thresholds = 15) + transform_('binX', thresholds = 15) ``` ## bin (2D binning) @@ -111,8 +111,8 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') |> - transform_of('symmetryY') + transform_('stackY') |> + transform_('symmetryY') ``` ## sortX / sortY @@ -123,7 +123,7 @@ Sort data before rendering. df = data.frame(x = c('C', 'A', 'B'), y = c(2, 5, 3)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - transform_of('sortX', reverse = TRUE) + transform_('sortX', reverse = TRUE) ``` ## Multiple transforms @@ -137,7 +137,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') |> - transform_of('normalizeY') |> + transform_('stackY') |> + transform_('normalizeY') |> coord_transpose() ``` diff --git a/man/annotate_df.Rd b/man/annotate_df.Rd deleted file mode 100644 index 4e83335..0000000 --- a/man/annotate_df.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/gglite.R -\name{annotate_df} -\alias{annotate_df} -\title{Annotate Data Frames for Column-Major JSON} -\usage{ -annotate_df(x) -} -\arguments{ -\item{x}{A nested list.} -} -\value{ -The annotated list. -} -\description{ -Recursively walks a nested list and wraps any data frame found in a \code{data} -field with \code{list(type = 'column', value = df)} so that the G2 column-major -helper script can convert it client-side. -} -\keyword{internal} diff --git a/man/axis_of.Rd b/man/axis_.Rd similarity index 80% rename from man/axis_of.Rd rename to man/axis_.Rd index 9356815..53ddcd0 100644 --- a/man/axis_of.Rd +++ b/man/axis_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/component.R -\name{axis_of} -\alias{axis_of} +\name{axis_} +\alias{axis_} \title{Configure an Axis} \usage{ -axis_of(chart, channel, ...) +axis_(chart, channel, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -24,6 +24,6 @@ Customise the axis for a positional channel (\code{'x'} or \code{'y'}). Set to \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - axis_of('x', title = 'Miles per Gallon') |> - axis_of('y', title = 'Horsepower') + axis_('x', title = 'Miles per Gallon') |> + axis_('y', title = 'Horsepower') } diff --git a/man/axis_x.Rd b/man/axis_x.Rd new file mode 100644 index 0000000..73c7d37 --- /dev/null +++ b/man/axis_x.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{axis_x} +\alias{axis_x} +\title{Configure the X Axis} +\usage{ +axis_x(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Axis options such as \code{title}, \code{labelFormatter}, \code{tickCount}, +\code{grid}, etc., or \code{FALSE} to hide.} +} +\description{ +Configure the X Axis +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp') |> + mark_point() |> + axis_x(title = 'Miles per Gallon') +} diff --git a/man/axis_y.Rd b/man/axis_y.Rd new file mode 100644 index 0000000..516d4dc --- /dev/null +++ b/man/axis_y.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{axis_y} +\alias{axis_y} +\title{Configure the Y Axis} +\usage{ +axis_y(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Axis options such as \code{title}, \code{labelFormatter}, \code{tickCount}, +\code{grid}, etc., or \code{FALSE} to hide.} +} +\description{ +Configure the Y Axis +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp') |> + mark_point() |> + axis_y(title = 'Horsepower') +} diff --git a/man/build_config.Rd b/man/build_config.Rd deleted file mode 100644 index 9ebdca5..0000000 --- a/man/build_config.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/render.R -\name{build_config} -\alias{build_config} -\title{Build G2 Spec} -\usage{ -build_config(chart) -} -\arguments{ -\item{chart}{A \code{g2} object.} -} -\value{ -A list suitable for JSON serialization. -} -\description{ -Convert a \code{g2} chart object into a nested list matching G2's -\code{chart.options()} spec format. Data frames are annotated for column-major -JSON serialisation via \code{\link[=annotate_df]{annotate_df()}}. Constructor options (width, height, -container) are handled separately by \code{\link[=chart_html]{chart_html()}}. -} -\keyword{internal} diff --git a/man/coordinate.Rd b/man/coord_.Rd similarity index 80% rename from man/coordinate.Rd rename to man/coord_.Rd index a10ca23..f68195c 100644 --- a/man/coordinate.Rd +++ b/man/coord_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/coordinate.R -\name{coordinate} -\alias{coordinate} +\name{coord_} +\alias{coord_} \title{Set the Coordinate System} \usage{ -coordinate(chart, type, ...) +coord_(chart, type, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -34,26 +34,26 @@ charts, use the \code{'polar'} coordinate with long-format data (x/y/color). df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - coordinate('polar') + coord_polar() # Theta coordinate (pie / donut chart) g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - transform_of('stackY') |> - coordinate('theta') + transform_('stackY') |> + coord_theta() # Radial coordinate (radial bar chart) g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - coordinate('radial') + coord_radial() # Parallel coordinate (uses position encoding) g2(iris, position = c('Sepal.Length', 'Sepal.Width', - 'Petal.Length', 'Petal.Width'), color = 'Species') |> + 'Petal.Length', 'Petal.Width'), color = 'Species', + padding = c(30, NA, NA, NA)) |> mark_line() |> - coordinate('parallel') |> - legend_of('color', position = 'bottom') |> - padding_of(top = 30) + coord_parallel() |> + legend_color(position = 'bottom') # Radar coordinate (polar with long-format data) df2 = data.frame( @@ -64,8 +64,8 @@ df2 = data.frame( g2(df2, x = 'item', y = 'score', color = 'team') |> mark_area(style = list(fillOpacity = 0.5)) |> mark_line(style = list(lineWidth = 2)) |> - coordinate('polar') |> - scale_of('x', padding = 0.5, align = 0) |> - scale_of('y', domainMin = 0, domainMax = 100) |> - axis_of('x', grid = TRUE) + coord_polar() |> + scale_x(padding = 0.5, align = 0) |> + scale_y(domainMin = 0, domainMax = 100) |> + axis_x(grid = TRUE) } diff --git a/man/coord_helix.Rd b/man/coord_helix.Rd index 1129abc..80e2776 100644 --- a/man/coord_helix.Rd +++ b/man/coord_helix.Rd @@ -13,5 +13,5 @@ coord_helix(chart, ...) \code{startAngle}, \code{endAngle}, or \code{transform}.} } \description{ -Shortcut for \code{coordinate(chart, 'helix', ...)}. +Shortcut for \code{coord_(chart, 'helix', ...)}. } diff --git a/man/coord_parallel.Rd b/man/coord_parallel.Rd index 2f77c5d..dc1e4f9 100644 --- a/man/coord_parallel.Rd +++ b/man/coord_parallel.Rd @@ -13,6 +13,6 @@ coord_parallel(chart, ...) \code{startAngle}, \code{endAngle}, or \code{transform}.} } \description{ -Shortcut for \code{coordinate(chart, 'parallel', ...)}. Used with \code{position} +Shortcut for \code{coord_(chart, 'parallel', ...)}. Used with \code{position} encoding for parallel coordinate plots. } diff --git a/man/coord_polar.Rd b/man/coord_polar.Rd index 794521c..5226f11 100644 --- a/man/coord_polar.Rd +++ b/man/coord_polar.Rd @@ -13,7 +13,7 @@ coord_polar(chart, ...) \code{startAngle}, \code{endAngle}, or \code{transform}.} } \description{ -Shortcut for \code{coordinate(chart, 'polar', ...)}. +Shortcut for \code{coord_(chart, 'polar', ...)}. } \examples{ g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), diff --git a/man/coord_radar.Rd b/man/coord_radar.Rd index 0bfc395..d26cee4 100644 --- a/man/coord_radar.Rd +++ b/man/coord_radar.Rd @@ -13,6 +13,6 @@ coord_radar(chart, ...) \code{startAngle}, \code{endAngle}, or \code{transform}.} } \description{ -Shortcut for \code{coordinate(chart, 'radar', ...)}. Used with \code{position} +Shortcut for \code{coord_(chart, 'radar', ...)}. Used with \code{position} encoding for radar (spider) charts. } diff --git a/man/coord_radial.Rd b/man/coord_radial.Rd index 5e20f12..2f4b510 100644 --- a/man/coord_radial.Rd +++ b/man/coord_radial.Rd @@ -13,7 +13,7 @@ coord_radial(chart, ...) \code{startAngle}, \code{endAngle}, or \code{transform}.} } \description{ -Shortcut for \code{coordinate(chart, 'radial', ...)}. Suitable for radial bar +Shortcut for \code{coord_(chart, 'radial', ...)}. Suitable for radial bar charts. } \examples{ diff --git a/man/coord_theta.Rd b/man/coord_theta.Rd index 116d805..b194c36 100644 --- a/man/coord_theta.Rd +++ b/man/coord_theta.Rd @@ -13,12 +13,12 @@ coord_theta(chart, ...) \code{startAngle}, \code{endAngle}, or \code{transform}.} } \description{ -Shortcut for \code{coordinate(chart, 'theta', ...)}. Used for pie and donut +Shortcut for \code{coord_(chart, 'theta', ...)}. Used for pie and donut charts. } \examples{ g2(data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)), x = 'x', y = 'y', color = 'x') |> - mark_interval() |> transform_of('stackY') |> + mark_interval() |> transform_('stackY') |> coord_theta(innerRadius = 0.5) } diff --git a/man/dropNulls.Rd b/man/dropNulls.Rd deleted file mode 100644 index 4f2f266..0000000 --- a/man/dropNulls.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/gglite.R -\name{dropNulls} -\alias{dropNulls} -\title{Remove NULL elements from a list} -\usage{ -dropNulls(x) -} -\description{ -Remove NULL elements from a list -} -\keyword{internal} diff --git a/man/g2.Rd b/man/g2.Rd index ce0efd5..4b585f1 100644 --- a/man/g2.Rd +++ b/man/g2.Rd @@ -4,7 +4,15 @@ \alias{g2} \title{Create a G2 Chart Object} \usage{ -g2(data = NULL, ..., width = 640, height = 480) +g2( + data = NULL, + ..., + width = 640, + height = 480, + padding = NULL, + margin = NULL, + inset = NULL +) } \arguments{ \item{data}{A data frame (or \code{NULL}).} @@ -12,6 +20,11 @@ g2(data = NULL, ..., width = 640, height = 480) \item{...}{Aesthetic mappings as \code{name = 'column'} pairs (character strings).} \item{width, height}{Width and height of the chart in pixels.} + +\item{padding, margin, inset}{Layout spacing in pixels. Each can be a scalar +(applied to all sides) or a length-4 vector \code{c(top, right, bottom, left)}; +use \code{NA} to skip individual sides. \code{NULL} (the default) leaves the value +unset.} } \value{ A \code{g2} object (S3 class). diff --git a/man/g2_cdn.Rd b/man/g2_cdn.Rd deleted file mode 100644 index 77aa4a8..0000000 --- a/man/g2_cdn.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/gglite.R -\name{g2_cdn} -\alias{g2_cdn} -\title{CDN URL for the G2 Library} -\usage{ -g2_cdn() -} -\value{ -A character string. -} -\description{ -Returns the URL for loading the G2 JavaScript library. Customizable via the -\code{gglite.g2_cdn} option. The default \verb{@5} resolves to the latest v5.x -release. -} -\keyword{internal} diff --git a/man/labels_of.Rd b/man/labels_.Rd similarity index 85% rename from man/labels_of.Rd rename to man/labels_.Rd index 10a8771..bcb61a9 100644 --- a/man/labels_of.Rd +++ b/man/labels_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/component.R -\name{labels_of} -\alias{labels_of} +\name{labels_} +\alias{labels_} \title{Add Labels to the Last Mark} \usage{ -labels_of(chart, ...) +labels_(chart, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -23,5 +23,5 @@ multiple times to add several label layers. df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - labels_of(text = 'y', position = 'inside') + labels_(text = 'y', position = 'inside') } diff --git a/man/legend_of.Rd b/man/legend_.Rd similarity index 86% rename from man/legend_of.Rd rename to man/legend_.Rd index c2df0f3..f649d76 100644 --- a/man/legend_of.Rd +++ b/man/legend_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/component.R -\name{legend_of} -\alias{legend_of} +\name{legend_} +\alias{legend_} \title{Configure a Legend} \usage{ -legend_of(chart, channel, ...) +legend_(chart, channel, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -24,5 +24,5 @@ Customise the legend for a visual channel (\code{'color'}, \code{'size'}, \code{ \examples{ g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - legend_of('color', position = 'right') + legend_('color', position = 'right') } diff --git a/man/legend_color.Rd b/man/legend_color.Rd new file mode 100644 index 0000000..9691c3c --- /dev/null +++ b/man/legend_color.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{legend_color} +\alias{legend_color} +\title{Configure the Color Legend} +\usage{ +legend_color(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Legend options such as \code{position} (\code{'top'}, \code{'bottom'}, \code{'left'}, +\code{'right'}), \code{layout}, \code{title}, etc.} +} +\description{ +Configure the Color Legend +} +\examples{ +g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> + mark_point() |> + legend_color(position = 'right') +} diff --git a/man/legend_opacity.Rd b/man/legend_opacity.Rd new file mode 100644 index 0000000..faecda8 --- /dev/null +++ b/man/legend_opacity.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{legend_opacity} +\alias{legend_opacity} +\title{Configure the Opacity Legend} +\usage{ +legend_opacity(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Legend options such as \code{position} (\code{'top'}, \code{'bottom'}, \code{'left'}, +\code{'right'}), \code{layout}, \code{title}, etc.} +} +\description{ +Configure the Opacity Legend +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp', opacity = 'wt') |> + mark_point() |> + legend_opacity(position = 'bottom') +} diff --git a/man/legend_shape.Rd b/man/legend_shape.Rd new file mode 100644 index 0000000..7819b4c --- /dev/null +++ b/man/legend_shape.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{legend_shape} +\alias{legend_shape} +\title{Configure the Shape Legend} +\usage{ +legend_shape(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Legend options such as \code{position} (\code{'top'}, \code{'bottom'}, \code{'left'}, +\code{'right'}), \code{layout}, \code{title}, etc.} +} +\description{ +Configure the Shape Legend +} +\examples{ +g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', shape = 'Species') |> + mark_point() |> + legend_shape(position = 'bottom') +} diff --git a/man/legend_size.Rd b/man/legend_size.Rd new file mode 100644 index 0000000..ebe33ce --- /dev/null +++ b/man/legend_size.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{legend_size} +\alias{legend_size} +\title{Configure the Size Legend} +\usage{ +legend_size(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Legend options such as \code{position} (\code{'top'}, \code{'bottom'}, \code{'left'}, +\code{'right'}), \code{layout}, \code{title}, etc.} +} +\description{ +Configure the Size Legend +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp', size = 'wt') |> + mark_point() |> + legend_size(position = 'bottom') +} diff --git a/man/mark.Rd b/man/mark_.Rd similarity index 84% rename from man/mark.Rd rename to man/mark_.Rd index ebdea56..f3c9fcc 100644 --- a/man/mark.Rd +++ b/man/mark_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/mark.R -\name{mark} -\alias{mark} +\name{mark_} +\alias{mark_} \title{Add a Geometry Layer (Mark)} \usage{ -mark(chart, type, ...) +mark_(chart, type, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -21,4 +21,6 @@ The modified \code{g2} object. Generic function to add a mark (geometry layer) to the chart. Use the specific \verb{mark_*()} wrappers for convenience. } -\keyword{internal} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp') |> mark_('point') +} diff --git a/man/mark_interval.Rd b/man/mark_interval.Rd index 1bf04b2..1429282 100644 --- a/man/mark_interval.Rd +++ b/man/mark_interval.Rd @@ -26,5 +26,5 @@ df = data.frame( color = rep(c('a', 'b'), 2) ) g2(df, x = 'x', y = 'y', color = 'color') |> - mark_interval() |> transform_of('stackY') + mark_interval() |> transform_('stackY') } diff --git a/man/padding_of.Rd b/man/padding_of.Rd deleted file mode 100644 index 31cb2d4..0000000 --- a/man/padding_of.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/component.R -\name{padding_of} -\alias{padding_of} -\title{Set Chart Padding} -\usage{ -padding_of(chart, top = NULL, right = NULL, bottom = NULL, left = NULL) -} -\arguments{ -\item{chart}{A \code{g2} object.} - -\item{top, right, bottom, left}{Padding in pixels for each side.} -} -\value{ -The modified \code{g2} object. -} -\description{ -Set padding around the chart plotting area. This is useful when axis -labels or titles are cut off. -} -\examples{ -g2(mtcars, x = 'mpg', y = 'hp') |> - mark_point() |> - padding_of(top = 30) -} diff --git a/man/scale_of.Rd b/man/scale_.Rd similarity index 86% rename from man/scale_of.Rd rename to man/scale_.Rd index 2dcd995..25df6a7 100644 --- a/man/scale_of.Rd +++ b/man/scale_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/scale.R -\name{scale_of} -\alias{scale_of} +\name{scale_} +\alias{scale_} \title{Configure a Scale} \usage{ -scale_of(chart, field, ...) +scale_(chart, field, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -29,14 +29,14 @@ G2 scale types: \code{'linear'}, \code{'ordinal'}, \code{'band'}, \code{'point'} \examples{ # Log-scaled x axis g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('x', type = 'log') + scale_('x', type = 'log') # Square-root scale on y g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('y', type = 'sqrt') + scale_('y', type = 'sqrt') # Ordinal colour palette g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - scale_of('color', palette = 'category10') + scale_('color', palette = 'category10') } diff --git a/man/scale_color.Rd b/man/scale_color.Rd new file mode 100644 index 0000000..b8a461b --- /dev/null +++ b/man/scale_color.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scale.R +\name{scale_color} +\alias{scale_color} +\title{Configure the Color Scale} +\usage{ +scale_color(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scale options passed to G2 (e.g., \code{type = 'log'}, \code{nice = TRUE}, +\code{domain}, \code{range}, \code{zero = TRUE}).} +} +\description{ +Configure the Color Scale +} +\examples{ +g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> + mark_point() |> + scale_color(palette = 'category10') +} diff --git a/man/scale_opacity.Rd b/man/scale_opacity.Rd new file mode 100644 index 0000000..84ba08e --- /dev/null +++ b/man/scale_opacity.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scale.R +\name{scale_opacity} +\alias{scale_opacity} +\title{Configure the Opacity Scale} +\usage{ +scale_opacity(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scale options passed to G2 (e.g., \code{type = 'log'}, \code{nice = TRUE}, +\code{domain}, \code{range}, \code{zero = TRUE}).} +} +\description{ +Configure the Opacity Scale +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp', opacity = 'wt') |> + mark_point() |> + scale_opacity(range = c(0.2, 1)) +} diff --git a/man/scale_shape.Rd b/man/scale_shape.Rd new file mode 100644 index 0000000..c257dfb --- /dev/null +++ b/man/scale_shape.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scale.R +\name{scale_shape} +\alias{scale_shape} +\title{Configure the Shape Scale} +\usage{ +scale_shape(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scale options passed to G2 (e.g., \code{type = 'log'}, \code{nice = TRUE}, +\code{domain}, \code{range}, \code{zero = TRUE}).} +} +\description{ +Configure the Shape Scale +} +\examples{ +g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', shape = 'Species') |> + mark_point() |> + scale_shape(range = c('circle', 'square', 'triangle')) +} diff --git a/man/scale_size.Rd b/man/scale_size.Rd new file mode 100644 index 0000000..129976d --- /dev/null +++ b/man/scale_size.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scale.R +\name{scale_size} +\alias{scale_size} +\title{Configure the Size Scale} +\usage{ +scale_size(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scale options passed to G2 (e.g., \code{type = 'log'}, \code{nice = TRUE}, +\code{domain}, \code{range}, \code{zero = TRUE}).} +} +\description{ +Configure the Size Scale +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp', size = 'wt') |> + mark_point() |> + scale_size(range = c(2, 10)) +} diff --git a/man/scale_x.Rd b/man/scale_x.Rd new file mode 100644 index 0000000..b2503c2 --- /dev/null +++ b/man/scale_x.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scale.R +\name{scale_x} +\alias{scale_x} +\title{Configure the X Scale} +\usage{ +scale_x(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scale options passed to G2 (e.g., \code{type = 'log'}, \code{nice = TRUE}, +\code{domain}, \code{range}, \code{zero = TRUE}).} +} +\description{ +Configure the X Scale +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> + scale_x(type = 'log') +} diff --git a/man/scale_y.Rd b/man/scale_y.Rd new file mode 100644 index 0000000..01b5b7b --- /dev/null +++ b/man/scale_y.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scale.R +\name{scale_y} +\alias{scale_y} +\title{Configure the Y Scale} +\usage{ +scale_y(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scale options passed to G2 (e.g., \code{type = 'log'}, \code{nice = TRUE}, +\code{domain}, \code{range}, \code{zero = TRUE}).} +} +\description{ +Configure the Y Scale +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> + scale_y(type = 'sqrt') +} diff --git a/man/scrollbar_of.Rd b/man/scrollbar_.Rd similarity index 82% rename from man/scrollbar_of.Rd rename to man/scrollbar_.Rd index a95b3bc..1e04c21 100644 --- a/man/scrollbar_of.Rd +++ b/man/scrollbar_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/component.R -\name{scrollbar_of} -\alias{scrollbar_of} +\name{scrollbar_} +\alias{scrollbar_} \title{Add a Scrollbar} \usage{ -scrollbar_of(chart, channel, ...) +scrollbar_(chart, channel, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -23,5 +23,5 @@ Add a Scrollbar df = data.frame(x = 1:100, y = cumsum(rnorm(100))) g2(df, x = 'x', y = 'y') |> mark_line() |> - scrollbar_of('x') + scrollbar_('x') } diff --git a/man/scrollbar_x.Rd b/man/scrollbar_x.Rd new file mode 100644 index 0000000..0d69a59 --- /dev/null +++ b/man/scrollbar_x.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{scrollbar_x} +\alias{scrollbar_x} +\title{Add an X Scrollbar} +\usage{ +scrollbar_x(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scrollbar options.} +} +\description{ +Add an X Scrollbar +} +\examples{ +df = data.frame(x = 1:100, y = cumsum(rnorm(100))) +g2(df, x = 'x', y = 'y') |> + mark_line() |> + scrollbar_x() +} diff --git a/man/scrollbar_y.Rd b/man/scrollbar_y.Rd new file mode 100644 index 0000000..1cbb333 --- /dev/null +++ b/man/scrollbar_y.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{scrollbar_y} +\alias{scrollbar_y} +\title{Add a Y Scrollbar} +\usage{ +scrollbar_y(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Scrollbar options.} +} +\description{ +Add a Y Scrollbar +} +\examples{ +df = data.frame(x = 1:100, y = cumsum(rnorm(100))) +g2(df, x = 'x', y = 'y') |> + mark_line() |> + scrollbar_y() +} diff --git a/man/slider_of.Rd b/man/slider_.Rd similarity index 84% rename from man/slider_of.Rd rename to man/slider_.Rd index 6c411ee..3ecbc7c 100644 --- a/man/slider_of.Rd +++ b/man/slider_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/component.R -\name{slider_of} -\alias{slider_of} +\name{slider_} +\alias{slider_} \title{Add a Slider} \usage{ -slider_of(chart, channel, ...) +slider_(chart, channel, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -22,5 +22,5 @@ Add a range slider to a positional channel for zooming/panning. \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - slider_of('x') + slider_('x') } diff --git a/man/slider_x.Rd b/man/slider_x.Rd new file mode 100644 index 0000000..a6b2c24 --- /dev/null +++ b/man/slider_x.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{slider_x} +\alias{slider_x} +\title{Add an X Slider} +\usage{ +slider_x(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Slider options.} +} +\description{ +Add an X Slider +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp') |> + mark_point() |> + slider_x() +} diff --git a/man/slider_y.Rd b/man/slider_y.Rd new file mode 100644 index 0000000..068c0c6 --- /dev/null +++ b/man/slider_y.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/component.R +\name{slider_y} +\alias{slider_y} +\title{Add a Y Slider} +\usage{ +slider_y(chart, ...) +} +\arguments{ +\item{chart}{A \code{g2} object.} + +\item{...}{Slider options.} +} +\description{ +Add a Y Slider +} +\examples{ +g2(mtcars, x = 'mpg', y = 'hp') |> + mark_point() |> + slider_y() +} diff --git a/man/theme_of.Rd b/man/theme_.Rd similarity index 86% rename from man/theme_of.Rd rename to man/theme_.Rd index 3e99ca4..9e2035b 100644 --- a/man/theme_of.Rd +++ b/man/theme_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/theme.R -\name{theme_of} -\alias{theme_of} +\name{theme_} +\alias{theme_} \title{Set the Chart Theme} \usage{ -theme_of(chart, type, ...) +theme_(chart, type, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -24,10 +24,10 @@ G2 built-in themes: \code{'classic'} (default), \code{'classicDark'}, \code{'lig # Dark theme g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('dark') + theme_('dark') # Academy theme g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('academy') + theme_('academy') } diff --git a/man/theme_academy.Rd b/man/theme_academy.Rd index 1babbeb..ccea5dc 100644 --- a/man/theme_academy.Rd +++ b/man/theme_academy.Rd @@ -12,7 +12,7 @@ theme_academy(chart, ...) \item{...}{Additional theme options merged with the type.} } \description{ -Shortcut for \code{theme_of(chart, 'academy', ...)}. +Shortcut for \code{theme_(chart, 'academy', ...)}. } \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_academy() diff --git a/man/theme_classic.Rd b/man/theme_classic.Rd index c2eb79a..e0d2e2f 100644 --- a/man/theme_classic.Rd +++ b/man/theme_classic.Rd @@ -12,7 +12,7 @@ theme_classic(chart, ...) \item{...}{Additional theme options merged with the type.} } \description{ -Shortcut for \code{theme_of(chart, 'classic', ...)}. This is the default theme. +Shortcut for \code{theme_(chart, 'classic', ...)}. This is the default theme. } \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_classic() diff --git a/man/theme_classicDark.Rd b/man/theme_classicDark.Rd index 577241b..6d0d53f 100644 --- a/man/theme_classicDark.Rd +++ b/man/theme_classicDark.Rd @@ -12,7 +12,7 @@ theme_classicDark(chart, ...) \item{...}{Additional theme options merged with the type.} } \description{ -Shortcut for \code{theme_of(chart, 'classicDark', ...)}. +Shortcut for \code{theme_(chart, 'classicDark', ...)}. } \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_classicDark() diff --git a/man/theme_dark.Rd b/man/theme_dark.Rd index 8732bd2..67d02bc 100644 --- a/man/theme_dark.Rd +++ b/man/theme_dark.Rd @@ -12,7 +12,7 @@ theme_dark(chart, ...) \item{...}{Additional theme options merged with the type.} } \description{ -Shortcut for \code{theme_of(chart, 'dark', ...)}. +Shortcut for \code{theme_(chart, 'dark', ...)}. } \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_dark() diff --git a/man/theme_light.Rd b/man/theme_light.Rd index a37fa39..f692737 100644 --- a/man/theme_light.Rd +++ b/man/theme_light.Rd @@ -12,7 +12,7 @@ theme_light(chart, ...) \item{...}{Additional theme options merged with the type.} } \description{ -Shortcut for \code{theme_of(chart, 'light', ...)}. +Shortcut for \code{theme_(chart, 'light', ...)}. } \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> theme_light() diff --git a/man/title_of.Rd b/man/title_.Rd similarity index 79% rename from man/title_of.Rd rename to man/title_.Rd index 21a3b30..9de212a 100644 --- a/man/title_of.Rd +++ b/man/title_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/component.R -\name{title_of} -\alias{title_of} +\name{title_} +\alias{title_} \title{Set the Chart Title} \usage{ -title_of(chart, text, ...) +title_(chart, text, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -22,5 +22,5 @@ Set the Chart Title \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - title_of('Motor Trend Cars', subtitle = 'mpg vs hp') + title_('Motor Trend Cars', subtitle = 'mpg vs hp') } diff --git a/man/tooltip_of.Rd b/man/tooltip_.Rd similarity index 85% rename from man/tooltip_of.Rd rename to man/tooltip_.Rd index 2b21cd0..7875ae7 100644 --- a/man/tooltip_of.Rd +++ b/man/tooltip_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/component.R -\name{tooltip_of} -\alias{tooltip_of} +\name{tooltip_} +\alias{tooltip_} \title{Configure the Tooltip} \usage{ -tooltip_of(chart, ...) +tooltip_(chart, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -22,5 +22,5 @@ in \verb{mark_*()} functions. \examples{ g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - tooltip_of(crosshairs = TRUE) + tooltip_(crosshairs = TRUE) } diff --git a/man/transform_of.Rd b/man/transform_.Rd similarity index 88% rename from man/transform_of.Rd rename to man/transform_.Rd index fecc4ab..35e019f 100644 --- a/man/transform_of.Rd +++ b/man/transform_.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/transform.R -\name{transform_of} -\alias{transform_of} +\name{transform_} +\alias{transform_} \title{Add a Data Transform to the Last Mark} \usage{ -transform_of(chart, type, ...) +transform_(chart, type, ...) } \arguments{ \item{chart}{A \code{g2} object.} @@ -40,26 +40,26 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') + transform_('stackY') # Grouped (dodged) bar chart g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('dodgeX') + transform_('dodgeX') # Percent stacked bar (normalizeY + stackY) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') |> - transform_of('normalizeY') + transform_('stackY') |> + transform_('normalizeY') # Jitter on a scatter plot g2(mtcars, x = 'cyl', y = 'hp') |> mark_point() |> - transform_of('jitterX') + transform_('jitterX') # Histogram using binX g2(mtcars, x = 'mpg') |> mark_interval(encode = list(y = 'count')) |> - transform_of('binX', thresholds = 15) + transform_('binX', thresholds = 15) } diff --git a/tests/testit/test-gglite.R b/tests/testit/test-gglite.R index ca6e521..e203717 100644 --- a/tests/testit/test-gglite.R +++ b/tests/testit/test-gglite.R @@ -36,7 +36,7 @@ assert('build_config() produces correct spec', { df = data.frame(x = c('A', 'B'), y = c(3, 7)) chart = g2(df, x = 'x', y = 'y') |> mark_interval() |> - scale_of('y', nice = TRUE) + scale_('y', nice = TRUE) config = build_config(chart) # spec should NOT contain constructor options @@ -69,9 +69,9 @@ assert('chart_html() generates correct HTML structure', { (grepl('"column"', html)) }) -# transform_of() adds transforms to the last mark -assert('transform_of() adds transforms', { - chart = g2() |> mark_interval() |> transform_of('stackY') +# transform_() adds transforms to the last mark +assert('transform_() adds transforms', { + chart = g2() |> mark_interval() |> transform_('stackY') (length(chart$layers[[1]]$transform) %==% 1L) (chart$layers[[1]]$transform[[1]]$type %==% 'stackY') }) @@ -100,14 +100,14 @@ assert('animate() sets animation options', { (chart$layers[[1]]$animate$enter$type %==% 'fadeIn') }) -# theme_of(), axis_of(), legend_of(), title_of() set chart options +# theme_(), axis_(), legend_(), title_() set chart options assert('component functions set chart options', { chart = g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - theme_of('dark') |> - axis_of('x', title = 'MPG') |> - legend_of('color', position = 'right') |> - title_of('Cars') + theme_('dark') |> + axis_('x', title = 'MPG') |> + legend_('color', position = 'right') |> + title_('Cars') (chart$theme$type %==% 'dark') (chart$axes$x$title %==% 'MPG') (chart$legends$color$position %==% 'right') @@ -118,8 +118,8 @@ assert('component functions set chart options', { assert('pipe chaining works end-to-end', { chart = g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('x', type = 'linear') |> - coordinate('polar') |> + scale_('x', type = 'linear') |> + coord_polar() |> interact('tooltip') (inherits(chart, 'g2')) (length(chart$layers) %==% 1L) @@ -180,3 +180,63 @@ assert('theme_academy() sets academy theme', { chart = g2() |> mark_point() |> theme_academy() (chart$theme$type %==% 'academy') }) + +# g2() layout arguments (padding, margin, inset) +assert('g2() padding scalar sets layout', { + chart = g2(padding = 20) + (chart$layout$padding %==% 20) +}) + +assert('g2() padding vector sets layout sides', { + chart = g2(padding = c(30, NA, NA, 10)) + (chart$layout$paddingTop %==% 30) + (chart$layout$paddingLeft %==% 10) + (is.null(chart$layout$paddingRight)) + (is.null(chart$layout$paddingBottom)) +}) + +assert('g2() margin and inset work', { + chart = g2(margin = 16, inset = c(5, 10, 5, 10)) + (chart$layout$margin %==% 16) + (chart$layout$insetTop %==% 5) + (chart$layout$insetRight %==% 10) +}) + +assert('process_layout rejects bad lengths', { + (has_error(process_layout('padding', c(1, 2)))) +}) + +# Helper wrapper functions +assert('scale_x() is shortcut for scale_(x)', { + chart = g2() |> mark_point() |> scale_x(type = 'log') + (chart$scales$x$type %==% 'log') +}) + +assert('axis_x() and axis_y() are shortcuts', { + chart = g2() |> mark_point() |> + axis_x(title = 'X') |> axis_y(title = 'Y') + (chart$axes$x$title %==% 'X') + (chart$axes$y$title %==% 'Y') +}) + +assert('legend_color() is shortcut for legend_(color)', { + chart = g2() |> mark_point() |> legend_color(position = 'right') + (chart$legends$color$position %==% 'right') +}) + +assert('slider_x() and scrollbar_y() are shortcuts', { + chart = g2() |> mark_point() |> slider_x() |> scrollbar_y() + (isTRUE(chart$sliders$x)) + (isTRUE(chart$scrollbars$y)) +}) + +assert('build_config includes layout options', { + chart = g2(data.frame(x = 1, y = 2), x = 'x', y = 'y', + padding = 20, inset = c(5, NA, 5, NA)) |> + mark_point() + config = build_config(chart) + (config$padding %==% 20) + (config$insetTop %==% 5) + (config$insetBottom %==% 5) + (is.null(config$insetRight)) +}) diff --git a/vignettes/gglite.Rmd b/vignettes/gglite.Rmd index 923811c..c3027c3 100644 --- a/vignettes/gglite.Rmd +++ b/vignettes/gglite.Rmd @@ -100,14 +100,14 @@ g2(df, x = 'x', y = 'y') |> ## Scales -Scales control how data values map to visual properties. Use `scale_of()` to -configure the scale for a given channel: +Scales control how data values map to visual properties. Use helpers like +`scale_x()`, `scale_y()`, and `scale_color()` to configure scales: ```{r} g2(mtcars, x = 'mpg', y = 'hp', color = 'wt') |> mark_point() |> - scale_of('y', type = 'log') |> - scale_of('color', palette = 'viridis') + scale_y(type = 'log') |> + scale_color(palette = 'viridis') ``` Custom domain and range: @@ -115,8 +115,8 @@ Custom domain and range: ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('x', domain = c(10, 35)) |> - scale_of('y', domain = c(0, 400)) + scale_x(domain = c(10, 35)) |> + scale_y(domain = c(0, 400)) ``` ## Coordinates @@ -138,7 +138,7 @@ g2(df, x = 'x', y = 'y', color = 'x') |> ```{r} g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - transform_of('stackY') |> + transform_('stackY') |> coord_theta(innerRadius = 0.5) ``` @@ -154,7 +154,7 @@ g2(df, x = 'x', y = 'y') |> ## Transforms -Transforms modify the data before rendering. Use `transform_of()` to apply +Transforms modify the data before rendering. Use `transform_()` to apply statistical or layout transforms: ### Stacked bars @@ -166,7 +166,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') + transform_('stackY') ``` ### Dodged bars @@ -174,7 +174,7 @@ g2(df, x = 'x', y = 'y', color = 'color') |> ```{r} g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('dodgeX') + transform_('dodgeX') ``` ### Stacked area chart @@ -186,7 +186,7 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'group') |> mark_area() |> - transform_of('stackY') + transform_('stackY') ``` ## Facets @@ -221,7 +221,7 @@ legends, and labels. ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - title_of('Motor Trend Cars', subtitle = 'mpg vs horsepower') + title_('Motor Trend Cars', subtitle = 'mpg vs horsepower') ``` ### Tooltips @@ -229,7 +229,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ```{r} g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - tooltip_of(crosshairs = TRUE) + tooltip_(crosshairs = TRUE) ``` ### Labels @@ -238,7 +238,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> df = data.frame(x = c('A', 'B', 'C', 'D'), y = c(3, 7, 2, 5)) g2(df, x = 'x', y = 'y') |> mark_interval() |> - labels_of(text = 'y') + labels_(text = 'y') ``` ## Interactions @@ -265,9 +265,9 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'product') |> mark_interval() |> - transform_of('dodgeX') |> - scale_of('color', range = c('#5470c6', '#91cc75')) |> - title_of('Quarterly Sales', subtitle = 'By product line') |> + transform_('dodgeX') |> + scale_color(range = c('#5470c6', '#91cc75')) |> + title_('Quarterly Sales', subtitle = 'By product line') |> interact('tooltip') |> interact('elementHighlightByX') |> theme_classic() diff --git a/vignettes/ggplot2.Rmd b/vignettes/ggplot2.Rmd index f7200e5..ecb6a71 100644 --- a/vignettes/ggplot2.Rmd +++ b/vignettes/ggplot2.Rmd @@ -19,12 +19,12 @@ you can get started quickly. | `ggplot(data, aes(x, y))` | `g2(data, x = 'x', y = 'y')` | Column names are **character strings**, not bare symbols | | `+` operator | `\|>` pipe | gglite uses the native pipe | | `geom_point()` | `mark_point()` | "geom" → "mark" | -| `scale_x_log10()` | `scale_of('x', type = 'log')` | One function for all scales | +| `scale_x_log10()` | `scale_x(type = 'log')` | Helper functions for each channel | | `coord_flip()` | `coord_transpose()` | | | `coord_polar()` | `coord_polar()` | | | `facet_wrap()` / `facet_grid()` | `facet_rect()` / `facet_circle()` | | | `theme_minimal()` | `theme_light()` | | -| `labs(title = ...)` | `title_of(...)` | | +| `labs(title = ...)` | `title_(...)` | | | Static PNG/PDF | Interactive HTML/JS | Charts support tooltips, brushing, filtering | ## No non-standard evaluation @@ -152,18 +152,18 @@ df = data.frame( ) g2(df, x = 'x', y = 'y', color = 'color') |> mark_interval() |> - transform_of('stackY') + transform_('stackY') ``` ::: -For grouped (dodged) bars, replace `transform_of('stackY')` with -`transform_of('dodgeX')`. +For grouped (dodged) bars, replace `transform_('stackY')` with +`transform_('dodgeX')`. ## Scales In ggplot2 each scale has its own function (`scale_x_log10()`, -`scale_color_brewer()`, etc.). In gglite there is a single `scale_of()` -function: +`scale_color_brewer()`, etc.). In gglite there are helpers like `scale_x()`, +`scale_y()`, and `scale_color()`: ::: flex-col ```r @@ -177,7 +177,7 @@ ggplot(mtcars, aes(mpg, hp)) + # gglite g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - scale_of('y', type = 'log') + scale_y(type = 'log') ``` ::: @@ -188,7 +188,7 @@ Custom color palettes: g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> mark_point() |> - scale_of('color', range = c('#e41a1c', '#377eb8', '#4daf4a')) + scale_color(range = c('#e41a1c', '#377eb8', '#4daf4a')) ``` ## Coordinates @@ -217,7 +217,7 @@ Polar coordinates create rose or pie charts: # gglite g2(df, x = 'x', y = 'y', color = 'x') |> mark_interval() |> - transform_of('stackY') |> + transform_('stackY') |> coord_theta(innerRadius = 0.5) ``` @@ -262,7 +262,7 @@ g2(mtcars, x = 'mpg', y = 'hp') |> ## Titles and labels -In ggplot2 you use `labs()`; in gglite use `title_of()`: +In ggplot2 you use `labs()`; in gglite use `title_()`: ::: flex-col ```r @@ -277,7 +277,7 @@ ggplot(mtcars, aes(mpg, hp)) + # gglite g2(mtcars, x = 'mpg', y = 'hp') |> mark_point() |> - title_of('Motor Trend Cars', + title_('Motor Trend Cars', subtitle = 'mpg vs horsepower') ``` ::: @@ -327,12 +327,12 @@ g2(data, x = 'col1', y = 'col2', color = 'col3') |> mark_area() # Transforms (position adjustments) -|> transform_of('stackY') -|> transform_of('dodgeX') +|> transform_('stackY') +|> transform_('dodgeX') # Scales -|> scale_of('x', type = 'log') -|> scale_of('color', palette = 'viridis') +|> scale_x(type = 'log') +|> scale_color(palette = 'viridis') # Coordinates |> coord_polar() @@ -346,9 +346,9 @@ g2(data, x = 'col1', y = 'col2', color = 'col3') |> theme_dark() # Components -|> title_of('Title', subtitle = 'Subtitle') -|> tooltip_of(crosshairs = TRUE) -|> labels_of(text = 'y') +|> title_('Title', subtitle = 'Subtitle') +|> tooltip_(crosshairs = TRUE) +|> labels_(text = 'y') # Interactions |> interact('tooltip')