Skip to content

Commit ec55d5e

Browse files
mbostockFil
andauthored
rank, quantile (#570)
* update dependencies * rank and quantile map methods * count, and fix off by one * document rank and quantile map methods Co-authored-by: Philippe Rivière <[email protected]>
1 parent 4e66e13 commit ec55d5e

File tree

6 files changed

+445
-69
lines changed

6 files changed

+445
-69
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,8 @@ Like the [group](#group) and [bin](#bin) transforms, the [Plot.map](#plotmapoutp
13191319
The following map methods are supported:
13201320

13211321
* *cumsum* - a cumulative sum
1322+
* *rank* - the rank of each value in the sorted array
1323+
* *quantile* - the rank, normalized between 0 and 1
13221324
* a function to be passed an array of values, returning new values
13231325
* an object that implements the *map* method
13241326

src/transforms/map.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {group} from "d3";
1+
import {count, group, rank} from "d3";
22
import {maybeZ, take, valueof, maybeInput, lazyChannel} from "../mark.js";
33
import {basic} from "./basic.js";
44

@@ -43,10 +43,17 @@ function maybeMap(map) {
4343
if (typeof map === "function") return mapFunction(map);
4444
switch (`${map}`.toLowerCase()) {
4545
case "cumsum": return mapCumsum;
46+
case "rank": return mapFunction(rank);
47+
case "quantile": return mapFunction(rankQuantile);
4648
}
4749
throw new Error("invalid map");
4850
}
4951

52+
function rankQuantile(V) {
53+
const n = count(V) - 1;
54+
return rank(V).map(r => r / n);
55+
}
56+
5057
function mapFunction(f) {
5158
return {
5259
map(I, S, T) {

0 commit comments

Comments
 (0)