3
$\begingroup$

I have two sets data as following:

data1 = 
  {11, 11, 24, 19, 16, 16, 15, 15, 15, 16, 15, 16, 16, 19, 23, 36,
   12, 11, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 11, 13, 13, 18};

data2 = 
  {15, 10, 13, 6, 4, 4, 3, 3, 3, 4, 3, 4, 4, 7, 11, 26, 4, 4, 7, 5, 
   6, 5, 6, 5, 6, 5, 6, 4, 5, 4, 3, 5};

I want to put them together, like in this example picture.

Compare two sets of data, (ignore Lables in this picture).

It seems that the Stacked option doesn't work well.

$\endgroup$
  • 1
    $\begingroup$ Have you tried Show[BarChart[data1, ChartStyle -> Directive[Red, Opacity[0.7]]], BarChart[data2, ChartStyle -> Directive[Blue, Opacity[0.7]]]] ? $\endgroup$ – rotom407 Jun 5 '17 at 6:20
3
$\begingroup$

In addition to using Show as suggested by rotom407, which would give a chart looking like

Show[
 BarChart[data1, ChartStyle -> Directive[Red, Opacity[.75]]], 
 BarChart[data2, ChartStyle -> Directive[Blue, Opacity[.5]]]]

show

I think you should consider two other charting styles, both of which have the advantages of better color and showing all bars fully.

Transposed stacking

BarChart[Transpose[{data1, data2}], 
  ChartStyle -> {Red, Blue}, ChartLayout -> "Stacked"]

stack

Paired with common x-axis

PairedBarChart[Style[data1, Red], Style[data2, Blue], BarOrigin -> "XAxis"]

paired

Update

This update addresses the question raised by OP in a comment below which concerns adding a legend to the paired bar chart.

ChartLegends doesn't permit the user to specify colors for the legend swatches, so I suggest using Legended.

PairedBarChart[
  Legended[Style[data1, Red], "data1"],
  Legended[Style[data2, Blue], "data2"],
  BarOrigin -> "XAxis"]

legenedenter image description here

$\endgroup$
  • $\begingroup$ BTW, how to add the ChartLegends as example picture. When I use ChartLegends -> {"data1", "data2"}, the color pieces always yellow. $\endgroup$ – Wang Shen Jun 6 '17 at 6:32
  • $\begingroup$ @WangShen. Which of the kinds of chart I discussed in this answer are you using? $\endgroup$ – m_goldberg Jun 6 '17 at 6:59
  • $\begingroup$ The 3rd chart, PairedBarChart. $\endgroup$ – Wang Shen Jun 6 '17 at 7:32
  • $\begingroup$ For the legend, one can also use ChartLegends -> SwatchLegend[{color1,color2}, {"label1", "label2"}] $\endgroup$ – Ben Kalziqi Jun 7 '17 at 6:06
2
$\begingroup$

Actually I suspect that you should be using Histogram not BarChart.The image you have posted is a histogram. The simplest implementation is

Histogram[{data1, data2}]

however your example data sets are likely to be the bin counts rather than the raw data. From the docs:

enter image description here

$\endgroup$
0
$\begingroup$
PairedBarChart[data1, data2, BarOrigin -> "XAxis", 
 ChartStyle -> {{Red, Blue}, None, None}, 
 ChartLegends -> {{"data1", "data2"}, None, None}]

Mathematica graphics

$\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.