3
$\begingroup$

I would like to use the same box width for two different plot. consider following example. How can I use the same box width for the second plot?

data = Table[RandomVariate[NormalDistribution[\[Mu], 1], 
    100], {\[Mu], {0, 3, 2, 5}}];

Multicolumn[{BoxWhiskerChart[data, AspectRatio -> 3], 
  BoxWhiskerChart[data[[;; 2]], AspectRatio -> 3]}]

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

Update: You can use a custom ChartElementFunction that takes bar widths as input:

ClearAll[cEF]
cEF[f_: "BoxWhisker", w_: .5] := {ChartElementData[f][{Mean @ #[[1]] +
    {-w, w}/2, #[[2]]}, ##2]} &

Examples:

SeedRandom[1]
data = Table[RandomVariate[NormalDistribution[μ, 1], 100], {μ, {0, 3, 2, 5}}];

Row[BoxWhiskerChart[data[[#]], ChartLabels -> #, 
    ChartElementFunction -> cEF["BoxWhisker", .5 (Length[#] + .5)/(Length[data] + .5)], 
    AspectRatio -> 3, ImageSize -> Medium, ChartStyle -> (ColorData[97] /@ #), 
    PlotRange -> {{0, Length[#] + .5}, MinMax[data]}] & /@ 
 {{1, 2, 3, 4}, {2}, {1, 3}, {1, 2, 4}}]

enter image description here

Row[BoxWhiskerChart[data[[#]], ChartLabels -> #, 
   ChartElementFunction -> cEF["GlassBoxWhisker", .9 (Length[#] + .5)/(Length[data] + .5)],
   AspectRatio -> 3, ImageSize -> Medium, 
   ChartStyle -> (ColorData[63, "ColorList"][[#]]), 
   PlotRange -> {{0, Length[#] + .5}, MinMax[data]}] & /@
  {{1, 2, 3, 4}, {2}, {1, 3}, {1, 2, 4}}]

enter image description here

Original answer:

Multicolumn[{BoxWhiskerChart[data, AspectRatio -> 3, 
   ImageSize -> Medium, PlotRange -> {-3, 8}], 
  BoxWhiskerChart[Join[{{}}, data[[;; 2]], {{}}], AspectRatio -> 3, 
   ImageSize -> Medium, PlotRange -> {-3, 8}]}]

enter image description here

$\endgroup$
4
  • $\begingroup$ your solution does not work if I use data[[;; 1]] $\endgroup$ Dec 13, 2018 at 14:45
  • $\begingroup$ @OkkesDulgerci, please see the update. $\endgroup$
    – kglr
    Dec 13, 2018 at 21:57
  • $\begingroup$ Now this is elegant!! $\endgroup$ Dec 13, 2018 at 22:45
  • $\begingroup$ @kglr Hi Kglr is the default for the whisker q1+1.5 Iqr and Q3+1.5 Iqr? How can I change the whisker limits only keeping the box the same i.e. say box with the same Q3 and Q1, and whisker with Min Val and Max value or 95% cI from standard deviation? I have looked at your other post but there you change both the box length and whiskers. I want only whisker chagned. $\endgroup$
    – Rupesh
    Aug 22, 2022 at 17:30

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.