Data visualization with SAS programming

SGPlot loess legend--can I get symbols?

Reply
Occasional Contributor
Posts: 14

SGPlot loess legend--can I get symbols?

Hey All,

 

I love the plot I get with the below code, except that I would like to have the different marker symbols appear on the lines in the legend--like this:

 

legend_want.PNG

 

(I get that if I specify a SERIES plot with the /MARKERS option.)

 

Instead, I just get the colored lines w/no symbols.  In the fake data I give below it's not a big deal, but my real graph has 12 different groups.

 

I've played around with the KEYLEGEND statement, but have not figured out a way to add those symbols.

 

Can it be done?

 

I'm using 9.4 (TS1M4) on X64_7PRO.

 

Many thanks!

 

-Roy

 

data gnu ;
  input
    @1    clinic   $char5.
    @9    mo       date9.
    @21   metric   3.0
  ;
  format mo mmddyy10. ;
datalines ;
north   01jan2002   344
north   01feb2002   400
north   01mar2002   300
north   01apr2002   423
north   01may2002   600
north   01jun2002   603
south   01jan2002   356
south   01feb2002   412
south   01mar2002   500
south   01apr2002   489
south   01may2002   200
south   01jun2002   193
run ;

options orientation = landscape ;
* ods graphics / height = 8in width = 10in ;

ods html
  path  = "c:\deleteme" (URL=NONE)
  body  = "deleteme.html"
  style = magnify
  nogfootnote
;

  proc sgplot data = gnu ;
    * series  x = mo y = metric / group = clinic lineattrs=(pattern = solid) markers ;
    loess  x = mo y = metric / group = clinic lineattrs=(pattern = solid) ;
  run ;

ods html close ;

The plot that gets me is:

SGPlot1.png

SAS Super FREQ
Posts: 806

Re: SGPlot loess legend--can I get symbols?

Posted in reply to RoyPardee

It can be done, but you need SAS9.4M5 using LEGENDITEM statement. 

 

See example below

ods graphics / attrpriority=none;

proc sgplot data = gnu  ;
  styleattrs
    datacolors=(green blue)
      DATACONTRASTCOLORS=(green blue)
      datasymbols=(square triangle)
      datalinepatterns=(solid solid)

;
  scatter  x = mo y = metric / group = clinic name="scatter";
  loess  x = mo y = metric / group = clinic nomarkers  name="loess";
  legenditem type=markerline name="g1" / 
    label="north"
    markerattrs=(color=green symbol=square)
    lineattrs=(color=green)
  ;
  legenditem type=markerline name="g2" / 
    label="south"
    markerattrs=(color=blue symbol=triangle)
    lineattrs=(color=blue)
  ;
  keylegend 
    "scatter" "loess"
    "g1" "g2"
  ;
run;

you can also use ODS TRACE ON; to see which output objects are created, then use ODS OUTPUT objName=datasetName; to get the data and then use other statements to plot what you want.

 

see this blog entry https://blogs.sas.com/content/graphicallyspeaking/2018/01/09/need-know-graph-template-data-object-pr... by @WarrenKuhfeld for details

Occasional Contributor
Posts: 14

Re: SGPlot loess legend--can I get symbols?

Posted in reply to Bruno_SAS

Thanks much for the reply!  Alas, that does not work for me--sas does not recognize the legenditem statement, no doubt b/c I'm not on the version you specified.

 

Also--yikes--that's a lot of code.

 

Maybe I should add it to the SAS Ballot as a requested enhancement...

SAS Super FREQ
Posts: 806

Re: SGPlot loess legend--can I get symbols?

Posted in reply to RoyPardee

Try this GTL code, should work with your version of SAS.

 

You be the judge on the length of the code ;-)

 


ods path
  (prepend) work.myTemplates (update)
;
ods graphics / attrpriority=none;
proc template;
  define statgraph sgplot;
    begingraph / collation=binary subpixel=on dataColors=( CX008000 CX0000FF ) dataContrastColors=( CX008000 CX0000FF ) dataSymbols=( SQUARE TRIANGLE ) dataLinePatterns=( 1 1 );
      LegendItem TYPE=MARKERLINE NAME="g1" / Lineattrs=( Color=CX008000) Markerattrs=( Color=CX008000 Symbol=SQUARE) LABEL="north";
      LegendItem TYPE=MARKERLINE NAME="g2" / Lineattrs=( Color=CX0000FF) Markerattrs=( Color=CX0000FF Symbol=TRIANGLE) LABEL="south";
      layout overlay / yaxisopts=(labelFitPolicy=Split) y2axisopts=(labelFitPolicy=Split);
        ScatterPlot X=mo Y=metric / subpixel=off primary=true Group=clinic LegendLabel="metric" NAME="scatter";
        LoessPlot X=mo Y=metric / NAME="loess" LegendLabel="Loess" Group=eval(sort(clinic, RETAIN=ALL));
        DiscreteLegend "scatter" "loess" "g1" "g2" / Location=Outside;
      endlayout;
    endgraph;
  end;
run;

proc sgrender data=gnu template=sgplot;
run;

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 111 views
  • 2 likes
  • 2 in conversation