1
$\begingroup$

I want to plot the means of several data sets, but instead of using a point I would like to use a box-and-whisker plot to represent the full data set. Here is what I mean (with some toy data):

a = {1.7610325845854935, 1.4482381483503182, 1.262594767165387, 2.2720460660207409};
b = {2.1844741151332943, 3.5175853047673318, 3.5245233201347768, 2.0819498353445609};
c = {1.3755036246747516, 1.6169073661736904, 2.3472569940481094, 2.9457474653314475};
dat = {a, b, c};
ListPlot[Table[Mean[dat[[i]]], {i, Length[dat]}], 
    PlotStyle -> PointSize -> Large, PlotRange -> Full]

I would like to set the PlotMarkers in a way that the first marker is a box-and-whisker of a, the second a box-and-whisker plot of b, and so on.

Thank you in advance for any help you can offer!

$\endgroup$
1
  • 1
    $\begingroup$ Like this: BoxWhiskerChart[dat, {"Mean", {"MedianMarker", None}}, Prolog -> {ColorData[97, 1], Line[Transpose[{Range[3], Mean /@ dat}]]}]? $\endgroup$ Commented Jul 1, 2016 at 18:45

2 Answers 2

2
$\begingroup$

Using Show[] to combine your graph with the BoxWhiskerChart[] (update: incorporated comment)

a = {1.7610325845854935, 1.4482381483503182, 1.262594767165387, 2.2720460660207409};
b = {2.1844741151332943, 3.5175853047673318, 3.5245233201347768, 2.0819498353445609};
c = {1.3755036246747516, 1.6169073661736904, 2.3472569940481094, 2.9457474653314475};
dat = {a, b, c};

Show[
 BoxWhiskerChart[dat, {"Mean", {"MedianMarker", None}}],
 ListLinePlot[Mean /@ dat, PlotRange -> Full]
 ]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ ListLinePlot[Mean /@ dat, PlotRange -> Full] is sufficient. $\endgroup$ Commented Jul 2, 2016 at 0:41
7
$\begingroup$

Update 3: From the function Charting`iBoxWhiskerChart we get the full list of built-in settings for Joined:

Grid[{{Automatic | All | True | 
     "Median", {{Mean[#1[[1]]], Quantile[#2, 0.5`]} &}},
   {"Mean", {{Mean[#1[[1]]], Mean[#2]} &}},
   {"Max", {{Mean[#1[[1]]], Max[#2]} &}},
   {"Min", {{Mean[#1[[1]]], Min[#2]} &}},
   {"LowerQuartile", {{Mean[#1[[1]]], Quantile[#2, 0.25`]} &}},
   {"UpperQuartile", {{Mean[#1[[1]]], Quantile[#2, 0.75`]} &}},
   {None | False, {}}}, Dividers -> All] // 
 Style[#, 20, ShowStringCharacters -> True] &

enter image description here

Update 2: It also turns out that, in both BoxWhiskerChart and DistributionChart, the option Joined has a few built-in settings such as "Mean","Min", "Max", "Median" (in addition to arbitrary pure functions shown in the original answer.)

SeedRandom[1]
data = RandomVariate[LogNormalDistribution[1, .5], {10, 100}];

Row[BoxWhiskerChart[data, Joined -> #, 
      PlotLabel -> Style[Row[{"Joined  -> ", "\"", #, "\""}], 16, "Panel"], 
      ChartStyle -> "Pastel", ImageSize -> 250] /. l : Line[__] :> {#2, Thick, l} & @@@ 
   Transpose[{{"Min", "Mean", "Median", "Max"}, {Red, Green, Blue, Orange}}]]

enter image description here

It seems that only a single line can be produced using Joined. To get multiple lines, we can Show multiple charts with different Joined settings:

colors = {Red, Green, Blue, Orange};
i = 1; 
Show[BoxWhiskerChart[data, Joined -> #, 
     ChartBaseStyle -> {EdgeForm[Opacity[.1]], FaceForm[]}, 
     ChartStyle -> "Pastel"] & /@ {"Min", "Max", "Mean", "Median"}] /. 
 l_Line :> {colors[[i++]], Thick, l}

enter image description here

Using DistributionChart in place of BoxWhiskerChart above we get

enter image description here

and

enter image description here

Update: A pure function setting for Joined also works for BarChart. Such functions apply to the list of horizontal and vertical ranges {{xmin, xmax}, {ymin, ymax}} of the rectangles:

BarChart[{{3, 3, 4, 1}, 2 + {3, 3, 4, 1}}, Joined -> (Mean /@ # & )] /. 
 l : Line[x_ /; (x[[1, 2]] =!= 0.)] :> {Red, Thick, l}

enter image description here

BarChart[{{3, 3, 4, 1}, 2 + {3, 3, 4, 1}}, Joined -> (Max /@ # & )] /. 
 l : Line[x_ /; (x[[1, 2]] =!= 0.)] :> {Red, Thick, l}

enter image description here

Original Answer:

BoxWhiskerChart with the option Joined

It turns out that, in BoxWhiskerChart, functions can be used as the setting for the option Joined and the setting (Mean[#2] &) gives the desired result without having to use ListLinePlot:

BoxWhiskerChart[{a, b, c}, "Mean", Joined -> (Mean[#2] &), ChartStyle -> "Pastel"]

enter image description here

If desired, post-process to style the line

BoxWhiskerChart[{a, b, c}, "Mean", Joined -> (Mean[#2] &), ChartStyle -> "Pastel"] /. 
  l : Line[__] :> {Red, Thick, l}

enter image description here

Another example:

SeedRandom[1]
data = RandomVariate[NormalDistribution[], {10, 100}];

BoxWhiskerChart[data, "Mean", Joined -> (Mean[#2] &), ChartStyle -> "Pastel"] /. 
  l : Line[__] :> {Red, Thick, l}

enter image description here

DistributionChart with the option Joined

Joined works the same way in DistributionChart.

DistributionChart[{a, b, c},  Joined -> (Mean[#2] &), 
  ChartElementFunction -> "PointDensity", ChartStyle -> "Pastel"] /. 
 l : Line[__] :> {Red, Thick, l}

enter image description here

DistributionChart[data,  Joined -> (Mean[#2] &), 
  ChartElementFunction -> "PointDensity", ChartStyle -> "Pastel"] /. 
 l : Line[__] :> {Red, Thick, l}

enter image description here

$\endgroup$
3
  • $\begingroup$ How nice! How did you discover this? $\endgroup$ Commented Aug 21, 2017 at 2:54
  • 1
    $\begingroup$ @J.M. , pure luck. The docs mention only Automatic and True as the settings for Joined. I accidentally discovered that All also works. Tried Joined -> (Print[{##}] &) and noticed that it returns {{{xmin, xmax}, {ymin, ymax}}, data} indicating that Joined is passed this information. So, tried different functions Median[#2]&, Quantile[#2, .9]& , Mean[#2]& and all worked. $\endgroup$
    – kglr
    Commented Aug 21, 2017 at 3:13
  • 2
    $\begingroup$ I wonder how many more gems can be uncovered just by sticking Print[{##}] & at appropriate positions... :) good work! $\endgroup$ Commented Aug 21, 2017 at 3:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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