Skip to content

add the *mode* aggregation #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {group as grouper, sort, sum, deviation, min, max, mean, median, variance, InternSet} from "d3";
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance, InternSet} 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 @@ -149,6 +149,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
Loading
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
Original file line number Diff line number Diff line change
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}))
]
});
}