Data visualization with SAS programming

Why is SAS changing the colors for a variable in vbar plots in proc sgplot?

Reply
New Contributor
Posts: 2

Why is SAS changing the colors for a variable in vbar plots in proc sgplot?


I'm using SAS 9.3. I have a dataset with several hundred readings. I am attempting to plot two variables relative to a third, using proc sgplot. The first two values, which I will call VarA and VarB, are continuous and have values between 1 and 5. The third variable, XValue, contains only integer values from 1 through 5 inclusive. I am using proc sgplot to print vertical bar charts for the frequency of each  For some reason which I cannot figure out, the colors for the bar charts are different for the two plots. I've attached a file showing the output, where you can see that the color order for the plots is as follows:

       XValue         VarA    VarB

          1               green   green

          2               blue     brown

          3               red       blue

          4               brown   red

          5               purple   purple

In other words, while the end colors are consistent, the colors for the intermediate values are not. I would like the plots to maintain consistent colors. The code I am using follows:

proc format;

value grpf

1 - <1.5 = "1 - 1.5"

1.5 - <2.5 = "1.5 - 2.5"

  2.5 - <3.5 = "2.5 - 3.5"

3.5 - <4.5 = "3.5 - 4.5"

4.5 - 5 = "4.5 - 5";

value xvalf

1 = "Why"

2 = "are"

3 = "these"

4 = "different"

5 = "colors?";

proc sgplot data=use;

title "Why is SAS changing the order of the colors for XValue?";

vbar VarA / stat=freq group=XValue grouporder=ascending groupdisplay=cluster;

format VarA VarB grpf. XValue xvalf.;

run;

proc sgplot data=use;

title "Why is SAS changing the order of the colors for XValue?";

vbar VarB / stat=freq group=XValue grouporder=ascending groupdisplay=cluster;

format VarA VarB grpf. XValue xvalf.;

run;

And the resulting plots are:

SGPlot30.pngSGPlot32.png

As you can see, the only difference between the two instances of proc sgplot is the variable being plotted by the vbar statement. How can I obtain consistent colors for multiple plots?

This is a simplified form of the program. I have many more plots I need to run, and I need the color of XValue to be consistent across all the plots. I'd appreciate any help anyone can give me.

Thanks!

Attachment
SAS Super FREQ
Posts: 1,044

Re: Why is SAS changing the colors for a variable in vbar plots in proc sgplot?

Group colors are assigned based on the ORDER in which the group values are encountered in the data.  If you want consistent group colors, you can use the Discrete Attributes Map Feature, where you can specify which group VALUE" gets which color.

New Contributor
Posts: 2

Re: Why is SAS changing the colors for a variable in vbar plots in proc sgplot?

Hi Sanjay, the group variable is identical in both plots. The dataset is identical, I am not sorting or changing it in any way between the two instances of proc sgplot. If I understand what you have said correctly, and  if I have not changed the order of the data, then the order of the colors should not be changing.

SAS Super FREQ
Posts: 864

Re: Why is SAS changing the colors for a variable in vbar plots in proc sgplot?

It's because the data order is different between varA and varB. The colors are assigned to the group values in the order that they come in. We have a nice feature in the SG procedures to assign attributes directly to group values so that they always look the same regardless of ordering. It's called an "attributes map". Think of it as a format for visual attributes :-). The attrmap must contain the *formatted* value for comparison. See the doc for more details. In the meantime, try this:

data attrmap;

retain id "myid";

length value $ 9 fillstyle $ 10;

input value $ fillstyle $;

cards;

Why       graphdata1

are       graphdata2

these       graphdata3

different       graphdata4

colors?       graphdata5

;

proc sgplot data=use dattrmap=attrmap;

title "Why is SAS changing the order of the colors for XValue?";

vbar VarA / stat=freq group=XValue grouporder=ascending groupdisplay=cluster attrid=myid;

format VarA VarB grpf. XValue xvalf.;

run;

proc sgplot data=use dattrmap=attrmap;

title "Why is SAS changing the order of the colors for XValue?";

vbar VarB / stat=freq group=XValue grouporder=ascending groupdisplay=cluster attrid=myid;

format VarA VarB grpf. XValue xvalf.;

run;

Post a Question
Discussion Stats
  • 3 replies
  • 648 views
  • 4 likes
  • 3 in conversation