Data visualization with SAS programming

order groups in sgplot vbar

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 124
Accepted Solution

order groups in sgplot vbar

Hi,

I am trying to make an sgplot vbar with grouped values in order to apply a certain color to certain bars

But I can't choose the order of grouping (0 should be green, 1 yellow, 2 orange and 3 red).

Is there a way to do that ?

MEAS_Pedisurv.png

Here is my code :

%let path='P:\testing\AlertLevels\graphs\';

proc template;

  define style styles.alertlevels;

  parent=styles.default;

    style body from body / background = white;

    class graphgridlines  / contrastcolor=black;

    class graphbackground / color=white;

    class graphwalls / color=white;

          class GraphData1 / color=CX00FF00; /* green a00FF0066*/

          class GraphData2 / color=CxEEFF00; /* yellow */

          class GraphData3 / color=CXFF8000; /* orange */

          class GraphData4 / color=CXFF0000; /* red */

  end;

run;

ods _all_ close;

goptions device=png;

ods html file='Ptests.html' path=&path style=alertlevels;

ods graphics on / reset=all imagename=&PathogenSource2;

proc sgplot data=PTEST_&PathogenSource;

                    vbar Date /  response=Cases group=AlertLevel nooutline barwidth=.7 transparency=.1 name='zut';

                    xaxis label = 'Week' fitpolicy=rotatethin;

                    yaxis label = 'Cases/week' grid;

                    title &PathogenSource;

                    keylegend 'zut' / noborder title='Alert Level';

run;


Accepted Solutions
Solution
‎02-06-2012 10:17 AM
SAS Super FREQ
Posts: 889

order groups in sgplot vbar

The reason that I asked about the SAS version is that, in SAS 9.3, we added functionality to the SG procedures that gives you the ability to assign visual attributes to group values. Since you're running SAS 9.2, the best solution is to use the Graph Template Language (GTL) and the INDEX option to index into the style. The INDEX takes a one-based column that references the number of the GraphDataN style element. You can make this column data step by just adding 1 to your alert level. Below is a little sample for you to see how the option works.

Hope this helps!

Dan

data levels;
informat date date7.;
format date date7.;
input date resp level style;
cards;
01jan11 12 1 2
01feb11 22 1 2
01mar11 32 2 3
01apr11 42 3 4
01may11 52 1 2
01jun11 12 1 2
01jul11 22 2 3
01aug11 32 3 4
01sep11 42 0 1
01oct11 52 1 2
01nov11 12 2 3
01dec11 22 3 4
;
run;


proc template;
define statgraph barchart;
begingraph;
layout overlay;
   barchart x=date y=resp / group=level index=style;
endlayout;
endgraph;
end;
run;

proc sgrender data=levels template=barchart; run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 889

order groups in sgplot vbar

What version of SAS do you have?

Frequent Contributor
Posts: 124

order groups in sgplot vbar

I have SAS Enterprise Guide 4.3 with SAS 9.2

Grand Advisor
Posts: 10,210

order groups in sgplot vbar

Have you tried sorting the data by AlertLevel? It may be that the color assignment is being done by the order the group variable appears in the data.

Frequent Contributor
Posts: 124

order groups in sgplot vbar

I just tried.

But no, it doesn't change anything.

Solution
‎02-06-2012 10:17 AM
SAS Super FREQ
Posts: 889

order groups in sgplot vbar

The reason that I asked about the SAS version is that, in SAS 9.3, we added functionality to the SG procedures that gives you the ability to assign visual attributes to group values. Since you're running SAS 9.2, the best solution is to use the Graph Template Language (GTL) and the INDEX option to index into the style. The INDEX takes a one-based column that references the number of the GraphDataN style element. You can make this column data step by just adding 1 to your alert level. Below is a little sample for you to see how the option works.

Hope this helps!

Dan

data levels;
informat date date7.;
format date date7.;
input date resp level style;
cards;
01jan11 12 1 2
01feb11 22 1 2
01mar11 32 2 3
01apr11 42 3 4
01may11 52 1 2
01jun11 12 1 2
01jul11 22 2 3
01aug11 32 3 4
01sep11 42 0 1
01oct11 52 1 2
01nov11 12 2 3
01dec11 22 3 4
;
run;


proc template;
define statgraph barchart;
begingraph;
layout overlay;
   barchart x=date y=resp / group=level index=style;
endlayout;
endgraph;
end;
run;

proc sgrender data=levels template=barchart; run;

Frequent Contributor
Posts: 124

order groups in sgplot vbar

This worked really well.

It was a bit more tricky to get the same result.

Thank you very much Dan

SGRender33.png

proc template;

          define statgraph barchart;

                    begingraph / border=false;

                              title;

                              entrytitle 'MEAS_PEDISURV';

                              layout overlay / yaxisopts=(label='Cases / week' griddisplay=on) xaxisopts=(label='week');

                                        barchart x=Date y=Cases / group=AlertLevel index=index barwidth=.8

                                                  name='zut' outlineattrs=(thickness=0 color=white)

                                                  datatransparency=.1 legendlabel='Alert Level' grouporder=ascending;

                                        discretelegend 'zut'/ border=false title='Alert Level';

                              endlayout;

                    endgraph;

          end;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 3052 views
  • 3 likes
  • 3 in conversation