-
Notifications
You must be signed in to change notification settings - Fork 51
/
pos.py
344 lines (285 loc) · 10.2 KB
/
pos.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#
# Copyright (c) 2019. JetBrains s.r.o.
# Use of this source code is governed by the MIT license that can be found in the LICENSE file.
#
from .core import FeatureSpec
#
# Position Adjustments
#
__all__ = ['position_dodge', 'position_dodgev', 'position_jitter', 'position_nudge', 'position_jitterdodge',
'position_stack', 'position_fill']
def position_dodge(width=None):
"""
Adjust position by dodging overlaps to the side.
Parameters
----------
width : float
Dodging width, when different to the width of the individual elements.
This is useful when you want to align narrow geoms with wider geoms.
The value of width is relative and typically ranges between 0 and 1.
Values that are greater than 1 lead to overlapping of the objects.
Returns
-------
`FeatureSpec`
Geom object position specification.
Notes
-----
Adjust position by dodging overlaps to the side.
Examples
--------
.. jupyter-execute::
:linenos:
:emphasize-lines: 10
import numpy as np
from lets_plot import *
LetsPlot.setup_html()
n = 100
np.random.seed(42)
x = np.random.randint(5, size=n)
c = np.random.choice(['a', 'b', 'c'], size=n)
ggplot({'x': x, 'c': c}, aes(x='x')) + \\
geom_bar(aes(fill='c'), width=.4, \\
position=position_dodge(width=.6))
"""
return _pos('dodge', width=width)
def position_dodgev(height=None):
"""
Adjust position by dodging overlaps to the side.
Parameters
----------
height : float
Dodging height, when different to the height of the individual elements.
This is useful when you want to align narrow geoms with taller geoms.
The value of height is relative and typically ranges between 0 and 1.
Values that are greater than 1 lead to overlapping of the objects.
Returns
-------
`FeatureSpec`
Geom object position specification.
Notes
-----
Adjust position by dodging overlaps to the side.
Examples
--------
.. jupyter-execute::
:linenos:
:emphasize-lines: 11
from lets_plot import *
LetsPlot.setup_html()
data = {
'xmin': [0.2, 4.6, 1.6, 3.5],
'xmax': [1.5, 5.3, 3.0, 4.4],
'y': ['a', 'a', 'b', 'b'],
'c': ['gr1', 'gr2', 'gr1', 'gr2']
}
ggplot(data, aes(y='y', color='c')) + \\
geom_errorbar(aes(xmin='xmin', xmax='xmax'), height=0.1, size=2, \\
position=position_dodgev(height=0.2))
"""
return _pos('dodgev', height=height)
def position_jitter(width=None, height=None, seed=None):
"""
Adjust position by assigning random noise to points. Better for discrete values.
Parameters
----------
width : float
Jittering width.
The value of width is relative and typically ranges between 0 and 0.5.
Values that are greater than 0.5 lead to overlapping of the points.
height : float
Jittering height.
The value of height is relative and typically ranges between 0 and 0.5.
Values that are greater than 0.5 lead to overlapping of the points.
seed : int
A random seed to make the jitter reproducible.
If None (the default value), the seed is initialised with a random value.
Returns
-------
`FeatureSpec`
Geom object position specification.
Notes
-----
Adjust position by dodging overlaps to the side.
Examples
--------
.. jupyter-execute::
:linenos:
:emphasize-lines: 12
import numpy as np
from lets_plot import *
LetsPlot.setup_html()
n = 100
np.random.seed(42)
x = np.random.randint(4, size=n)
y = np.random.randint(3, size=n)
c = np.char.add(x.astype(str), y.astype(str))
ggplot({'x': x, 'y': y, 'c': c}, aes('x', 'y')) + \\
geom_point(aes(fill='c'), show_legend=False, \\
size=8, alpha=.5, shape=21, color='black', \\
position=position_jitter(width=.2, height=.2, seed=42))
"""
return _pos('jitter', width=width, height=height, seed=seed)
def position_nudge(x=None, y=None):
"""
Adjust position by nudging a given offset.
Parameters
----------
x : float
Nudging width.
y : float
Nudging height.
Returns
-------
`FeatureSpec`
Geom object position specification.
Notes
-----
Adjust position by dodging overlaps to the side.
Examples
--------
.. jupyter-execute::
:linenos:
:emphasize-lines: 11
import numpy as np
from lets_plot import *
LetsPlot.setup_html()
n = 5
np.random.seed(42)
x = np.random.uniform(size=n)
y = np.random.uniform(size=n)
t = np.random.choice(list('abcdefghijk'), size=n)
ggplot({'x': x, 'y': y, 't': t}, aes('x', 'y')) + \\
geom_point(size=5, shape=21, color='black', fill='red') + \\
geom_text(aes(label='t'), position=position_nudge(y=.05))
"""
return _pos('nudge', x=x, y=y)
def position_jitterdodge(dodge_width=None, jitter_width=None, jitter_height=None, jitter_seed=None):
"""
This is primarily used for aligning points generated through `geom_point()`
with dodged boxplots (e.g., a `geom_boxplot()` with a fill aesthetic supplied).
Parameters
----------
dodge_width : float
Bin width.
The value of `dodge_width` is relative and typically ranges between 0 and 1.
Values that are greater than 1 lead to overlapping of the boxes.
jitter_width : float
Jittering width.
The value of `jitter_width` is relative and typically ranges between 0 and 0.5.
Values that are greater than 0.5 lead to overlapping of the points.
jitter_height : float
Jittering height.
The value of `jitter_height` is relative and typically ranges between 0 and 0.5.
Values that are greater than 0.5 lead to overlapping of the points.
jitter_seed : int
A random seed to make the jitter reproducible.
If None (the default value), the seed is initialised with a random value.
Returns
-------
`FeatureSpec`
Geom object position specification.
Notes
-----
Adjust position by dodging overlaps to the side.
Examples
--------
.. jupyter-execute::
:linenos:
:emphasize-lines: 13
import numpy as np
from lets_plot import *
LetsPlot.setup_html()
n = 50
np.random.seed(42)
x = np.random.uniform(size=n)
c = np.random.choice(['a', 'b', 'c'], size=n)
ggplot({'x': x, 'c': c}) + \\
geom_crossbar(aes(x='c', y='x', color='c'), \\
stat='boxplot') + \\
geom_point(aes(x='c', y='x', color='c'), \\
size=4, shape=21, fill='white',
position=position_jitterdodge(jitter_seed=42))
"""
return _pos('jitterdodge', dodge_width=dodge_width, jitter_width=jitter_width, jitter_height=jitter_height,
jitter_seed=jitter_seed)
def position_stack(vjust=None, mode=None):
"""
Adjust position by stacking overlapping objects on top of each other.
Preferred for density-like geometries.
Parameters
----------
vjust : float
Vertical adjustment for geoms that have a position (like points or lines),
not a dimension (like bars or areas).
Set to 0 to align with the bottom, 0.5 for the middle, and 1 for the top.
mode : {'groups', 'all'}, default='groups'
If 'groups', objects inside one group are positioned as in `position='identity'`,
but each group is shifted to sum of heights of previous groups
(where height of a group is a maximum of it's y values).
If 'all', each object will be shifted.
Returns
-------
`FeatureSpec`
Geom object position specification.
Notes
-----
Adjust position by stacking overlapping objects on top of each other.
Examples
--------
.. jupyter-execute::
:linenos:
:emphasize-lines: 9
from lets_plot import *
LetsPlot.setup_html()
data = {
'x': [1, 1, 1, 2, 2, 2],
'y': [1, 2, 3, 1, 2, 3],
'g': ["a", "b", "b", "a", "a", "b"],
}
ggplot(data, aes('x', 'y', color='g')) + \\
geom_point(position=position_stack(), size=10)
"""
return _pos('stack', vjust=vjust, mode=mode)
def position_fill(vjust=None, mode=None):
"""
Adjust position by stacking overlapping objects on top of each other
and standardise each stack to have constant height.
Parameters
----------
vjust : float
Vertical adjustment for geoms that have a position (like points or lines),
not a dimension (like bars or areas).
Set to 0 to align with the bottom, 0.5 for the middle, and 1 for the top.
mode : {'groups', 'all'}, default='groups'
If 'groups', objects inside one group are positioned as in `position='identity'`,
but each group is shifted to sum of heights of previous groups
(where height of a group is a maximum of it's y values).
If 'all', each object will be shifted.
Returns
-------
`FeatureSpec`
Geom object position specification.
Notes
-----
Adjust position by stacking overlapping objects on top of each other
and standardise each stack to have constant height.
Examples
--------
.. jupyter-execute::
:linenos:
:emphasize-lines: 9
from lets_plot import *
LetsPlot.setup_html()
data = {
'x': [1, 1, 1, 1, 1, 2, 2, 2],
'y': [1, 2, 3, 4, 5, 1, 2, 3],
'g': ["a", "a", "b", "b", "b", "a", "a", "b"],
}
ggplot(data, aes('x', 'y', color='g')) + \\
geom_point(position=position_fill(), size=10)
"""
return _pos('fill', vjust=vjust, mode=mode)
def _pos(name, **other):
args = locals().copy()
args.pop('other')
return FeatureSpec('pos', **args, **other)