# Using a BoxWhiskerChart as a plot marker in ListPlot

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.

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

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]
]


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

Update 3: From the function ChartingiBoxWhiskerChart 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] &


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}}]]


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}


Using DistributionChart in place of BoxWhiskerChart above we get

and

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}


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


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"]


If desired, post-process to style the line

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


Another example:

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

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


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}


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


• How nice! How did you discover this? Commented Aug 21, 2017 at 2:54
• @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.
– kglr
Commented Aug 21, 2017 at 3:13
• I wonder how many more gems can be uncovered just by sticking Print[{##}] & at appropriate positions... :) good work! Commented Aug 21, 2017 at 3:38