Data visualization with SAS programming

How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

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;


Accepted Solutions
Solution
‎01-26-2018 08:26 AM
Super User
Super User
Posts: 9,193

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

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


All Replies
Solution
‎01-26-2018 08:26 AM
Super User
Super User
Posts: 9,193

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

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.

PROC Star
Posts: 1,190

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

[ Edited ]

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

New Contributor
Posts: 4

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

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!)

New Contributor
Posts: 4

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

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;

Super User
Posts: 12,996

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)


_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.

New Contributor
Posts: 4

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

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!)

 

SAS Super FREQ
Posts: 496

Re: How do I exclude a line added to a scatter plot from the legend (keylegend exclude lineparm)

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.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 154 views
  • 4 likes
  • 5 in conversation