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