Data visualization with SAS programming

Creating a stacked bar chart with continuous data?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 106
Accepted Solution

Creating a stacked bar chart with continuous data?

The horizontal axis is populated by categorical variable of the different buildings (i.e.b1, b2). I want the height of the bars to be the total performance measure (a continuous variable) of the buildings and then within each of these bars is the different measures that make up the total performance measure. For instance, if the height of the bar is 28 (the total), I want different demarcations for the 3 performance measures that make up that 28: speed: 10 (blue), accuracy 8 (green),  responsiveness 10 (yellow). So each building would have stacked bars with various heights and 3 different variables that make up that bar and color coded. The legend would show that blue = speed, accuracy= green and responsiveness=yellow.

My dataset looks like this:

Building      Speed     Accuracy     Responsiveness     Total

B1               10                8               10                         28

B2                8                 7               9                           24


Accepted Solutions
Solution
‎03-07-2013 10:48 AM
SAS Employee
Posts: 990

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

I think you'd have to hard-code an axis statement in Gchart.  There's a 'missing' option for the bar midpoints, but I don't think there's one for the response variable.

data building;

  input Building  Value;

  datalines;

1   22

2    0

3   44

4   .

5   .

6   0

;

run;

axis1 order=(1 to 6 by 1);

proc gchart data=building;

vbar Building / discrete type=sum sumvar=value

maxis=axis1;

run;

View solution in original post


All Replies
Super User
Posts: 21,572

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

Look at vbarparm and hbarparm.

Or see Rob Allisons graph:

http://robslink.com/SAS/democd7/aaaindex.htm

SAS Employee
Posts: 990

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

Here are the basics...

data foo;

input building $ 1-2 speed accuracy responsiveness;

datalines;

B1               10                8               10                       

B2                8                 7               9                         

;

run;

proc transpose data=foo out=foo (rename=(_name_=measure col1=value));

by building;

run;

proc gchart data=foo;

label measure='Measure';

vbar building / type=sum sumvar=value subgroup=measure;

run;

foo.png

Frequent Contributor
Posts: 106

Re: Creating a stacked bar chart with continuous data?

Posted in reply to RobertAllison_SAS

Thank you. However, when I run this code, I get a chart with funky Building ID midpoints. I should have mentioned that the building variable is numeric, but I want to use it like a character. There is a weird summing that occurs. How do I make sure Building ID shows up as 1 and not 30 for instance?

SAS Employee
Posts: 990

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

By default, gchart takes numeric bar values and splits them into ranges to make a histogram.

If you want each numeric bar value to be treated as a discrete value (like a character), you can specify the 'discrete' option.

vbar building / discrete type=sum sumvar=value subgroup=measure;


Frequent Contributor
Posts: 106

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

If I use the discrete option then the buildings that are missing data will not show up on the x axis. Can I convert the building ID to numeric in the transpose procedure?

SAS Employee
Posts: 990

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

It depends... By 'missing' do you mean they're not in the data at all, or the building number is a sas-missing, or that the building number is there but with sas-missing response values, or other?  I'd have to see some data to steer you in the right direction.  You can convert a numeric to a character either before or after the transpose (again - it depends...)

There's a "missing" option in gchart, you can specify all the desired bar values in an axis statement, etc.

Frequent Contributor
Posts: 106

Re: Creating a stacked bar chart with continuous data?

Posted in reply to RobertAllison_SAS

Ah thank you.  The missing option works. Many thanks!

If it is not uncouth, I have an additional question. If I want to use proc sgplot using this data and I have an additional subgroup, what is the subgroup equivalent in sgplot?

SAS Super FREQ
Posts: 978

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

For the multi-class situation, use SGPANEL. For example,

proc sgpanel data=sashelp.prdsale;

panelby year / onepanel novarname noborder layout=columnlattice;

vbar product / response=actual group=division;

run;

Frequent Contributor
Posts: 106

Re: Creating a stacked bar chart with continuous data?

Thanks DanH!

Frequent Contributor
Posts: 106

Re: Creating a stacked bar chart with continuous data?

Posted in reply to RobertAllison_SAS

I want to plot the following data on a bar chart, where building ID is on the x-axis and value is the height of the bar. If the value is 0 or .  I want the building id to show up on the x axis. I am using the missing option, but the building ids where value = . do not show up. There is a difference between having a value of 0 and a value that is missing.

building      value

1                    22

2                    0

3                  44

4                   .

5                    .

6                     0

SAS Super FREQ
Posts: 1,227

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

This graph created using SAS 9.3, but it works fine on SAS 9.2 (remove the DATASKIN option).

Building.png

data building;
  input Building  Value;
  datalines;
1   22
2    0
3   44
4   .
5   .
6   0
;
run;

ods html close;

ods graphics / reset width=5in height=3in imagename='Building';
title 'Value by Building';
proc sgplot data=building;
  vbar building / response=value missing nostatlabel dataskin=gloss;
  run;

Frequent Contributor
Posts: 106

Re: Creating a stacked bar chart with continuous data?

Posted in reply to Sanjay_SAS

Sanjay, is there a gchart equivalent?

Solution
‎03-07-2013 10:48 AM
SAS Employee
Posts: 990

Re: Creating a stacked bar chart with continuous data?

Posted in reply to sarahsasuser

I think you'd have to hard-code an axis statement in Gchart.  There's a 'missing' option for the bar midpoints, but I don't think there's one for the response variable.

data building;

  input Building  Value;

  datalines;

1   22

2    0

3   44

4   .

5   .

6   0

;

run;

axis1 order=(1 to 6 by 1);

proc gchart data=building;

vbar Building / discrete type=sum sumvar=value

maxis=axis1;

run;

Frequent Contributor
Posts: 106

Re: Creating a stacked bar chart with continuous data?

Posted in reply to RobertAllison_SAS

Yes, this is what I ultimately ended up doing. Thanks!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 1806 views
  • 4 likes
  • 6 in conversation