|
| 1 | +#' Configure an Axis |
| 2 | +#' |
| 3 | +#' Customise the axis for a positional channel (`'x'` or `'y'`). Set to |
| 4 | +#' `FALSE` to hide the axis. |
| 5 | +#' |
| 6 | +#' @param chart A `g2` object. |
| 7 | +#' @param channel Positional channel: `'x'` or `'y'`. |
| 8 | +#' @param ... Axis options such as `title`, `labelFormatter`, `tickCount`, |
| 9 | +#' `grid`, etc., or `FALSE` to hide. |
| 10 | +#' @return The modified `g2` object. |
| 11 | +#' @export |
| 12 | +#' @examples |
| 13 | +#' g2(mtcars, x = 'mpg', y = 'hp') |> |
| 14 | +#' mark_point() |> |
| 15 | +#' axis_of('x', title = 'Miles per Gallon') |> |
| 16 | +#' axis_of('y', title = 'Horsepower') |
| 17 | +axis_of = function(chart, channel, ...) { |
| 18 | + args = list(...) |
| 19 | + if (length(args) == 1 && is.logical(args[[1]])) { |
| 20 | + chart$axes[[channel]] = args[[1]] |
| 21 | + } else { |
| 22 | + chart$axes[[channel]] = args |
| 23 | + } |
| 24 | + chart |
| 25 | +} |
| 26 | + |
| 27 | +#' Configure a Legend |
| 28 | +#' |
| 29 | +#' Customise the legend for a visual channel (`'color'`, `'size'`, `'shape'`, |
| 30 | +#' `'opacity'`). Set to `FALSE` to hide. |
| 31 | +#' |
| 32 | +#' @param chart A `g2` object. |
| 33 | +#' @param channel Visual channel name. |
| 34 | +#' @param ... Legend options such as `position` (`'top'`, `'bottom'`, `'left'`, |
| 35 | +#' `'right'`), `layout`, `title`, etc. |
| 36 | +#' @return The modified `g2` object. |
| 37 | +#' @export |
| 38 | +#' @examples |
| 39 | +#' g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |> |
| 40 | +#' mark_point() |> |
| 41 | +#' legend_of('color', position = 'right') |
| 42 | +legend_of = function(chart, channel, ...) { |
| 43 | + args = list(...) |
| 44 | + if (length(args) == 1 && is.logical(args[[1]])) { |
| 45 | + chart$legends[[channel]] = args[[1]] |
| 46 | + } else { |
| 47 | + chart$legends[[channel]] = args |
| 48 | + } |
| 49 | + chart |
| 50 | +} |
| 51 | + |
| 52 | +#' Set the Chart Title |
| 53 | +#' |
| 54 | +#' @param chart A `g2` object. |
| 55 | +#' @param text Title text string. |
| 56 | +#' @param ... Additional title options such as `subtitle`, `align`, `style`. |
| 57 | +#' @return The modified `g2` object. |
| 58 | +#' @export |
| 59 | +#' @examples |
| 60 | +#' g2(mtcars, x = 'mpg', y = 'hp') |> |
| 61 | +#' mark_point() |> |
| 62 | +#' title_of('Motor Trend Cars', subtitle = 'mpg vs hp') |
| 63 | +title_of = function(chart, text, ...) { |
| 64 | + dots = list(...) |
| 65 | + if (length(dots)) { |
| 66 | + chart$chart_title = c(list(title = text), dots) |
| 67 | + } else { |
| 68 | + chart$chart_title = text |
| 69 | + } |
| 70 | + chart |
| 71 | +} |
| 72 | + |
| 73 | +#' Configure the Tooltip |
| 74 | +#' |
| 75 | +#' Set chart-level tooltip options. Mark-level tooltips can be passed via `...` |
| 76 | +#' in `mark_*()` functions. |
| 77 | +#' |
| 78 | +#' @param chart A `g2` object. |
| 79 | +#' @param ... Tooltip options such as `shared`, `crosshairs`, `marker`, or |
| 80 | +#' `FALSE` to disable. |
| 81 | +#' @return The modified `g2` object. |
| 82 | +#' @export |
| 83 | +#' @examples |
| 84 | +#' g2(mtcars, x = 'mpg', y = 'hp') |> |
| 85 | +#' mark_point() |> |
| 86 | +#' tooltip_of(crosshairs = TRUE) |
| 87 | +tooltip_of = function(chart, ...) { |
| 88 | + args = list(...) |
| 89 | + if (length(args) == 1 && is.logical(args[[1]])) { |
| 90 | + chart$tooltip_config = args[[1]] |
| 91 | + } else { |
| 92 | + chart$tooltip_config = args |
| 93 | + } |
| 94 | + chart |
| 95 | +} |
| 96 | + |
| 97 | +#' Add Labels to the Last Mark |
| 98 | +#' |
| 99 | +#' Append a label configuration to the most recently added mark. Can be called |
| 100 | +#' multiple times to add several label layers. |
| 101 | +#' |
| 102 | +#' @param chart A `g2` object. |
| 103 | +#' @param ... Label options such as `text` (channel name), `position`, |
| 104 | +#' `formatter`, `style`. |
| 105 | +#' @return The modified `g2` object. |
| 106 | +#' @export |
| 107 | +#' @examples |
| 108 | +#' df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2)) |
| 109 | +#' g2(df, x = 'x', y = 'y') |> |
| 110 | +#' mark_interval() |> |
| 111 | +#' labels_of(text = 'y', position = 'inside') |
| 112 | +labels_of = function(chart, ...) { |
| 113 | + n = length(chart$layers) |
| 114 | + if (n == 0) stop('add a mark before setting labels') |
| 115 | + chart$layers[[n]]$labels = c(chart$layers[[n]]$labels, list(list(...))) |
| 116 | + chart |
| 117 | +} |
| 118 | + |
| 119 | +#' Set Style on the Last Mark |
| 120 | +#' |
| 121 | +#' @param chart A `g2` object. |
| 122 | +#' @param ... Style options such as `fill`, `stroke`, `lineWidth`, |
| 123 | +#' `fillOpacity`, `strokeOpacity`. |
| 124 | +#' @return The modified `g2` object. |
| 125 | +#' @export |
| 126 | +#' @examples |
| 127 | +#' g2(mtcars, x = 'mpg', y = 'hp') |> |
| 128 | +#' mark_point() |> |
| 129 | +#' style_mark(fill = 'steelblue', stroke = 'white', lineWidth = 1) |
| 130 | +style_mark = function(chart, ...) { |
| 131 | + n = length(chart$layers) |
| 132 | + if (n == 0) stop('add a mark before setting style') |
| 133 | + chart$layers[[n]]$style = list(...) |
| 134 | + chart |
| 135 | +} |
| 136 | + |
| 137 | +#' Add a Slider |
| 138 | +#' |
| 139 | +#' Add a range slider to a positional channel for zooming/panning. |
| 140 | +#' |
| 141 | +#' @param chart A `g2` object. |
| 142 | +#' @param channel Positional channel: `'x'` or `'y'`. |
| 143 | +#' @param ... Slider options. |
| 144 | +#' @return The modified `g2` object. |
| 145 | +#' @export |
| 146 | +#' @examples |
| 147 | +#' g2(mtcars, x = 'mpg', y = 'hp') |> |
| 148 | +#' mark_point() |> |
| 149 | +#' slider_of('x') |
| 150 | +slider_of = function(chart, channel, ...) { |
| 151 | + if (is.null(chart$sliders)) chart$sliders = list() |
| 152 | + args = list(...) |
| 153 | + chart$sliders[[channel]] = if (length(args)) args else TRUE |
| 154 | + chart |
| 155 | +} |
| 156 | + |
| 157 | +#' Add a Scrollbar |
| 158 | +#' |
| 159 | +#' @param chart A `g2` object. |
| 160 | +#' @param channel Positional channel: `'x'` or `'y'`. |
| 161 | +#' @param ... Scrollbar options. |
| 162 | +#' @return The modified `g2` object. |
| 163 | +#' @export |
| 164 | +#' @examples |
| 165 | +#' df = data.frame(x = 1:100, y = cumsum(rnorm(100))) |
| 166 | +#' g2(df, x = 'x', y = 'y') |> |
| 167 | +#' mark_line() |> |
| 168 | +#' scrollbar_of('x') |
| 169 | +scrollbar_of = function(chart, channel, ...) { |
| 170 | + if (is.null(chart$scrollbars)) chart$scrollbars = list() |
| 171 | + args = list(...) |
| 172 | + chart$scrollbars[[channel]] = if (length(args)) args else TRUE |
| 173 | + chart |
| 174 | +} |
0 commit comments