4
$\begingroup$

I am trying to create a diagram like this: enter image description here

But I am struggling to do so, I have tried the following

data1 = Table[i, {i, 0, 10, 1}]
data2 = Table[i, {i, 0, 20, 2}]
BoxWhiskerChart[{data1, data2}]
BoxWhiskerChart[{data1, data2}, 
 Epilog -> {PointSize[Medium], Point[data1]}]

But the get the following results

enter image description here

$\endgroup$
5
$\begingroup$

Epilog take points as {x0,y0} not the one you provided {y0,y1,...}

BoxWhiskerChart[{data1, data2}, Epilog -> {Red, PointSize[Medium], 
   Point[Table[{i, i}, {i, 0, 20, 1}]]}]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ I cannot produce your plot with MMA 11.3 $\endgroup$ – OkkesDulgerci Apr 19 '18 at 12:44
4
$\begingroup$

You can also use a custom ChartElementFunction that combines the built-in functions ChartElementData["BoxWhisker"] and ChartElementData["PointDensity"] to add points:

ClearAll[cef]
cef = {ChartElementData["BoxWhisker"][##], EdgeForm[], FaceForm[None], 
    ChartElementData["PointDensity", 
      "PointStyle" ->  Directive[EdgeForm[Black], PointSize[Large], 
        Darker[Charting`ChartStyleInformation["Color"]]]][##]} &;

Using this with the example data in OP:

BoxWhiskerChart[{data1, data2}, ChartStyle -> "Pastel", ChartElementFunction -> cef]

enter image description here

If you want to put the points at the centers of the boxes, you can use

ClearAll[cef2]
cef2 = {ChartElementData["BoxWhisker"][##], 
         PointSize[Large], Darker[Charting`ChartStyleInformation["Color"]], 
         Point[Thread[{Mean[#[[1]]], #2}]]} & ;

BoxWhiskerChart[{data1, data2}, ChartStyle -> "Pastel", ChartElementFunction -> cef2]

enter image description here

Alternatively, you can combine ChartElementFunctions to render data points as lines:

ClearAll[cef3]
cef3[t_ : .15] = {ChartElementData["BoxWhisker"][##], EdgeForm[], 
        FaceForm[None], Thick, Darker[Charting`ChartStyleInformation["Color"]] ,
        ChartElementData["LineDensity"][{#[[1]] + {t, -t}, #[[2]]}, ##2]} &;

BoxWhiskerChart[{data1, data2}, ChartStyle -> "Pastel", ChartElementFunction -> cef3[]] 

enter image description here

$\endgroup$
3
$\begingroup$

Numbers 0.724 and 1.724 are adhoc

{data1, data2} = 
  Table[RandomVariate[NormalDistribution[μ, 1], 
    50], {μ, {0, 3}}];
BoxWhiskerChart[{data1, data2}, 
 Epilog -> {Red, PointSize[Medium], 
   Point /@ {Transpose@{ConstantArray[0.724, Length@data1], data1}, 
     Transpose@{ConstantArray[1.724, Length@data2], data2}}}]

enter image description here

Edit

You can add some noise to x coordinate.

{data1, data2} = 
  Table[RandomVariate[NormalDistribution[\[Mu], 1], 
    40], {\[Mu], {0, 3}}];
BoxWhiskerChart[{data1, data2}, 
 Epilog -> {Red, PointSize[Medium], 
   Point /@ {Transpose@{ConstantArray[0.724, Length@data1] + 
        RandomReal[{-0.05, 0.05}, Length@data1], data1}, 
     Transpose@{ConstantArray[1.724, Length@data2] + 
        RandomReal[{-0.08, 0.08}, Length@data2], data2}}}]

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.