Data visualization with SAS programming

SGPLOT VBOX Change Category Color

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

SGPLOT VBOX Change Category Color

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);


Accepted Solutions
Solution
‎01-05-2018 11:43 AM
SAS Super FREQ
Posts: 1,042

Re: SGPLOT VBOX Change Category Color

[ Edited ]

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


All Replies
Super User
Posts: 22,823

Re: SGPLOT VBOX Change Category Color

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...

SAS Super FREQ
Posts: 1,042

Re: SGPLOT VBOX Change Category Color

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

Contributor
Posts: 23

Re: SGPLOT VBOX Change Category Color

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?

Solution
‎01-05-2018 11:43 AM
SAS Super FREQ
Posts: 1,042

Re: SGPLOT VBOX Change Category Color

[ Edited ]

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

SAS Super FREQ
Posts: 1,042

Re: SGPLOT VBOX Change Category Color

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

Contributor
Posts: 23

Re: SGPLOT VBOX Change Category Color

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;

Contributor
Posts: 23

Re: SGPLOT VBOX Change Category Color

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

SAS Super FREQ
Posts: 1,042

Re: SGPLOT VBOX Change Category Color

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

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 508 views
  • 0 likes
  • 3 in conversation