2
$\begingroup$

I’d like to figure out how I can show my entire dataset overlaid on a box whisker plot.

data = {{0.763983, -1.11426, 1.05153, -0.0369066, 1.1571, 0.834856, 0.146695,
0.100646, 2.69399, 0.425281}, {0.506237, 1.05127, 1.58117, 
0.976295, 1.62204, 1.39005, 1.62387, 1.36098, 1.83858, 
0.709489}, {-1.62786, 0.947744, 4.50628, 0.444416, 1.30947, 0.60594,
0.890642, 4.04505, 4.57667, 0.781394}}

The following allows me to show Outliers overlaid on the plot. How would I go about overlaying all of the datapoint on the plot?

BoxWhiskerChart[data, {"Notched", {"MedianMarker", Blue}, {"Outliers", 
"\[EmptySmallCircle]", Red}},ChartLabels -> {"X", "Y", "Z"}]
$\endgroup$
  • $\begingroup$ Show[BoxWhiskerChart[data], ListPlot[MapIndexed[{#2[[1]], #1} &, data, {2}]]] $\endgroup$ – Dr. belisarius Feb 17 '14 at 22:08
  • $\begingroup$ Thanks this works well…! Still learning the intricacies of Mathematica’s plotting routines $\endgroup$ – Pam Feb 18 '14 at 14:11
  • $\begingroup$ Belisarius, can you explain how the MapIndexed trick works. I seem to have a hard time understanding it. Would appreciate some help….. $\endgroup$ – Pam Feb 18 '14 at 14:15
  • $\begingroup$ Can you post an answer so that I can acknowledge this as an answer? $\endgroup$ – Pam Feb 18 '14 at 14:16
3
$\begingroup$

One way to do it is:

Show[BoxWhiskerChart[data], ListPlot[MapIndexed[{#2[[1]], #1} &, data, {2}]]]

Mathematica graphics

MapIndexed operates just like Mapbut gives you a second argument which is the index of the list element you're getting each time.

$\endgroup$
  • $\begingroup$ I don’t still quite get {#2[[1]], #1} part of it… I know they stand for slot sequences but still don’t see how they map to the data… $\endgroup$ – Pam Feb 18 '14 at 15:08
  • $\begingroup$ @Pam Try MapIndexed[f, {a,b}] to see that #2[[1]] takes values 1, 2 and #1 takes values a, b $\endgroup$ – Dr. belisarius Feb 18 '14 at 15:13
2
$\begingroup$

You can overlay your data and BoxWhiskerChart using a custom ChartStyleFunction. The first example ceF1 combines built-in ChartElementDataFunction["BoxWhisker"] with the graphics primitives obtained from ListPlot of the data. The second and third examples use the buit-in functions "PointDensity" and "LineDensity" in place of ListPlot.

ceF1 = {PointSize[.015], ChartElementDataFunction["BoxWhisker"][##], 
    ListPlot[Thread[{Mean[#[[1]]], #2}], PlotStyle -> 
       Darker[Darker@Charting`ChartStyleInformation["Color"]]][[1]]} &;

ceF2 = {PointSize[.05], FaceForm[Opacity[.3]], 
    ChartElementDataFunction["PointDensity", "PointStyle" -> 
     Directive[Darker[Charting`ChartStyleInformation["Color"]],  PointSize[Large]]][##], 
   Opacity[.8], ChartElementDataFunction["BoxWhisker"][##]} &;

ceF3 = {Thick, Darker[Charting`ChartStyleInformation["Color"]], 
    ChartElementDataFunction["LineDensity"][##], Opacity[.8], 
    ChartElementDataFunction["BoxWhisker"][##]} &;

Examples:

Panel[BoxWhiskerChart[data, {"Notched", {"MedianMarker", Blue}, 
  {"Outliers", "\[EmptyCircle]", Directive[15, Bold, Red]}}, 
  ChartLabels -> {"X", "Y", "Z"}, ChartStyle -> 63, 
  ImageSize -> 320, ChartElementFunction -> ToExpression[#]], 
  Style["ChartElementFunction->" <> #, 18, "Panel"], Top] & /@ 
   {"ceF1", "ceF2", "ceF3"} // Row

enter image description here

$\endgroup$

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.