From dcb564b298432c53a2ccc4320e41fa0258c95c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Fri, 30 Jul 2021 11:46:52 +0200 Subject: [PATCH] add the *mode* aggregation the plot example shows the most common word of length x as a title for each bar closes #420 (cherry-pick of ea9f8cdf8e0181d748a8175b664e690e0dcd0183) --- README.md | 1 + src/transforms/group.js | 3 +- test/output/wordLengthMobyDick.svg | 56 +++++++++++++++++++++-------- test/plots/word-length-moby-dick.js | 2 +- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f7cabc7799..3be95e9e43 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/transforms/group.js b/src/transforms/group.js index 7c6ee2f3b5..b3d7bcb5ae 100644 --- a/src/transforms/group.js +++ b/src/transforms/group.js @@ -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"; @@ -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"); } diff --git a/test/output/wordLengthMobyDick.svg b/test/output/wordLengthMobyDick.svg index c01a5f9d45..5b9cff28d1 100644 --- a/test/output/wordLengthMobyDick.svg +++ b/test/output/wordLengthMobyDick.svg @@ -90,19 +90,47 @@ Word length → - - - - - - - - - - - - - - + + a + + + of + + + the + + + that + + + there + + + though + + + because + + + whenever + + + passenger + + + passengers + + + circulation + + + metaphysical + + + involuntarily + + + Circumambulate + \ No newline at end of file diff --git a/test/plots/word-length-moby-dick.js b/test/plots/word-length-moby-dick.js index 15319707cc..5de3b53024 100644 --- a/test/plots/word-length-moby-dick.js +++ b/test/plots/word-length-moby-dick.js @@ -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})) ] }); }