Menu

Stacked bar plots for negative value datasets

pbwild2003
2010-09-17
2012-09-07
  • pbwild2003

    pbwild2003 - 2010-09-17

    Hi,

    is it possible to display negative datasets in a stacked bar plot?

    eg

    $data2 = array(

    array('Mean Vario720 m/s',-10,-2),

    array('Vario720 m/s',-20,2),

    array('GPS Ht.Loss m', -30,8),

    array('Baro.Ht.Loss m', -4,0)

    );

    thanks,

    Peter

     
  • lbayuk

    lbayuk - 2010-09-17

    No, stacked bar plot doesn't support negative values. (In fact it takes the
    absolute value of each value to avoid negative numbers.)

    Since it is adding up the Y values for each X, I don't think having negative
    values makes any sense. But I'm open to being convinced otherwise.

    I suppose a stacked bar plot with all negative values would make sense, but
    your data has a mixture of negative and positive values.

     
  • pbwild2003

    pbwild2003 - 2010-09-18

    Hi,

    what I'm really looking for is a way to display discrete datasets in bar-graph
    form with an indication of the standard deviation of the dataset. I've got
    some good looking results by (mis) using stacked bar display but now need to
    account for some negative sets. It isn't a problem to make the set completely
    negative.

    Or can you suggest a different approach? Using a linepoint plot with error
    bars is ok, but a bar graph display looks so much better.

    Thanks,

    Peter

     
  • lbayuk

    lbayuk - 2010-09-18

    Let me take a look at stackedbar drawing to see if it can work without taking
    the absolute values. The results won't always make sense if positive and
    negative values are mixed, but as long as the results are consistent and
    explainable, I don't see why it needs to do abs().

     
  • lbayuk

    lbayuk - 2010-09-21

    Not as easy as I thought, but it does seem to be doable. The first non-zero value in a row will set the direction, up or down, for the rest of the bar. With all positive values, it looks the same as now (of course). With all negative values, the bar extends downward from the X axis with the first segment at the top. The labels and shading adjust for downward bars.

    Mixing positive and negative values in a row is not recommended. It keeps the running total, but doesn't draw any segments in the wrong direction. The results are consistent and I think correct, but probably not useful.

    Move the X axis and it properly clips bars that go in that direction, but it stretches bars that go in the other direction which looks odd. But I can't see a better way.

    I still have to do the same fixes for horizontal stacked bars.

    Sample with explanation can be found here: http://phplot.sourceforge.net/samples/negative_stacked_bars.php)

    (The code shown requires a version of PHPlot which isn't even in CVS yet.)

     

    Last edit: lbayuk 2012-12-11
  • pbwild2003

    pbwild2003 - 2010-09-22

    The documentation looks good - let me know if you need a tester for the new
    code!

    Cheers,

    Peter

     
  • lbayuk

    lbayuk - 2010-09-22

    I think I've got it all working. Some more testing, and I'll put it into CVS
    and you will be able to try it out if you want.

    I also opened a feature request for this. https://sourceforge.net/tracker/?func=detail&aid=3073679&group_id=14653&atid=364653

    (Because I like to have a bug report or feature request number to reference at release time.)

     

    Last edit: lbayuk 2012-12-11
  • lbayuk

    lbayuk - 2010-09-27

    Sorry, there was a delay while I fixed a problem with axis positions that came
    up while testing this change to stacked bar plots. It only affects horizontal
    plots, and was a simple change but required a number of updates to the
    reference manual. That's done now.

    I just committed the change to CVS for negative values in stacked bar plots.

    If you want to try it now, you can pull it right off the CVS web site under
    Code, CVS Browse, select phplot, phplot.php and click download next to the
    lastest version. Otherwise, if you want to wait for the next release, it won't
    be too long. There is just one change in progress (to improve color handling).
    It is already coded and just needs to be brought in sync with the latest
    changes and retested, then it will likely be release time.

     
  • pbwild2003

    pbwild2003 - 2010-09-27

    Great - thanks a lot! that's really helped a lot.

    Is there an easy way to make alternating columns in a stacked bar plot get
    plotted with two sets of different colours?

    Thanks,

    Peter

     
  • lbayuk

    lbayuk - 2010-09-27

    You can control the colors within each stack using SetDataColors(). Normally, the corresponding segments in each stack have the same color. If you want more
    control than that, you can use the Data Colors Callback. This is described here: http://phplot.sourceforge.net/phplotdocs/conc-colors-datacolor-callback.html with an example here: http://phplot.sourceforge.net/phplotdocs/ex-colorcallbackbars.html

    If I understand what you want to do, you would use SetDataColors() to define 2 * N colors, then your data colors callback just returns (N * ($row % 2) + $col). Here N is the number of colors in each set, or the maximum number of segments in each bar.

     

    Last edit: lbayuk 2012-12-11

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.