When I run this code, for the first BY group (Origin=Asia), the legend shows that SUV is represented by the red circle. In the second BY group (Origin=Europe), SUV is represented by the cyan color circle. How can I get consistent colors across BY groups, so that each group has the same color in each plot?
proc sort data=sashelp.cars out=cars;
by origin;
run;
proc sgplot data=cars;
styleattrs datacontrastcolors=(red green blue cyan magenta orange black);
by origin;
scatter x=msrp y=mpg_city/group=type;
run;
Hello, you can use Discrete Attribute Maps to achieve the consistency between groups. This is because generally, the attributes for the group are determined by the order that the value is within the data. Using discrete attribute maps can make you indicate the colors that you want to assign to each group value.
There are more details here: https://documentation.sas.com/?docsetId=grstatproc&docsetTarget=n18szqcwir8q2nn10od9hhdh2ksj.htm&doc...
Here is an example:
proc sort data=sashelp.cars out=cars;
by origin;
run;
data myattrmap;
length markercolor $ 7 markersymbol $ 6;
input id $ value $ markercolor $ markersymbol $;
datalines;
myid Hybrid red circle
myid SUV green circle
myid Sedan blue circle
myid Sports cyan circle
myid Truck magenta circle
myid Wagon orange circle
;
run;
proc sgplot data=cars dattrmap=myattrmap;
by origin;
scatter x=msrp y=mpg_city/group=type attrid=myid;
run;
Hello, you can use Discrete Attribute Maps to achieve the consistency between groups. This is because generally, the attributes for the group are determined by the order that the value is within the data. Using discrete attribute maps can make you indicate the colors that you want to assign to each group value.
There are more details here: https://documentation.sas.com/?docsetId=grstatproc&docsetTarget=n18szqcwir8q2nn10od9hhdh2ksj.htm&doc...
Here is an example:
proc sort data=sashelp.cars out=cars;
by origin;
run;
data myattrmap;
length markercolor $ 7 markersymbol $ 6;
input id $ value $ markercolor $ markersymbol $;
datalines;
myid Hybrid red circle
myid SUV green circle
myid Sedan blue circle
myid Sports cyan circle
myid Truck magenta circle
myid Wagon orange circle
;
run;
proc sgplot data=cars dattrmap=myattrmap;
by origin;
scatter x=msrp y=mpg_city/group=type attrid=myid;
run;
Thanks @djrisks. I think I should have thought of that.
Anyway, I discovered that when you do this on my real data, which uses custom formats, the value in the MYATTRMAP must be the formatted value, not the unformatted value.
You're welcome @PaigeMiller . Yes, that's true about the formatted values.
For an introduction to discrete attribute maps, see the articles
"Roses are red, violets are blue" by Dan Heath
or
"Consistent groups colors by value" by Sanjay Matange
For advanced users, see "Automate the creation of a discrete attribute map" by Rick Wicklin
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.