Skip to content

Commit

Permalink
rank, quantile (#570)
Browse files Browse the repository at this point in the history
* 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 <fil@rezo.net>
  • Loading branch information
mbostock and Fil authored Oct 1, 2021
1 parent 4e66e13 commit ec55d5e
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 69 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,8 @@ Like the [group](#group) and [bin](#bin) transforms, the [Plot.map](#plotmapoutp
The following map methods are supported:

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

Expand Down
9 changes: 8 additions & 1 deletion src/transforms/map.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {group} from "d3";
import {count, group, rank} from "d3";
import {maybeZ, take, valueof, maybeInput, lazyChannel} from "../mark.js";
import {basic} from "./basic.js";

Expand Down Expand Up @@ -43,10 +43,17 @@ function maybeMap(map) {
if (typeof map === "function") return mapFunction(map);
switch (`${map}`.toLowerCase()) {
case "cumsum": return mapCumsum;
case "rank": return mapFunction(rank);
case "quantile": return mapFunction(rankQuantile);
}
throw new Error("invalid map");
}

function rankQuantile(V) {
const n = count(V) - 1;
return rank(V).map(r => r / n);
}

function mapFunction(f) {
return {
map(I, S, T) {
Expand Down
Loading

0 comments on commit ec55d5e

Please sign in to comment.