3
$\begingroup$

Is it possible to display standard deviation and mean in addition to the currently displayed metrics in the Tooltip of a BoxWhiskerChart.

BoxWhiskerChart[RandomVariate[NormalDistribution[0, 1], 100]]

enter image description here

Currently, it has 5 metrics (max, Q3, median, Q1 and min) and I want to add more metrics to the Tooltip. I appreciate your time and help.

$\endgroup$
1

1 Answer 1

5
$\begingroup$
$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

label[data_, index_, label_] := Grid[
  Prepend[
   Transpose[
    {Style[#, Bold] & /@ {"max", "75%", "median", "25%", "min", 
       "μ", "σ"},
     #@data & /@ {Max, Quantile[#, 3/4] &, Median, Quantile[#, 1/4] &,
        Min, Mean, StandardDeviation}}],
   {StringForm["Case: ``", index[[2]]], SpanFromLeft}],
  Frame -> All]

SeedRandom[1234];

data = Table[
   RandomVariate[NormalDistribution[μ, 1], 
    100], {μ, {0, 3, 2, 5}}];

BoxWhiskerChart[data, "Outliers", 
 LabelingFunction -> (Placed[label[##], Tooltip] &), 
 ChartLabels -> Range[Length[data]]]

enter image description here

EDIT: The default definition for Quantile does not give the same results as Quartiles. From the documentation, "Quartiles[list] is equivalent to Quantile[list, {1/4, 1/2, 3/4}, {{1/2, 0}, {0, 1}}]."

For example,

(Quantile[#, {1/4, 1/2, 3/4}] & /@ data) == (Quartiles /@ data)

(* False *)

(Quantile[#, 1/2] & /@ data) == (Median /@ data)

(* False *)

(Quantile[#, {1/4, 1/2, 3/4}, {{1/2, 0}, {0, 1}}] & /@ data) === 
    (Quartiles /@ data)

(* True *)

(Quantile[#, 1/2, {{1/2, 0}, {0, 1}}] & /@ data) == 
    (Median /@ data)

(* True *)

Using the modified Quantile

label2[data_, index_, label_] := 
 Grid[Prepend[
   Transpose[{Style[#, Bold] & /@ {"max", "75%", "median", "25%", "min", 
       "μ", "σ"}, #@data & /@ {Max, 
       Quantile[#, 3/4, {{1/2, 0}, {0, 1}}] &, Median, 
       Quantile[#, 1/4, {{1/2, 0}, {0, 1}}] &, Min, Mean, 
       StandardDeviation}}], {StringForm["Case: ``", index[[2]]], 
    SpanFromLeft}], Frame -> All]

BoxWhiskerChart[data, "Outliers", 
 LabelingFunction -> (Placed[label2[##], Tooltip] &), 
 ChartLabels -> Range[Length[data]]]

(* same chart with modified tooltips *)

$\endgroup$
1
  • $\begingroup$ This is awesome, Thanks @Bob $\endgroup$
    – Rupesh
    Sep 10 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.