1
$\begingroup$

I have the bar chart below:

enter image description here

My code:

BarChart[{Labeled[{a760, c760, b760, d760}, "760" ], 
  Labeled[{a780, c780, b780, d780}, "780" ]}, 
 ChartStyle -> {Blue, Red, Green, Black}, 
 ChartLegends -> 
  Placed[{"65.5 ps", "274 ps", "16.8 ps", "2040 ps" }, {Right}], 
 BarSpacing -> {0, 1}]

The third entry of 780 is missing, so I used Missing[]. I want to attach the forth entry

of 780 to the second entry of 780. The point is that the color of the forth entry must

remain black because each color indicates a quality such as lifetimes.

$\endgroup$
  • $\begingroup$ When you say "attach" do you mean "stack"? That is, to put the black bar on top of the red bar? Or below? Or what does attach mean in this case? $\endgroup$ – C. E. Mar 26 '14 at 13:45
  • $\begingroup$ I mean put the dark one alongside of the red one. I want them side to side. $\endgroup$ – MOON Mar 26 '14 at 15:31
  • 2
    $\begingroup$ Please include the data, if possible so we can reproduce your graph and inspect it. $\endgroup$ – DavidC Mar 26 '14 at 16:52
4
$\begingroup$

I can't think of any clever way to remove the gap, so here is a way to get the chart using Style to specify individual colours for the bars:

data1 = {10, 5, 20, 2};
data2 = {12, 8, Missing[], 3};

colors = {Blue, Red, Green, Black};

applycolors = Style @@@ DeleteCases[Thread @ {#, colors}, {Missing[], _}] &;

BarChart[{
   Labeled[applycolors @ data1, "760"],
   Labeled[applycolors @ data2, "780"]},
  BarSpacing -> {0, 1}] ~Legended~
 SwatchLegend[colors, {"65.5 ps", "274 ps", "16.8 ps", "2040 ps"}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you. Your answer is exactly what I needed. $\endgroup$ – MOON Mar 27 '14 at 0:44
0
$\begingroup$

You can transform the data to associate with each data point the number of Missing[] data to the left of that point as metadata, and using the metadata information to translate the rectangles to the left by an appropriate amount:

ClearAll[processF, ceF]
prepF = Function[{d}, MapIndexed[# -> Count[d[[;; #2[[1]]]], _Missing] &, d]];
ceF = Module[{box = {#[[1]] - #3[[1]], #[[2]]}},
        ChartElementDataFunction["Rectangle"][box, ##2]] &;

Examples:

Simon's example:

data1 = {10, 5, 20, 2};
data2 = {12, 8, Missing[], 3};
colors = {Blue, Red, Green, Black};
newdata = prepF /@ {data1, data2};
bc1 = BarChart[newdata, ChartStyle -> colors, ImageSize -> 400, BarSpacing-> {0, 1}];
bc2 = BarChart[newdata, ChartStyle -> colors, ImageSize -> 400, BarSpacing-> {0, 1},  
   ChartElementFunction -> ceF];
Column[{bc1, bc2}]

enter image description here

Another example:

data1 = {10, 5, Missing[], 20, 2, Missing[], 4, 5};
data2 = {12, 8, Missing[], Missing[], 3, 4, Missing[], 5};

newdata = prepF /@ {data1, data2};

bc1 = BarChart[newdata, ChartStyle -> 1, ImageSize -> 400, BarSpacing-> {0, 1}];
bc2 = BarChart[newdata, ChartStyle -> 1, ImageSize -> 400, BarSpacing-> {0, 1},
  ChartElementFunction -> ceF];

Column[{bc1, bc2}]

enter image description here

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