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"][##]} &)]
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.
Answer
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"][##]} &)]
Comments
Post a Comment