Hello all,
I was wondering if it's possible to change the color of the markers within PROC SGPLOT when you have multiple groups as in the example below.
Thank you very much in advance!
Recep
DATA test;
INPUT Hospital $ age_group $ count;
CARDS;
Hosp1 0-16 0
Hosp1 17-74 10
Hosp1 75+ 1
Hosp2 0-16 14
Hosp3 0-16 1
Hosp3 17-74 16
Hosp4 0-16 1
Hosp4 17-74 4
Hosp5 17-74 7
Hosp6 17-74 80
Hosp6 75+ 2
Hosp7 17-74 4
Hosp8 17-74 9
Hosp9 17-74 95
Hosp9 75+ 1
Hosp10 0-16 1
Hosp10 17-74 27
;
RUN;
proc sgplot data=test;
scatter x=hospital y=count/ group=age_group
grouporder=ascending
MARKERATTRS=(symbol=circlefilled size=10)
LEGENDLABEL= "Hospitalizations"
datalabel
datalabelpos=top;
xaxis display=all fitpolicy=rotate grid;
yaxis offsetmin=0.02 grid;
title ' ';
run;
Do you mean that you want to use specific colors, a different set or something else? The ODS style sets the default colors so changing style changes colors (usually). If you want to override the default color list for that call to the procedure only then that might be the STYLEATTRS statement.
DATACONTRAST colors affect markers. So you might try
proc sgplot data=test; styleattrs datacontrastcolors= (red green blue gold) ; scatter x=hospital y=count/ group=age_group grouporder=ascending MARKERATTRS=(symbol=circlefilled size=10) LEGENDLABEL= "Hospitalizations" datalabel datalabelpos=top; xaxis display=all fitpolicy=rotate grid; yaxis offsetmin=0.02 grid; title ' '; run;
The typical behavior of most of these procedures is the first value encountered gets the color from the list, the second value the second and so on. So data order can affect color/marker/line type etc.
If you want a consistent assignment you need to keep data order consistent, which might mean sorting by a group variable.
Or if you want consistent colors across many procedures you may want to create a DATTRMAP data set.
Do you mean that you want to use specific colors, a different set or something else? The ODS style sets the default colors so changing style changes colors (usually). If you want to override the default color list for that call to the procedure only then that might be the STYLEATTRS statement.
DATACONTRAST colors affect markers. So you might try
proc sgplot data=test; styleattrs datacontrastcolors= (red green blue gold) ; scatter x=hospital y=count/ group=age_group grouporder=ascending MARKERATTRS=(symbol=circlefilled size=10) LEGENDLABEL= "Hospitalizations" datalabel datalabelpos=top; xaxis display=all fitpolicy=rotate grid; yaxis offsetmin=0.02 grid; title ' '; run;
The typical behavior of most of these procedures is the first value encountered gets the color from the list, the second value the second and so on. So data order can affect color/marker/line type etc.
If you want a consistent assignment you need to keep data order consistent, which might mean sorting by a group variable.
Or if you want consistent colors across many procedures you may want to create a DATTRMAP data set.
Thank you very much!
styleattrs datacontrastcolors= (red green blue gold); option did exactly what I needed.
I also realized that if VBAR statement is used in the PROC SGPLOT, instead of "datacontrastcolors=(...)", "datacolors=(...)" will do the trick.
Best,
Recep
@Recep wrote:
Thank you very much!
styleattrs datacontrastcolors= (red green blue gold); option did exactly what I needed.
I also realized that if VBAR statement is used in the PROC SGPLOT, instead of "datacontrastcolors=(...)", "datacolors=(...)" will do the trick.
Best,
Recep
Part of the fun is finding out which color set goes with which display.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.