I want to create a plot as written below (scatter plot with graduated color response by a third variable), except add a fourth group variable identified by the shape of the marker.
For example, there will be two groups, circles and triangles, each having graduated color scheme all on the same x-y plot.
proc sgplot data=mydata;
scatter x=variable1 y=variable2 / colorresponse=variable3
markerattrs=(symbol=CircleFilled)
colormodel=(CX3288BD CX99D594 CXE6F598 CXFEE08B CXFC8D59 CXD53E4F);
run;
Thank you!
Here is a little more complete example, forcing only filled markers, and adding a KEYLEGEND to show your shape values.
proc sgplot data=sashelp.class;
styleattrs datasymbols=(circlefilled squarefilled trianglefilled diamondfilled);
series x=weight y=height / colorresponse=age group=sex groupms=sex
markers lineattrs=(thickness=0) name="scat"
colormodel=(CX3288BD CX99D594 CXE6F598 CXFEE08B CXFC8D59 CXD53E4F);
gradlegend "scat";
keylegend "scat" / type=markersymbol;
run;
Yes, this can be done; but it requires a little slight-of hand 🙂
As you have probably found, you cannot use both a GROUP and a COLORRESPONSE in the SCATTER plot (the GROUP is ignored). However, SERIES plots have additional options that give you the ability to control visual attributes with additional class variables. So, to get what you want, do the following:
1. Change SCATTER to SERIES
2. Add the MARKERS option to turn on the markers.
3. Set LINEATTRS=(thickness=0) /* You don't want to see the lines */
4. Set GROUP=<your class variable> to get grouping
5. Set GROUPMS=<the same class variable> to vary only the marker symbol (COLORRESPONSE will control the color)
Here is a simple example adapting your original code:
proc sgplot data=sashelp.class;
series x=weight y=height / colorresponse=age group=sex groupms=sex
markers lineattrs=(thickness=0)
colormodel=(CX3288BD CX99D594 CXE6F598 CXFEE08B CXFC8D59 CXD53E4F);
run;
Hope this helps!
Dan
Here is a little more complete example, forcing only filled markers, and adding a KEYLEGEND to show your shape values.
proc sgplot data=sashelp.class;
styleattrs datasymbols=(circlefilled squarefilled trianglefilled diamondfilled);
series x=weight y=height / colorresponse=age group=sex groupms=sex
markers lineattrs=(thickness=0) name="scat"
colormodel=(CX3288BD CX99D594 CXE6F598 CXFEE08B CXFC8D59 CXD53E4F);
gradlegend "scat";
keylegend "scat" / type=markersymbol;
run;
I labeled your more complicated version as a solution, but the first, simpler code actually provides better visualization in some situations. I'll be using it too. Thank you again!
OR this one ?
ods graphics/ attrpriority=none; proc sgplot data=sashelp.class; styleattrs datasymbols=(circlefilled squarefilled trianglefilled diamondfilled); scatter x=weight y=height / colorresponse=age group=sex name="scat" colormodel=(CX3288BD CX99D594 CXE6F598 CXFEE08B CXFC8D59 CXD53E4F); gradlegend "scat"; keylegend "scat" ; run;
@Ksharp is correct -- I was overthinking this one. The ATTRPRIORITY option is the key here. If possible, we should adjust the recommended solution to his post.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.