Data visualization with SAS programming

How to change colors on bars?

Reply
Occasional Contributor
Posts: 6

How to change colors on bars?

Hi again. More questions regarding proc template.

I want to have diffrent colors on the bars depending on the values. For instance, I want color red on "negative" bars and color blue on "positive" bars. Right now I have the same colors for both bars:

graph_avvik.jpg

0-17 bar should be red same with 26-35.

18-25 bar should be blue.

Any help is appriciated.

Thanks.

best regards

Johan

Here is my code:

data test;

  input CU_AGE $ MALGRUPPE REFERANSE DIFF;

  datalines;

0-17 0.1 0.3 -0.6

18-25 0.2 0.1 0.2

26-35 0.4 0.6 -0.3

;

proc template;

define statgraph profile_age;

dynamic _NEGATIVE_;

dynamic _ticklist_;

begingraph /;

entrytitle "";

layout overlay / xaxisopts=( griddisplay=on Label="% av målgruppe" /*offsetmin=0*/ type=linear ) yaxisopts=( reverse=true display=( ticks tickvalues line ) type=discrete ) y2axisopts=(reverse=true);

   if (_NEGATIVE_)   ReferenceLine x=0 / lineattrs=GraphAxisLines;

   endif;

   barchart X='CU_AGE'n Y=DIFF / primary=true orient=horizontal LegendLabel="Aldersgruppe" NAME="a" dataskin=PRESSED  target=REFERANSE barlabel=true barwidth=0.1 BASELINEINTERCEPT=0;

   /*ScatterPlot X='TARGET'n Y='CU_GENDER'n / discreteOffset=-0.35 Markerattrs=( Symbol=TRIANGLEDOWNFILLED Size=10) DataTransparency=0.4 LegendLabel="normale population" NAME="t";*/

   DiscreteLegend "a" "t" / Location=outside Title="";

endlayout;

endgraph;

end;

run;

ods graphics on / height=500px width=600px;

proc sgrender data=work.test template=profile_age;

format DIFF  percent12.1;

run;

ods graphics off;

SAS Super FREQ
Posts: 1,044

Re: How to change colors on bars?

Easiest way would be to compute a classifier column that has two values based on DIFF.  Then you can assign that as the GROUP role for the bar chart.  Alternatively, you could use the IFC or IFN function to assign such a value directly to the group role.  GROUP=ifc(diff >=0, 'Positive', 'Negative'); 

Then, use the appropriate colors you need in the style colors. With SAS 9.4, you can use the DATACOLOR option on the BEGINGRAPH statement.   To ensure Positive bars are always blue, use the DiscreteAttrMap.

Occasional Contributor
Posts: 6

Re: How to change colors on bars?

Hi

We have only SAS 9.3. Is it still possible?

I have now this dataset:

graph_avvik_2.jpg

what is my next step?

SAS Super FREQ
Posts: 1,044

Re: How to change colors on bars?

Assign the GROUP column to the GROUP role in the bar chart.  You will get the first two colors from the current style, based on which valued is first in the data.  To ensure Positive is always Blue, use the DiscreteAttrMap.  This is the right way to ensure correct color assignment.

Alternatively, add two dummy observations to the top of your data, with missing CU_AGE, first one having "Positive" and second one having "Negative" values for the GROUP variable.  This will ensure the first group color (generally blue) will be assigned to "Positive", and second group color (generally light red) will be assigned to "Negative".

Occasional Contributor
Posts: 6

Re: How to change colors on bars?

Thank you very much Smiley Happy sir

Occasional Contributor
Posts: 6

Re: How to change colors on bars?

Thank you it works Smiley Happy

Here is the code for those who wants them Smiley Happy

data test;

  input CU_AGE $ MALGRUPPE REFERANSE DIFF;

  datalines;

0-17 0.1 0.3 -0.6

18-25 0.2 0.1 0.2

26-35 0.4 0.6 -0.3

;

data test;

  set test;

  GROUP=ifc(DIFF>=0, 'Positive', 'Negative');

run;

proc template;

define statgraph profile_age;

dynamic _NEGATIVE_;

dynamic _ticklist_;

begingraph /;

entrytitle "";

discreteattrmap name='colors' / ignorecase=true;

         value "Positive" / fillattrs=(color=blue);

         value "Negative" / fillattrs=(color=red);

      enddiscreteattrmap;

discreteattrvar attrvar=barcolors var=group attrmap='colors';

layout overlay / xaxisopts=( griddisplay=on Label="% av målgruppe" /*offsetmin=0*/ type=linear ) yaxisopts=( reverse=true display=( ticks tickvalues line ) type=discrete ) y2axisopts=(reverse=true);

   if (_NEGATIVE_)   ReferenceLine x=0 / lineattrs=GraphAxisLines;

   endif;

   barchart X='CU_AGE'n Y=DIFF / primary=true orient=horizontal LegendLabel="Aldersgruppe" NAME="a" dataskin=PRESSED  target=REFERANSE barlabel=true barwidth=0.1 BASELINEINTERCEPT=0 group=barcolors;

   /*ScatterPlot X='TARGET'n Y='CU_GENDER'n / discreteOffset=-0.35 Markerattrs=( Symbol=TRIANGLEDOWNFILLED Size=10) DataTransparency=0.4 LegendLabel="normale population" NAME="t";*/

   DiscreteLegend "a" "t" / Location=outside Title="";

endlayout;

endgraph;

end;

run;

ods graphics on / height=500px width=600px;

proc sgrender data=work.test template=profile_age;

format DIFF  percent12.1;

run;

ods graphics off;

Post a Question
Discussion Stats
  • 5 replies
  • 394 views
  • 3 likes
  • 2 in conversation