Desktop productivity for business analysts and programmers

How to specify different color bars using vbar with proc sgplot?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to specify different color bars using vbar with proc sgplot?

Hello all,

 

I'd like to specfiy different colors for a barchart based on values above and below zero. For example red for greater than zero, green for less than zero, and yellow for zero. I'm relatively new to SAS and using SAS graphics so this may be a rudimentary question.

 

Here is the code I've used so to produce a chart with uniform color:

 

ods graphics / reset border=off width=10in height=8in;

 

proc sgplot data=mydata;

     vbar name / response=percent

          categoryorder=respdesc datalabel datalabelattrs=(size=10pt)

          dataskin=crisp barwidth=0.6 fillattrs=(color=CornFlowerBlue);

     title h=16pt bold color=black "Crowding by Name";

     yaxis label="% Over/Under" labelattrs=(size=11pt)

          values=(-100 to 100 by 5) grid;

     xaxis label="Name";

          xaxis display=(nolabel noline)

               fitpolicy=rotate valuesrotate=vertical valueattrs=(size=11pt); 

run;

title;


Accepted Solutions
Solution
‎08-03-2017 02:34 PM
Super User
Posts: 11,343

Re: How to specify different color bars using vbar with proc sgplot?

I believe you are asking for a "range attribute" data set. This is an auxiliary data set referenced by the rattrmap option on the proc statement that contains information. Here is an example using the SASHELP.CLASS data set.

 

data clrresp;
retain id "myid";
length min $ 5 max $ 5;
input min $ max $ color $ altcolor $ colormodel1 $ colormodel2 $ colormodel3 $;
datalines;
_min_ 90    purple purple .    .      .
90    100   gold   gold   .    .      .
100   _max_ .      .      red  orange yellow
;
run;

proc sgplot data=sashelp.class rattrmap=clrresp;
  vbar age / colorresponse=weight rattrid=myid 
    response=weight stat=mean colorstat=mean;
run;

View solution in original post


All Replies
Solution
‎08-03-2017 02:34 PM
Super User
Posts: 11,343

Re: How to specify different color bars using vbar with proc sgplot?

I believe you are asking for a "range attribute" data set. This is an auxiliary data set referenced by the rattrmap option on the proc statement that contains information. Here is an example using the SASHELP.CLASS data set.

 

data clrresp;
retain id "myid";
length min $ 5 max $ 5;
input min $ max $ color $ altcolor $ colormodel1 $ colormodel2 $ colormodel3 $;
datalines;
_min_ 90    purple purple .    .      .
90    100   gold   gold   .    .      .
100   _max_ .      .      red  orange yellow
;
run;

proc sgplot data=sashelp.class rattrmap=clrresp;
  vbar age / colorresponse=weight rattrid=myid 
    response=weight stat=mean colorstat=mean;
run;
Occasional Contributor
Posts: 6

Re: How to specify different color bars using vbar with proc sgplot?

Thanks! Works like a charm.
Occasional Contributor
Posts: 6

Re: How to specify different color bars using vbar with proc sgplot?

 

Here's the successful solution:

 

 

data bardata;

retain id "myid";

length min $ 5 max $ 5;

input min $ max $ color $altcolor $ colormodel1 $ colormodel2 $ colormodel3 $;

datalines;

_min_ 0 green . . . .

0 0 gold gold . . .

1 _max_ red . . . .

;

run;

title h=16pt bold color=black "Crowding";

proc sgplot data=mydata rattrmap=bardata;

vbar name / colorresponse=rcp rattrid=myid

response=rcp categoryorder=respdesc datalabel datalabelattrs=(size=10pt)

dataskin=crisp barwidth=0.6

stat=mean colorstat=mean;

yaxis label="% Over/Under" labelattrs=(size=11pt)

values=(-100 to 100 by 5) grid;

xaxis label="Name";

xaxis display=(nolabel noline)

fitpolicy=rotate valuesrotate=vertical valueattrs=(size=11pt);

run;

title;

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 165 views
  • 1 like
  • 2 in conversation