BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
davehalltwp
Quartz | Level 8

Thank you in advance.

 

I am trying to force the items in my scatter plot to legend to appear in a certain order (by numeric visit number, but labeled via a format).  So far I can only get them to appear alphabetically, for some reason.  (Also, not sure why SAS is using the same symbol for multiple visits).

 

Here is my code:

 

proc format;
     value visf   1 = 'Baseline'
	             28 = 'Day 28'
                 91 = 'Month 3'
				183 = 'Month 6'
				274 = 'Month 9'
				365 = 'Month 12'
				457 = 'Month 15'
				548 = 'Month 18'
				639 = 'Month 21'
				731 = 'Month 24'
				822 = 'Month 27'
				913 = 'Month 30';

     run;

proc sgplot data = ttrpc0; 
     scatter x = pc y = ttr / name = "scatter1" 
	                          group = avisitn groupdisplay = cluster
                              markerattrs=(color=black);   
     xaxis label         = 'Plasma AG10 Concentration (ng/mL)' 
           labelattrs    = (family = "courier new" size = 9.9 pt color = black weight = bold)
           offsetmin     =  .04 
           offsetmax     =  .04; 
     yaxis label         = 'Serum TTR (mg/dL)' 
           labelattrs    = (family = "courier new" size = 9.9 pt color = black weight = bold);

     format avisitn visf. ;

     run; 

And here is what the plot currently looks like (a bit busy, but this is what "they" want):

 

davehalltwp_0-1685547598124.png

 

Any ideas from you experts?

 

If nothing else I thought I could create a custom legend, but before that I would need to resolve the distinct-symbol-per-visit issue.  And I don't feel like that should be necessary; I'm not really doing anything sophisticated here.

 

Thank you very much...

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

The reason you're running into the distinctiveness problem is that you have the color forced to black, but the list of marker symbols defined in the ODS style is less than the number of groups (I believe there are only 7 defined in this style).

 

The best approach to give you both the order you want, and all-black distinctive markers, is to use a discrete attributes map, and use that attrmap to populate the legend/ The key in the attrmap is the SHOW column, which you need to set to ATTRMAP to make the legend populate from the attrmap.

 

This code should do it for you. I do not have the data to run, so give it a try and let me know if you still have any issues.

 

proc format;
     value visf   1 = 'Baseline'
	             28 = 'Day 28'
                 91 = 'Month 3'
				183 = 'Month 6'
				274 = 'Month 9'
				365 = 'Month 12'
				457 = 'Month 15'
				548 = 'Month 18'
				639 = 'Month 21'
				731 = 'Month 24'
				822 = 'Month 27'
				913 = 'Month 30';

     run;

data my_attrmap;
retain ID "my_id" show "attrmap" markercolor "black";
length value $ 8 markersymbol $ 11;
input value $ 1-8 markersymbol $;
cards;
Baseline circle
Day 28   plus
Month 3  square
Month 6  asterisk
Month 9  diamond
Month 12 greaterthan
Month 15 lessthan
Month 18 Z
Month 21 X
Month 24 tack
Month 27 tilde
Month 30 union
;
run;

proc sgplot data = ttrpc0  dattrmap=my_attrmap;
     scatter x = pc y = ttr / name = "scatter1" 
	                          group = avisitn groupdisplay = cluster
                              attrid=my_id;   
     xaxis label         = 'Plasma AG10 Concentration (ng/mL)' 
           labelattrs    = (family = "courier new" size = 9.9 pt color = black weight = bold)
           offsetmin     =  .04 
           offsetmax     =  .04; 
     yaxis label         = 'Serum TTR (mg/dL)' 
           labelattrs    = (family = "courier new" size = 9.9 pt color = black weight = bold);

     format avisitn visf. ;

     run; 

View solution in original post

4 REPLIES 4
ballardw
Super User

Try adding to your Sgplot code

Keylegend  /sortorder=Ascending;

The default behavior for Grouped data is the first value of the group variable encountered in the data is displayed first.

 

If you are actually using a monochrome output style you need to have different symbols defined for some of your "months" as you cannot tell the difference between half your values. You can modify the symbols using a Styleattrs statement or a Dattrmap to associate specific values of your group variable with a specific symbol (and color) .

davehalltwp
Quartz | Level 8

Hi Ballardw.  Yeah, I tried

 

Keylegend  /sortorder=Ascending;

before.  It still sorts it according to the formatted value.

 

I will keep digging.

 

Thank you.

 

 

DanH_sas
SAS Super FREQ

The reason you're running into the distinctiveness problem is that you have the color forced to black, but the list of marker symbols defined in the ODS style is less than the number of groups (I believe there are only 7 defined in this style).

 

The best approach to give you both the order you want, and all-black distinctive markers, is to use a discrete attributes map, and use that attrmap to populate the legend/ The key in the attrmap is the SHOW column, which you need to set to ATTRMAP to make the legend populate from the attrmap.

 

This code should do it for you. I do not have the data to run, so give it a try and let me know if you still have any issues.

 

proc format;
     value visf   1 = 'Baseline'
	             28 = 'Day 28'
                 91 = 'Month 3'
				183 = 'Month 6'
				274 = 'Month 9'
				365 = 'Month 12'
				457 = 'Month 15'
				548 = 'Month 18'
				639 = 'Month 21'
				731 = 'Month 24'
				822 = 'Month 27'
				913 = 'Month 30';

     run;

data my_attrmap;
retain ID "my_id" show "attrmap" markercolor "black";
length value $ 8 markersymbol $ 11;
input value $ 1-8 markersymbol $;
cards;
Baseline circle
Day 28   plus
Month 3  square
Month 6  asterisk
Month 9  diamond
Month 12 greaterthan
Month 15 lessthan
Month 18 Z
Month 21 X
Month 24 tack
Month 27 tilde
Month 30 union
;
run;

proc sgplot data = ttrpc0  dattrmap=my_attrmap;
     scatter x = pc y = ttr / name = "scatter1" 
	                          group = avisitn groupdisplay = cluster
                              attrid=my_id;   
     xaxis label         = 'Plasma AG10 Concentration (ng/mL)' 
           labelattrs    = (family = "courier new" size = 9.9 pt color = black weight = bold)
           offsetmin     =  .04 
           offsetmax     =  .04; 
     yaxis label         = 'Serum TTR (mg/dL)' 
           labelattrs    = (family = "courier new" size = 9.9 pt color = black weight = bold);

     format avisitn visf. ;

     run; 
davehalltwp
Quartz | Level 8
Thank you, Dan, solved both problemas...

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 4 replies
  • 1248 views
  • 3 likes
  • 3 in conversation