Error bars on bar graphs

You were kind enough to help with this problem by supplying the code below.   I would like to add a third bar to each, something like seattle = {47, 70, 82}; phoenix = {68,105, 112} and so on.  How do I change this program to make that work?  It seems like it should be an easy change, but it only ever seems to want to plot 6 error bars - I can't make it do 9.  Thanks

// Create 2x1 numeric vectors

seattle = { 47, 70 };

phoenix = { 68, 105 };

denver = { 32, 85 };

// Create 2x1 string labels
labels = "January" $| "June";

// Declare 'myPlot' to be a plotControl struct
// and fill with defaults settings for bar plots
struct plotControl myPlot;
myPlot = plotGetDefaults("bar");

// Create legend
leg_text = "Phoenix" $| "Seattle" $| "Denver" $| "";
plotSetLegend(&myPlot, leg_text, "top left inside");

// Set legend border to zero pixels wide
plotSetLegendBorder(&myPlot, "gray", 0);

// Set Fill to two different fill patterns
// and 100% opacity
plotSetFill(&myPlot, 12|13, 1);

// Use HTML to create the degrees symbol
plotSetYLabel(&myPlot, "Temp °F");

// Concatenate temperature vectors
// into a 2x2 matrix
heights = phoenix ~ seattle ~ denver;

// Draw the graph
plotBar(myPlot, labels, heights);

se = { 5 8,  
      8 10,
     30 15 };

plotAddBoxErrorBars(labels, heights, se);

proc (0) = plotAddBoxErrorBars(labels, heights, se);
    local box_xs;

    box_xs = calcBoxCenters(rows(labels), cols(heights));

    plotAddErrorBar(box_xs, vec(heights), vecr(se));
endp;

proc (1) = calcBoxCenters(nlabels, nboxes);
    local box_width, idx, x;

    box_width = 0.5;

    x = seqa(1, 1, nlabels);

    idx = seqa(x[1]-box_width/2+box_width/(nboxes*2), 2*box_width/(nboxes*2), nboxes);

    retp(vecr(idx+(x-1)'));  
endp;

1 Answer



0



To add a third set of bars to the code you posted, you will have to:

  1. Add a third "temperature" to "seattle", "phoenix" and "denver".
  2. Add a third "month" to "labels".
  3. Add a third column to the standard error matrix, "se".
new;
cls;

/*
** Change
*/
// Create 3x1 numeric vectors
seattle = { 47, 51, 70 };
phoenix = { 68, 95, 105 };
denver = { 32, 77, 85 };

/*
** Change
*/
// Create 3x1 string labels
labels = "January" $| "May" $| "June";

// Declare 'myPlot' to be a plotControl struct
// and fill with defaults settings for bar plots
struct plotControl myPlot;
myPlot = plotGetDefaults("bar");

// Create legend
leg_text = "Phoenix" $| "Seattle" $| "Denver" $| "";
plotSetLegend(&myPlot, leg_text, "top left inside");

// Set legend border to zero pixels wide
plotSetLegendBorder(&myPlot, "gray", 0);

// Set Fill to two different fill patterns
// and 100% opacity
plotSetFill(&myPlot, 12|13, 1);

// Use HTML to create the degrees symbol
plotSetYLabel(&myPlot, "Temp °F");

// Concatenate temperature vectors
// into a 2x2 matrix
heights = phoenix ~ seattle ~ denver;

// Draw the graph
plotBar(myPlot, labels, heights);

/*
** Change adding a third column to SE
*/
se = { 5 8 20,
      8 10 14,
     30 15 19 };

plotAddBoxErrorBars(labels, heights, se);

proc (0) = plotAddBoxErrorBars(labels, heights, se);
    local box_xs;

    box_xs = calcBoxCenters(rows(labels), cols(heights));

    plotAddErrorBar(box_xs, vec(heights), vecr(se));    
endp;

proc (1) = calcBoxCenters(nlabels, nboxes);
    local box_width, idx, x;

    box_width = 0.5;
    x = seqa(1, 1, nlabels);
    idx = seqa(x[1]-box_width/2+box_width/(nboxes*2), 2*box_width/(nboxes*2), nboxes);

    retp(vecr(idx+(x-1)'));
endp;

aptech

1,773

Your Answer

1 Answer

0

To add a third set of bars to the code you posted, you will have to:

  1. Add a third "temperature" to "seattle", "phoenix" and "denver".
  2. Add a third "month" to "labels".
  3. Add a third column to the standard error matrix, "se".
new;
cls;

/*
** Change
*/
// Create 3x1 numeric vectors
seattle = { 47, 51, 70 };
phoenix = { 68, 95, 105 };
denver = { 32, 77, 85 };

/*
** Change
*/
// Create 3x1 string labels
labels = "January" $| "May" $| "June";

// Declare 'myPlot' to be a plotControl struct
// and fill with defaults settings for bar plots
struct plotControl myPlot;
myPlot = plotGetDefaults("bar");

// Create legend
leg_text = "Phoenix" $| "Seattle" $| "Denver" $| "";
plotSetLegend(&myPlot, leg_text, "top left inside");

// Set legend border to zero pixels wide
plotSetLegendBorder(&myPlot, "gray", 0);

// Set Fill to two different fill patterns
// and 100% opacity
plotSetFill(&myPlot, 12|13, 1);

// Use HTML to create the degrees symbol
plotSetYLabel(&myPlot, "Temp °F");

// Concatenate temperature vectors
// into a 2x2 matrix
heights = phoenix ~ seattle ~ denver;

// Draw the graph
plotBar(myPlot, labels, heights);

/*
** Change adding a third column to SE
*/
se = { 5 8 20,
      8 10 14,
     30 15 19 };

plotAddBoxErrorBars(labels, heights, se);

proc (0) = plotAddBoxErrorBars(labels, heights, se);
    local box_xs;

    box_xs = calcBoxCenters(rows(labels), cols(heights));

    plotAddErrorBar(box_xs, vec(heights), vecr(se));    
endp;

proc (1) = calcBoxCenters(nlabels, nboxes);
    local box_width, idx, x;

    box_width = 0.5;
    x = seqa(1, 1, nlabels);
    idx = seqa(x[1]-box_width/2+box_width/(nboxes*2), 2*box_width/(nboxes*2), nboxes);

    retp(vecr(idx+(x-1)'));
endp;


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.