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

Hi!

My plot has WALLCOLOR=LTGRAY. Is it possible to have that color as background in the legend too?

Here is a part of the syntax:

PROC SGPLOT DATA = DB;
styleattrs datacontrastcolors=(red blue green magenta yellow black) WALLCOLOR=LTGRAY
             datasymbols=(x x x circle circle circle Triangle Triangle Triangle);
   SERIES X = date Y = Wert / markers lineattrs=(pattern=solid) markerattrs=(size=11) group = Nr;
   keylegend/position=bottom valueattrs=(family=arial size=12pt) down=3;
...

Best regards 🕊

Antje

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Actually there is any easier way which does not involve any GTL, but defining your own style, check out the sample below.

 

/*
 * have a look at the default style definition
 * search for graphlegendbackground
 */
proc template;
  source styles.default;
run;

/*
 * now create your own style based on the default style you use
 * just change the definition for GraphLegendBackground
 */
proc template;
  define style mystyle;
    parent=styles.htmlblue;
    class GraphLegendBackground /
      backgroundcolor = ltgray
      color = ltgray
  ;
  end;
run;

/*
 * now create the graph using your own style definition
 * et voilà
 */
ods html5 file="c:\temp\legendbackground.html"
          style=mystyle
      ;
      
proc sgplot data=sashelp.class tmplout="c:\temp\sgplot.sas";
  styleattrs
  datacontrastcolors=(red blue green magenta yellow black) WALLCOLOR=LTGRAY
    datasymbols=(x x x circle circle circle Triangle Triangle Triangle);
  series x=age y=height / group=sex;
  keylegend / location=outside ;
run;
ods html5 close;

Also check this paper, https://support.sas.com/resources/papers/proceedings17/SAS0675-2017.pdf, on the various style elements being used.

View solution in original post

8 REPLIES 8
Ksharp
Super User

Do you mind to put it inside graph ?

 

   keylegend/position=bottom valueattrs=( family=arial size=12pt) down=3 location=inside;
Ksharp
Super User

Or if you want make backcolor be LTGRAY ?

 

PROC SGPLOT DATA = SASHELP.HEART ;
styleattrs datacontrastcolors=(red blue green magenta yellow black)
WALLCOLOR=LTGRAY backcolor=LTGRAY
             datasymbols=(x x x circle circle circle Triangle Triangle Triangle);
   SERIES X = weight Y = height / markers lineattrs=(pattern=solid) markerattrs=(size=11) group = bp_status;
   keylegend/position=bottom   valueattrs=( family=arial size=12pt) down=3 NOOPAQUE  

;
run;
AntjeWestphal
Obsidian | Level 7

Thanks, @Ksharp!

If there is no other option to get only a grey background of the legend, I would choose your second idea.

BrunoMueller
SAS Super FREQ

The KEYLEGEND statement does not support to set the background. Proc SGPLOT code is translated into the Graph Template Language (GTL), on this level it is possible to set the background of the legend.

 

How to get the GTL from Proc SGPLOT, add the TMPLOUT= option, see below.

proc sgplot data=sashelp.class tmplout="c:\temp\sgplot.sas";
  styleattrs
  datacontrastcolors=(red blue green magenta yellow black) WALLCOLOR=LTGRAY
    datasymbols=(x x x circle circle circle Triangle Triangle Triangle);
  series x=age y=height / group=sex;
  keylegend / location=outside ;
run;

Have a look at the resulting file, this the GTL code used to create the graph.

 

Now adapt the code by adding the BACKGROUND= option to the DISCRETELEGEND statement, see below for an example.

ods path
  (prepend) work.mytemplates (update)
;

proc template;
define statgraph sgplot;
begingraph / collation=binary subpixel=on dataContrastColors=( CXFF0000 CX0000FF CX008000 CXFF00FF CXFFFF00 CX000000 ) dataSymbols=( X X X CIRCLE CIRCLE CIRCLE TRIANGLE TRIANGLE TRIANGLE );
layout overlay / WallColor=CXC0C0C0 yaxisopts=(labelFitPolicy=Split) y2axisopts=(labelFitPolicy=Split);
   SeriesPlot X='Age'n Y='Height'n / primary=true Group='Sex'n LegendLabel="Height" NAME="SERIES";
   DiscreteLegend "SERIES" / Location=Outside Title="Sex" backgroundcolor=ltgray;
endlayout;
endgraph;
end;
run;

proc sgrender data=sashelp.class template=sgplot;
run;

 

 

AntjeWestphal
Obsidian | Level 7

Thanks @BrunoMueller!

I never used proc template before and I have to become acquainted with it.

BrunoMueller
SAS Super FREQ

As a general rule always start with proc SGPLOT, SGPANEL etc. Only if there is not other way I would go for the Graph Template Language.

BrunoMueller
SAS Super FREQ

Actually there is any easier way which does not involve any GTL, but defining your own style, check out the sample below.

 

/*
 * have a look at the default style definition
 * search for graphlegendbackground
 */
proc template;
  source styles.default;
run;

/*
 * now create your own style based on the default style you use
 * just change the definition for GraphLegendBackground
 */
proc template;
  define style mystyle;
    parent=styles.htmlblue;
    class GraphLegendBackground /
      backgroundcolor = ltgray
      color = ltgray
  ;
  end;
run;

/*
 * now create the graph using your own style definition
 * et voilà
 */
ods html5 file="c:\temp\legendbackground.html"
          style=mystyle
      ;
      
proc sgplot data=sashelp.class tmplout="c:\temp\sgplot.sas";
  styleattrs
  datacontrastcolors=(red blue green magenta yellow black) WALLCOLOR=LTGRAY
    datasymbols=(x x x circle circle circle Triangle Triangle Triangle);
  series x=age y=height / group=sex;
  keylegend / location=outside ;
run;
ods html5 close;

Also check this paper, https://support.sas.com/resources/papers/proceedings17/SAS0675-2017.pdf, on the various style elements being used.

AntjeWestphal
Obsidian | Level 7

@BrunoMueller Great, it works perfectly! Thanks for your input and the paper!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 1208 views
  • 11 likes
  • 3 in conversation