BookmarkSubscribeRSS Feed
RobinDC
Calcite | Level 5


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!

3 REPLIES 3
Jay54
Meteorite | Level 14

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.

RobinDC
Calcite | Level 5

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.

DanH_sas
SAS Super FREQ

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1965 views
  • 4 likes
  • 3 in conversation