BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_123_
Fluorite | Level 6

I have a scatter plot with some added lines but I don't want to show them in the legend - I must be referencing it wrong in the keylegend exclude options.  Can anyone help?

 

proc sgplot data=work.data noborder;

scatter x=var1 y=var2 group=var3;

lineparm x=0 y=5 SLOPE=0;

lineparm x=70 y=0 SLOPE=5;

keylegend /  

   across=2

   location=inside

   position=bottomright

   noborder

   exclude = ("lineparm");

run;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Do you not need to name each plot for this to work?

proc sgplot data=work.data noborder noautolegend
  scatter x=var1 y=var2 group=var3 name="scatter";
  lineparm x=0 y=5 slope=0 name="line1";
  lineparm x=70 y=0 slope=5 name="line2";
  keylegend "scatter" / across=2 location=inside position=bottomright noborder;
run;

Note how I use don't mix in upper case - it makes reading code harder.

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Do you not need to name each plot for this to work?

proc sgplot data=work.data noborder noautolegend
  scatter x=var1 y=var2 group=var3 name="scatter";
  lineparm x=0 y=5 slope=0 name="line1";
  lineparm x=70 y=0 slope=5 name="line2";
  keylegend "scatter" / across=2 location=inside position=bottomright noborder;
run;

Note how I use don't mix in upper case - it makes reading code harder.

PeterClemmensen
Tourmaline | Level 20

You can reference your scatter plot in the Keylegend statement by using the name="scatter" in the scatter options and specify keylegend "scatter" to reference only the scatter plot in the legend.

 

Also, I think you are missing a / in your scatter statement before your group= option

_123_
Fluorite | Level 6

Thanks - worked a charm (and there was a missing / - I removed various other options to make it simpler to paste it in and just got trigger happy with the delete key!)

_123_
Fluorite | Level 6

So one other largely trivial thing to do on this plot that I can't figure out.... I have ten categories and in the legend they are listed by row then column, rather than going 1 to 5 on the left hand side and 6-10 on the right hand side.  Can't find a way to edit this...

 

proc sgplot data=work.data noborder;

styleattrs dataconstrastcolors=("&colour10" "&colour9" "&colour8" "&colour7"

"&colour6" "&colour5" "&colour4" "&colour3" "&colour2" "&colour1");

scatter x=var1 y=var2 / name="scatter"

group=grouping_var

markerattrs=(size=4 symbol=CircleFilled);

lineparm x=0 y=1000 slope=0 / name="name1"

lineattrs=(colour="dark red" thickness=2);

lineparm x=1000 y=0 slope=99 / name="name2"

lineattrs=(colour="dark red" thickness=2);

xaxis max=25000 min=200 label="Title" labelattrs=(weight=bold);

yaxis reverse min=0 max=20000 label="Title" labelattrs=(weight=bold);

keylegend "scatter" /

title="Title"

titleattrs=(weight=bold)

across=2

location=inside

position=bottomright

noborder

opaque;

run;

ballardw
Super User

@_123_ wrote:

So one other largely trivial thing to do on this plot that I can't figure out.... I have ten categories and in the legend they are listed by row then column, rather than going 1 to 5 on the left hand side and 6-10 on the right hand side.  Can't find a way to edit this...

 

proc sgplot data=work.data noborder;

 

keylegend "scatter" /

 

title="Title"

titleattrs=(weight=bold)

across=2

location=inside

position=bottomright

noborder

opaque;

run;


You do not describe what you want your legend to look like but what do you think that highlighted option above does? It makes the legend have two columns. The DOWN option controls the number of rows.

_123_
Fluorite | Level 6

Ideally I want two columns by five rows but the grouping is effectively by decile.  When I put the across two in, I get an order like this:

 

1  2

3  4

5  6

7  8

9  10

 

It would be easier to read it like this:

 

1  6

2  7

3  8

4  9

5  10

 

It's not a deal breaker but it seems like something I should be able to fix (If I knew more about what I was doing!)

 

WarrenKuhfeld
Rhodochrosite | Level 12

This post among other things shows how to control the order of the legend.

 

https://blogs.sas.com/content/graphicallyspeaking/2017/04/04/consistent-ordering-graph-components/

 

The following step creates the attribute map:

data attr(drop=n);        			
   retain ID 'a' Show 'AttrMap' ;
   input Value $ 1-9 n;     			
   LineStyle        = cats('GraphData', n); 		
   MarkerStyle      = linestyle;           	 	
   FillStyle        = linestyle;
   TextStyleElement = linestyle;
   datalines;
> 90 days 2
> 30 days 12
Current   3
;

It reads the instream data set that contains the values of the account status, and the number of the GraphDatan style element that is used for each. Assignment statements create all of the style variables that are available in an attribute map (even those that do not get used in this particular example). The variable Show='AttrMap' makes the legend appear in the order of the values in the attribute map data set. The following step shows a first pass at making the graph.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 2281 views
  • 5 likes
  • 5 in conversation