4
$\begingroup$

I want to plot negative values using PairedBarChart in V9. However, with this code

PairedBarChart[{0.0000254914, -0.0000222568, 0.0756163,}, 
 {0.00043002, 0.0709322, 0.0681315}, PlotRange -> {{-0.002, 20}, All}]

I get:

PairedBarChart::ldata: "{0.0000254914,-0.0000222568,0.0756163} is not a valid dataset or list of datasets.

How can I plot negative values with PairedBarChart in V9?

Many thanks in advance Sofia

$\endgroup$
5
$\begingroup$

PairedBarChart >> Possible Issues:

PairedBarChart does not accept negative values:

A work-around is to combine two BarCharts using Row:

SeedRandom[0]
data = RandomInteger[{-5, 5}, {2, 10}];
opts = {ChartStyle -> 63, BarOrigin -> Left, AxesOrigin -> {0, 0}, ImageSize -> 500};

Panel[Row[BarChart[#, opts] & /@ data, Spacer[10]]]

enter image description here

Of course, we lose the convenience of some PairedBarChart options such as ChartLabels. To add labels we can use Epilog in the first chart and use appropriate image padding to get both charts with the same size:

labels = CharacterRange["A", "Z"][[;; 10]];
epilog = Inset[Style[#, "Panel", 16, Bold], #2] & @@@ 
               Transpose[{labels, Thread[{1.2 Max[data[[1]]], Range[10]}]}];

Panel[Row[{BarChart[data[[1]], opts, Epilog -> epilog, 
    ImagePadding -> {{0, 40}, {20, 0}}, 
    ChartElementFunction -> "GlassRectangle"], 
   BarChart[data[[2]], ImagePadding -> {{0, 40}, {20, 0}}, opts, 
    ChartElementFunction -> "GlassRectangle"]}, Spacer[5]]]

enter image description here

$\endgroup$
2
$\begingroup$

As pointed out by @kguler, PairedBarChart does not accept negative values. However, you can get the desired output by adding a constant offset to your data list so they are all non-negative and change the tick labels by Ticks->{..} like this:

data1 = {1, -2, 3}; data2 = {-2, 1, -3};

min = Min[data1, data2];
max = Max[data1, data2];

offset = -min + 1;

PairedBarChart[data1 + offset, data2 + offset, 
 Ticks -> {Table[{i, i - offset}, {i, 0, max + offset}]}]

output

This code may not be very robust and you'll have to adjust the offset/ticks as appropriate.

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