Skip to content

Commit

Permalink
add the *mode* aggregation
Browse files Browse the repository at this point in the history
the plot example shows the most common word of length x as a title for each bar

closes #420
  • Loading branch information
Fil committed Jun 17, 2021
1 parent e9a24fc commit ea9f8cd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -978,6 +978,7 @@ The following aggregation methods are supported:
* *median* - the median value
* *deviation* - the standard deviation
* *variance* - the variance per [Welford’s algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm)
* *mode* - the value with the most occurrences
* a function to be passed the array of values for each bin
* an object with a *reduce* method

Expand Down
3 changes: 2 additions & 1 deletion src/transforms/group.js
@@ -1,4 +1,4 @@
import {group as grouper, sort, sum, deviation, min, max, mean, median, variance} from "d3";
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance} from "d3";
import {firstof} from "../defined.js";
import {valueof, maybeColor, maybeInput, maybeTransform, maybeTuple, maybeLazyChannel, lazyChannel, first, identity, take, labelof, range} from "../mark.js";

Expand Down Expand Up @@ -148,6 +148,7 @@ export function maybeReduce(reduce, value) {
case "mean": return reduceAccessor(mean);
case "median": return reduceAccessor(median);
case "variance": return reduceAccessor(variance);
case "mode": return reduceAccessor(mode);
}
throw new Error("invalid reduce");
}
Expand Down
56 changes: 42 additions & 14 deletions test/output/wordLengthMobyDick.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plots/word-length-moby-dick.js
Expand Up @@ -21,7 +21,7 @@ export default async function() {
percent: true
},
marks: [
Plot.barY(words, Plot.groupX({y: "proportion"}, {x: "length"}))
Plot.barY(words, Plot.groupX({y: "proportion", title: "mode"}, {x: "length", title: d => d}))
]
});
}

0 comments on commit ea9f8cd

Please sign in to comment.