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$
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$
  • $\begingroup$ your solution does not work if I use data[[;; 1]] $\endgroup$ – OkkesDulgerci Dec 13 '18 at 14:45
  • $\begingroup$ @OkkesDulgerci, please see the update. $\endgroup$ – kglr Dec 13 '18 at 21:57
  • $\begingroup$ Now this is elegant!! $\endgroup$ – OkkesDulgerci Dec 13 '18 at 22:45

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.