4
$\begingroup$

I am creating a custom ChartElementFunction for BoxWhiskerChart. I would like to access the box-and-whisker specifications from the second parameter of BoxWhiskerChart to use in the custom function; just as the built-in element functions. Minimal custom element function:

ClearAll[cef];
cef[boundingBox_, data_, meta_] :=
 Module[{qt = Quantile[data, {0, 0.25, .5, 0.75, 1}, {{1/2, 0}, {0, 1}}],
         h = First@Differences@boundingBox[[2]],
         m = Mean@boundingBox[[2]]},
  {
   {Thickness[.005], CapForm["Butt"], Blue,
    Line[{
     {qt[[1]], m - .1 h}, 
     {qt[[1]], m + .1 h}}]},
   {Thickness[.005], CapForm["Butt"], Magenta,
    Line[{
     {qt[[5]], m - .1 h}, 
     {qt[[5]], m + .1 h}}]}
   }
  ]

Minimal example where the fences are drawn different colours. The box-and-whisker "Fences" specification says to draw them 80% of the height of the box-whisker. However, I don't have access to this and have to hard-code a value (in this case 20% of the height). The regular element function is added to cut down on the size of the post.

SeedRandom[953];
data = RandomVariate[ChiSquareDistribution[5], 100];
BoxWhiskerChart[data, {"Basic", {"Fences", .8, None}}, 
 BarOrigin -> Left,
 ChartElementFunction -> ({cef[##], ChartElementDataFunction["BoxWhisker"][##]} &)]

enter image description here

Can the second parameter box-and-whisker specifications be accessed in a custom ChartElementFunction as they are in the built-in ones? I would prefer not to move the specifications into a parameter of the custom function.

$\endgroup$
  • $\begingroup$ @JasonB Nope. Those are the normal arguments to the chart element function. I'm looking to access the {"Basic", {"Fences", .8, None}} argument to BoxWhiskerChart just as ChartElementDataFunction["BoxWhisker"] accesses it and knows to use None as the style of the fences (which is why there are no 80% height fences in the chart). $\endgroup$ – Edmund Jul 25 '16 at 22:17
  • $\begingroup$ yeah I figured that out as soon as I commented, deleted. Do you have the Spelunking package installed? It looks like you can get an understanding for the internals via Spelunk[Charting`iBoxWhiskerChart] $\endgroup$ – Jason B. Jul 25 '16 at 22:25
6
$\begingroup$

Inspecting the code for the function System`BarFunctionDump`boxplot, it looks like you can access the fence specs -- (.8, None) in your example -- using Charting`ChartStyleInformation["Fence"] inside your cef.

More generally, all box and whiskers specifications, "Color", "BarOrigin", "Outliers", "BoxRange" etc., can be accessed using Charting`ChartStyleInformation.

ClearAll[cef];
cef[boundingBox_, data_, meta_] := 
 Module[{qt = 
    Quantile[data, {0, 0.25, .5, 0.75, 1}, {{1/2, 0}, {0, 1}}], 
   h = First@Differences@boundingBox[[2]], 
   m = Mean@boundingBox[[2]]}, {{Thickness[.005], CapForm["Butt"], 
    Blue, Print /@ (Row[{#, " = ", Charting`ChartStyleInformation[#]}] & /@ 
    {"Color",  "BarOrigin", "Outliers", "BoxRange", "MedianConfInt",  "MeanConfInt", 
    "Whisker", "Fence", "MedianMarker", "MeanMarker", "MedianConfIntPara"}), 
    Line[{{qt[[1]], m - .1 h}, {qt[[1]], 
       m + .1 h}}]}, {Thickness[.005], CapForm["Butt"], Magenta, 
    Line[{{qt[[5]], m - .1 h}, {qt[[5]], m + .1 h}}]}}]



SeedRandom[953];

data = RandomVariate[ChiSquareDistribution[5], 100];
BoxWhiskerChart[data, {"Basic", {"Fences", .8, None},
   {"Outliers", "A", Green}, {"FarOutliers", "B", Orange}}, 
 BarOrigin -> Left, 
 ChartElementFunction -> ({cef[##], ChartElementDataFunction["BoxWhisker"][##]} &)]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Very nice find. (1+) Waiting a bit for the accept as is the custom. $\endgroup$ – Edmund Jul 26 '16 at 0:12
  • $\begingroup$ Did you find anything for "FarOutliers"? $\endgroup$ – Edmund Jul 26 '16 at 0:48
  • 1
    $\begingroup$ @Edmund, when "Outliers" and/or "FarOutliers" are specified Charting`ChartStyleInformation["Outliers"] gives a two-element list the second of which is the spec for "FarOutliers". $\endgroup$ – kglr Jul 26 '16 at 1:54

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.