Data visualization with SAS programming

Independent legend

Accepted Solution Solved
Reply
Super Contributor
Posts: 494
Accepted Solution

Independent legend

Is there a good way to create an independent legend that you can use for multiple graphs in Enterprise guide 5.1 SAS 9.2?  So that all graphs use the same color schema and the legend is only shown once?


Accepted Solutions
Solution
‎12-12-2014 10:49 AM
Super Contributor
Posts: 252

Re: Independent legend

David, I have presented a paper that shows you how you can plot your own legend:

http://www.lexjansen.com/pharmasug/2014/DG/PharmaSUG-2014-DG13.pdf

Otherwise, I agree with @ballardw suggestion to create a consistent legend.

View solution in original post


All Replies
Super User
Posts: 11,134

Re: Independent legend

The ability to assign graphic attributes to values across procedures was one of the major additions to SG graphics in 9.3. In 9.2 careful ordering of the categorical variables, something ensuring they appeared with a missing or 0 value depending on the procedure usually let me do this IF I pre-summarized the data. A sum of one value is that value ...

The legend issue, assuming you can get the color schema to work, might be the option NOAUTOLEGEND on the procedures where you don't want the legend to appear.

Super Contributor
Posts: 494

Re: Independent legend

This article might do the trick still playing with it.

http://support.sas.com/kb/24/945.html

Solution
‎12-12-2014 10:49 AM
Super Contributor
Posts: 252

Re: Independent legend

David, I have presented a paper that shows you how you can plot your own legend:

http://www.lexjansen.com/pharmasug/2014/DG/PharmaSUG-2014-DG13.pdf

Otherwise, I agree with @ballardw suggestion to create a consistent legend.

SAS Super FREQ
Posts: 1,145

Re: Independent legend

If you really want the result as in the linked sample, and not just an independent legend, proc SGPANEL is perfectly suited for this.  It would produce a common legend, all in probably 4-5 lines of code.  Use same data set as in the sample program.  Should work with SAS 9.2.

ods graphics / reset width=5in height=3in attrpriority=color';

proc sgpanel data=one;

  panelby grp;

  series x=x y=y / group=z markers;

  run;

SeriesPanel.png

Super Contributor
Posts: 494

Re: Independent legend

In my particular case I need to charge the scale between the various graphs.  It looks like sgpanel provides one scale for multiple graphs.

SAS Super FREQ
Posts: 1,145

Re: Independent legend

You can set the UNIFORM option to get  different scaling when the axis is not shared.

Else, you can use proc SGSCATTER with PLOT statement to get fully independent plots cells.

Super Contributor
Posts: 494

Re: Independent legend

I need to include different where clauses to include different subsets of data.  Sgpannel has the group clause however it does not have the option to manually specify different subsets.

E.g. only show summation total, show all possibilities. 

I’m comparing similar graphs using line charts and bar graphs.  Sgscatter is for scatterplots.

SAS Super FREQ
Posts: 925

Re: Independent legend

Actually. if you use the JOIN option on the PLOT or COMPARE statements in SGSCATTER, you can get line plots as well.

Super Contributor
Posts: 494

Re: Independent legend

Could you post a link to an example of sgscatter using a line graph without the typical scatter plots? I am using a summation clause in my current plots, the sgscatter functions appear to be for plotting data points rather than summations.  The compare feature compares two columns, I am trying to compare two different subsets using a where clause.

SAS Super FREQ
Posts: 925

Re: Independent legend

Here are some simple examples:

proc means data=sashelp.class;

class age;

var weight height;

output out=temp mean=;

run;

proc sgscatter  data=temp;

plot (weight height)*age / join;

run;

proc sgscatter data=temp;

compare x=age y=(weight height) / join;

run;

Super Contributor
Posts: 494

Re: Independent legend

Can I use the same procedure to do:

Graph 1

line for the weight for age 11

line for the weight for age 12

line for the weight for age 13

Then beside it using the same legend

Graph 2

line for the weight for age 14 only including where mean > 55

line for the weight for age 15 only including where mean > 55

line for the weight for age 16 only including where mean > 55


In other words can I use where clause like functions?

Super Contributor
Posts: 494

Re: Independent legend

I’ve been working off of djrisks’ paper.  I was able to use it to create an independent series plot.  I’m currently working on adding manual sorting and coloring to the legend.  Note that the base part of the code is this:  the additional grouping is not needed for most situations.

data enroll_led;

set enroll_le;

index_final = _n_;

run;

data legend;

set enroll_led;

/* For patient legend */

do i = 6, 10;

xax = i;

output;

end;

drop i;

run;

ods graphics on / reset = all border = off imagefmt = png width = 3in height = 2in imagename

= "grlegend";

goptions reset = all;

proc template;

                define statgraph order;

                 dynamic TITLE1 TITLE2;

                 begingraph;

                 entrytitle TITLE1;

                 entrytitle TITLE2;

                layout overlay / xaxisopts =(display = NONE) yaxisopts = (display = NONE)

                y2axisopts = ( display = STANDARD reverse = true label = " "

                linearopts= ( tickvaluesequence=(start=1 end=32 increment=1)));

                /* Main plot */

                seriesplot x=xax y= degree_level / yaxis = y2 display = all group=degree_level

                index = index_final lineattrs = (pattern =1 thickness = 1) /*markerattrs = (size = 1pt)*/;

                endlayout;

                 endgraph;

                end;

                run;

                proc sgrender data= legend template=order;

                     dynamic title1=" ";

                     dynamic title2="Legend";

                run;

Super Contributor
Posts: 252

Re: Independent legend

David, I'm pleased my paper helped you! For clarification, what would you like to do now?

Super Contributor
Posts: 494

Re: Independent legend

My overall goal which now is achievable given your paper, a bit of additional research, and proof of concepts; is to have a legend for a number of small dynamic dashboard graphs.  The legend needs to match the colors displayed on the graphs.  The graphs need to change display sets based on input values affecting their where clauses. The legend needs to have a predefined order that is non-alphabetical.

My plan is create a hardcoded color pattern set.

Assign the color pattern set to the legend. 

Order the legend by a database index key.

Make the legend only show the colors that are currently being used.

Sort the dataset and assign the color pattern to the dataset each time I change data subsets, when input values are changed.  (This is inefficient but appears to be the best way).

Add line charts (sgplot) and barcharts (gcharts)

I’m using SAS 9.2 and EG 5.1

🔒 This topic is solved and locked.

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

Discussion stats
  • 21 replies
  • 1119 views
  • 8 likes
  • 5 in conversation