BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
acuffza
Calcite | Level 5

I'm using SAS 9.4. I'm creating box plots with two categories and I would like to choose a different, customized color for each category. If possible, I don't want to create a graph template because these graphs are exactly how I need them to be and I only want to change the color. Here's my code:

 

%macro differences_box_plot (metabolite=, metabolite_title=, format=);

ods graphics / width=1.5in height=1.1in border=off;
proc sgplot data=diffs;
vbox &metabolite. / category=num_drawnum_phen fillattrs=(color=darkgray) whiskerattrs=(color=black) medianattrs=(color=black) meanattrs=(color=black);
title height=12pt "&metabolite_title." ;
xaxis discreteorder=data display=(nolabel) valueattrs=(size=7pt);
yaxis labelattrs=(size=7pt) valueattrs=(size=7pt);
label &metabolite.="Δ [metabolite], mM";
refline 0 / lineattrs=(pattern=shortdash);
format num_drawnum_phen &format..;
run;
title "";

%mend differences_box_plot;

 


%differences_box_plot (metabolite=Adipate, metabolite_title=Adipate, format=sens_sig);

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

To make the legend go away, add NOAUTOLEGEND to the SGPLOT statement.

 

To assign the fill colors, use an attributes map (like Reeza mentioned). Here is a simple example:

 

data attrmap;
retain id "myid" linecolor "gray";
length value $ 1 fillcolor $ 4;
input value $ fillcolor $;
cards;
F pink
M blue
;
run;

proc sgplot data=sashelp.class dattrmap=attrmap noautolegend;
vbox weight / category=sex group=sex attrid=myid;
run;

Hope this helps!

Dan

View solution in original post

8 REPLIES 8
Reeza
Super User

Assuming SAS 9.3+ you can try an attribute map instead, it's much easier than modifying the template.

https://blogs.sas.com/content/iml/2012/10/17/specify-the-colors-of-groups-in-sas-statistical-graphic...

DanH_sas
SAS Super FREQ

Add:

 

group=num_drawnum_phen

 

to your VBOX statement and you should get independent colors. As for controlling the colors, do you want specific colors assigned to your data values, or does it matter?

 

Thanks!
Dan

acuffza
Calcite | Level 5

There are specific colors I'd like to make each group, it's defaulting to red and blue for the outlines now (other colors are the grays I assigned, but I mainly want to change the fill color of each group). That also made a legend appear which I don't want, do you know how to get rid of that?

DanH_sas
SAS Super FREQ

To make the legend go away, add NOAUTOLEGEND to the SGPLOT statement.

 

To assign the fill colors, use an attributes map (like Reeza mentioned). Here is a simple example:

 

data attrmap;
retain id "myid" linecolor "gray";
length value $ 1 fillcolor $ 4;
input value $ fillcolor $;
cards;
F pink
M blue
;
run;

proc sgplot data=sashelp.class dattrmap=attrmap noautolegend;
vbox weight / category=sex group=sex attrid=myid;
run;

Hope this helps!

Dan

DanH_sas
SAS Super FREQ

You might also want to add an entry for MARKERCOLOR in your attributes map if you are displaying outliers.

acuffza
Calcite | Level 5

That got me a lot closer, but it's still choosing red and blue for some reason! Here's my modified code:

 

data diff_attrmap;
retain id "myid" linecolor "gray"; length fillcolor $4;
input value fillcolor $;
cards;
1 lime
2 cyan
;
run;


%macro differences_box_plot (metabolite=, metabolite_title=, format=);

ods graphics / width=1.5in height=1.1in border=off;
proc sgplot data=diffs dattrmap=diff_attrmap noautolegend;
vbox &metabolite. / category=num_drawnum_phen group=num_drawnum_phen attrid=myid/*fillattrs=(color=darkgray) whiskerattrs=(color=black) medianattrs=(color=black) meanattrs=(color=black)*/;
title height=12pt "&metabolite_title." ;
xaxis discreteorder=data display=(nolabel) valueattrs=(size=7pt);
yaxis labelattrs=(size=7pt) valueattrs=(size=7pt);
label &metabolite.="Δ [metabolite], mM";
refline 0 / lineattrs=(pattern=shortdash);
format num_drawnum_phen &format..;
run;
title "";

%mend differences_box_plot;

acuffza
Calcite | Level 5

Nevermind, I fixed it! I saw that "value" had to be character. Thank you very much!

DanH_sas
SAS Super FREQ

Yes, the VALUE column must contain the FORMATTED value of the group.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 14734 views
  • 2 likes
  • 3 in conversation