Data visualization with SAS programming

How to display only legend entries dynamically in proc gchart

Accepted Solution Solved
Reply
SAS Employee
Posts: 15
Accepted Solution

How to display only legend entries dynamically in proc gchart

[ Edited ]

Hi,

 

I am creating graphs using Proc Gchart for which sub-group values are displayed in the legend. Since my sub-group sometimes stretch to 200 values and causing graph to throw warnings, I took the legend out of graph and displaying them separately in another blank graph with noaxes, noframe options. But since I am creating a graph here, it is taking the whole box width rather than adjusting to the number of legend entries dynamically. I mean to say, the graph box size that is displaying legend entries is always same whether it is 200 entries or just 3 entries. Please refer the attached word document with its screen shots that explains better. Since we are adding them in an RTF, I want to adjust the graph box size dynamically according to the number of legend entries rather than one size box like now. Can anyone help me with some tips here?

 

Please let me know if I am not clear with explaining the issue. Thanks in advance.

 

 

Regards

-ravib

 

 


Accepted Solutions
Solution
‎03-19-2016 08:26 PM
Grand Advisor
Posts: 10,204

Re: How to display only legend entries dynamically in proc gchart

It would help to show the code, including the legend statement used.

 

If I understand what you are doing then you have two calls to proc gchart. You may need to use GOPTIONS to change the display size with HSIZE and VSIZE between Proc statements. You would have to summarize your data to know how many elements are to be displayed to insure that the space is not too small. This may require a little bit of trial and error to determine guidlines for the size parameters based on that number. And don't forget to set the sizes back after the second graph.

 

I admit that I have a hard time visualizing what I assume is a bar chart with 200 sub groups that allows clear comparisons between whatever you may be summarizing.

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to display only legend entries dynamically in proc gchart

Hi,

 

Some questions:

Why do you need each account number to be a separate group?  This doesn't make sense as they should be grouped?

 

It can be done of course.  You just need to separate the values you want in each graph out, say into a loop data, then use that dataste to create each graph statement, e.g.;

loop table is:

6010456,5024564,12123233

...

Then

data _null_;

  call execute('proc gchart ...;

                       where account in ('||strip(loop_value)||');

                       ...;

                     runSmiley Wink;

run;

 

This will generate one graph statement for each observation in the loop dataset, where clausing out for each group given there.  Thats probably the simplest way. 

I would also suggest that move towards the more modern graphing procedures, sgplot and GTL.  You can find lots of examples and code: http://blogs.sas.com/content/graphicallyspeaking/

Solution
‎03-19-2016 08:26 PM
Grand Advisor
Posts: 10,204

Re: How to display only legend entries dynamically in proc gchart

It would help to show the code, including the legend statement used.

 

If I understand what you are doing then you have two calls to proc gchart. You may need to use GOPTIONS to change the display size with HSIZE and VSIZE between Proc statements. You would have to summarize your data to know how many elements are to be displayed to insure that the space is not too small. This may require a little bit of trial and error to determine guidlines for the size parameters based on that number. And don't forget to set the sizes back after the second graph.

 

I admit that I have a hard time visualizing what I assume is a bar chart with 200 sub groups that allows clear comparisons between whatever you may be summarizing.

SAS Employee
Posts: 15

Re: How to display only legend entries dynamically in proc gchart

Hi ballardw,

 

Thanks for the response and sorry if my previous explanation was not clear. You almost got the issue and thanks for your suggestions on creating HSIZE, VSIZE macro variables. I did the same way using 5 different number ranges for legend entries as an immediate solution. But since my input data has dynamic legend, I want to adjust its display area also dynamically instead of hard coding the graph sizes. So in this approach, I had to display only the legend separately if the number is exceeding 20 to avoid warnings.

I have added few more details in the attached document. Hope you get more clarity with my quesiton.

 

Please review and let me know if there is any better way to approach? Thanks once again.

 

 

Regards

ravib 

Super Contributor
Posts: 252

Re: How to display only legend entries dynamically in proc gchart

Hi Ravib,

 

I was just curious if you have thought about using SGPLOT with the hbar statement and keylegend statement? This is because the legends are easy to adjust there, that is the fonts can be adjusted, and so can the size of the symbols. There's also an option called maxlegendarea with allows more legend entries to be fitted it. I believe the legends are dynamically adjusted to an extent too.

 

 

 

 

I've also done something similar to you before where I created a plot of just the legends when the legend was taking up to much space.

 

Also FYI, here is an example of how just a legend can be created using GTL.

 

http://blogs.sas.com/content/graphicallyspeaking/2012/04/20/just-a-legend-please/

Super Contributor
Posts: 252

Re: How to display only legend entries dynamically in proc gchart

Hi Ravib,

 

Here is an example of creating legends in GTL. It doesn't adjust automatically as I initially though, but just showing you how the legend can be increased when there are fewer items.

data cars;
  set sashelp.cars;
run;

proc template;
  define statgraph legend;
    begingraph;
	  layout lattice / rows = 2 rowweights = (0 100);

        layout overlay;
		  scatterplot x = model y = horsepower / group = model name = "leg" markerattrs = (symbol = squarefilled);
	    endlayout;

		layout overlay;
		  discretelegend "leg" / autoitemsize = true;
	    endlayout;

	  endlayout;
	endgraph;
  end;
run;

/* 1 group - normal size legend */

proc sgrender data = cars template = legend;
  where make = "Acura" and model = " MDX";
run;  



proc template;
  define statgraph legend2;
    begingraph;
	  layout lattice / rows = 2 rowweights = (0 100);

        layout overlay;
		  scatterplot x = model y = horsepower / group = model name = "leg" markerattrs = (size = 30 symbol = squarefilled);
	    endlayout;

		layout overlay;
		  discretelegend "leg";
	    endlayout;

	  endlayout;
	endgraph;
  end;
run;

/* 1 group - larger symbol in legend */

proc sgrender data = cars template = legend2;
  where make = "Acura" and model = " MDX";
run;  




proc template;
  define statgraph legend3;
    begingraph;
	  layout lattice / rows = 2 rowweights = (0 100);

        layout overlay;
		  scatterplot x = model y = horsepower / group = model name = "leg" markerattrs = (size = 30 symbol = squarefilled);
	    endlayout;

		layout overlay;
		  discretelegend "leg" / valueattrs = (size = 20);
	    endlayout;

	  endlayout;
	endgraph;
  end;
run;

/* 1 group - larger symbol and text in legend */

proc sgrender data = cars template = legend3;
  where make = "Acura" and model = " MDX";
run;  
SAS Employee
Posts: 15

Re: How to display only legend entries dynamically in proc gchart

Hi,

 

Thanks for the response and inputs given. Since my graph has to use grouping and stacking, I opted for Proc Gchart which has relatively limited options in certain needs. And that's the reason I am using same Proc Gchart to display its corresponding legends separately. 

 

Regards

ravib

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 777 views
  • 2 likes
  • 4 in conversation