33# ' Customise the axis for a positional channel (`'x'` or `'y'`). Set to
44# ' `FALSE` to hide the axis. When called immediately after a `mark_*()`
55# ' function (or `style_mark()`, `labels_()`, etc.), the axis is applied to
6- # ' that mark only, enabling per-mark axis customization (e.g., a right-side
7- # ' y-axis for a dual-axis chart). Otherwise it applies at the chart level.
6+ # ' that mark only, enabling per-mark axis customization for dual-axis charts.
7+ # ' Otherwise it applies at the chart level.
88# '
99# ' @param chart A `g2` object.
1010# ' @param channel Positional channel: `'x'` or `'y'`.
1818# ' axis_('x', title = 'Miles per Gallon') |>
1919# ' axis_('y', title = 'Horsepower')
2020# '
21- # ' # Mark-level axis for dual- axis chart
22- # ' df = data.frame(x = 1:5, a = c(1, 4, 2, 5, 3), b = c(100, 200, 150, 300, 250) )
23- # ' g2(df, ~ x) |>
24- # ' mark_interval(encode = list(y = 'a') ) |>
25- # ' mark_line (encode = list(y = 'b ')) |>
21+ # ' # Dual- axis chart: each mark gets its own axis immediately after mark_*()
22+ # ' air = aggregate(cbind(Temp, Wind) ~ Month, data = airquality, FUN = mean )
23+ # ' air$Month = month.abb[air$Month]
24+ # ' g2(air, x = 'Month' ) |>
25+ # ' mark_interval (encode = list(y = 'Temp ')) |>
2626# ' scale_y(independent = TRUE) |>
27- # ' axis_y(position = 'right', grid = FALSE)
27+ # ' axis_y(title = 'Temperature (°F)') |>
28+ # ' mark_line(encode = list(y = 'Wind')) |>
29+ # ' scale_y(independent = TRUE) |>
30+ # ' axis_y(position = 'right', grid = FALSE, title = 'Wind Speed (mph)')
2831axis_ = function (chart = NULL , channel , ... ) {
2932 mod = check_chart(axis_ , chart , c(if (! missing(channel )) list (channel ), list (... )))
3033 if (! is.null(mod )) return (mod )
@@ -144,41 +147,36 @@ title_ = function(chart = NULL, text, ...) {
144147 chart
145148}
146149
147- # Keys that configure the tooltip interaction (e.g., crosshairs, shared) rather
148- # than the tooltip data items (e.g., channel, valueFormatter). Used by tooltip_()
149- # to route args to the correct G2 spec location.
150- .tooltip_interact_keys = c(
151- ' shared' , ' series' , ' facet' , ' body' , ' crosshairs' , ' marker' ,
152- ' groupName' , ' disableNative' , ' disableAutoHide' , ' offset' ,
153- ' position' , ' bounding' , ' mount' , ' css' , ' enterable' , ' sort' ,
154- ' filter' , ' render'
155- )
156-
157150# ' Configure the Tooltip
158151# '
159- # ' Configure tooltip behavior and data display. Behavior options such as
160- # ' `crosshairs` and `shared` are applied to the tooltip interaction
161- # ' (`interaction.tooltip` in G2). Data display options such as `channel` and
162- # ' `valueFormatter` are applied to the last mark's tooltip (call after adding
163- # ' marks). Pass `FALSE` to disable the tooltip entirely.
152+ # ' Configure tooltip interaction behavior. All options are applied to
153+ # ' `interaction.tooltip` in the G2 spec: pass `FALSE` to disable the tooltip,
154+ # ' or pass named options such as `crosshairs`, `shared`, `marker`, and any
155+ # ' `crosshairs*`/`marker*` style properties. To configure the data displayed in
156+ # ' a tooltip for a specific mark (e.g., `channel`, `valueFormatter`, `items`),
157+ # ' pass a `tooltip` list argument directly to the mark function instead, e.g.,
158+ # ' `mark_line(tooltip = list(channel = 'y', valueFormatter = '.0%'))`.
164159# '
165160# ' @param chart A `g2` object.
166- # ' @param ... Tooltip options. Behavior options: `shared`, `crosshairs`,
167- # ' `marker`, `series`, `facet`, `groupName`, and all `crosshairs*`/`marker*`
168- # ' style props. Data options (applied to the last mark): `channel`,
169- # ' `valueFormatter`, `title`, `items`, `name`, `color`. Or `FALSE` to
170- # ' disable.
161+ # ' @param ... Tooltip interaction options such as `shared`, `crosshairs`,
162+ # ' `marker`, `series`, `crosshairsStroke`, or `FALSE` to disable the tooltip.
171163# ' @return The modified `g2` object.
172164# ' @export
173165# ' @examples
174- # ' # Enable crosshairs
175- # ' g2(mtcars, hp ~ mpg) |>
166+ # ' # Enable crosshairs (works best with line/area marks which use series tooltip)
167+ # ' df = data.frame(x = 1:6, y = c(3, 1, 4, 1, 5, 2))
168+ # ' g2(df, y ~ x) |>
169+ # ' mark_line() |>
176170# ' tooltip_(crosshairs = TRUE)
177171# '
178- # ' # Format y-axis values in tooltip (call after mark)
179- # ' g2(mtcars, hp ~ mpg) |>
180- # ' mark_point() |>
181- # ' tooltip_(channel = 'y', valueFormatter = '.0f')
172+ # ' # Shared tooltip for multi-series line chart
173+ # ' df2 = data.frame(
174+ # ' x = rep(1:5, 2), y = c(3, 1, 4, 1, 5, 2, 7, 1, 8, 3),
175+ # ' group = rep(c('A', 'B'), each = 5)
176+ # ' )
177+ # ' g2(df2, y ~ x, color = ~ group) |>
178+ # ' mark_line() |>
179+ # ' tooltip_(shared = TRUE)
182180# '
183181# ' # Disable tooltip
184182# ' g2(mtcars, hp ~ mpg) |>
@@ -191,27 +189,8 @@ tooltip_ = function(chart = NULL, ...) {
191189 chart $ interactions [[' tooltip' ]] = args [[1 ]]
192190 return (chart )
193191 }
194- keys = names(args )
195- # grepl prefix match covers crosshairs* and marker* style props (e.g.
196- # crosshairsStroke, markerFill) which are too numerous to enumerate
197- is_int = keys %in% .tooltip_interact_keys | grepl(' ^crosshairs|^marker' , keys )
198- if (any(is_int )) {
199- cur = if (is.list(chart $ interactions [[' tooltip' ]])) chart $ interactions [[' tooltip' ]] else list ()
200- chart $ interactions [[' tooltip' ]] = modifyList(cur , args [is_int ])
201- }
202- data_args = args [! is_int ]
203- if (length(data_args )) {
204- if (length(chart $ layers )) {
205- n = length(chart $ layers )
206- cur = if (is.list(chart $ layers [[n ]]$ tooltip )) chart $ layers [[n ]]$ tooltip else list ()
207- chart $ layers [[n ]]$ tooltip = modifyList(cur , data_args )
208- } else {
209- # No marks yet: store at chart level as a fallback. This is serialized
210- # as the view-level tooltip component, but may not be processed by G2
211- # for channel/valueFormatter — prefer calling tooltip_() after marks.
212- chart $ tooltip_config = modifyList(as.list(chart $ tooltip_config ), data_args )
213- }
214- }
192+ cur = if (is.list(chart $ interactions [[' tooltip' ]])) chart $ interactions [[' tooltip' ]] else list ()
193+ chart $ interactions [[' tooltip' ]] = modifyList(cur , args )
215194 chart
216195}
217196
0 commit comments