• Print  |
  • Feedback  |

Knowledge Base


TS-421

HOW CAN I PLACE TWO CHARTS ON A PAGE WITH A SINGLE TITLE?

INPUT:

I want to place multiple charts on a page. All of the charts represent the same variables, but for different time periods, so I only need a single legend, rather than a separate legend for each chart. Is there a way to do this?

OUTPUT:

One way to produce multiple charts with a single legend is to create the charts with PROC GCHART and replay them in a panel with PROC GREPLAY. You can use AXIS and LEGEND statements along with PROC GCHART statement options to position the graphs and produce a single legend. The following example illustrates the process.

Suppose you have quaqrterly sales data for two store locations across two years. The data are read in as the variables CITY, YEAR, QUARTER, and SALES as follows:

   data sales;
      input city $char9. year quarter sales;
   cards;
   Midville    1991 1 20000
   Midville    1991 2 20000
   Midville    1991 3 18000
   Midville    1991 4 14000
   Midville    1992 1 25000
   Midville    1992 2 22000
   Midville    1992 3 21000
   Midville    1992 4 12000
   Farmville   1991 1 15000
   Farmville   1991 2 16000
   Farmville   1991 3 20000
   Farmville   1991 4 15000
   Farmville   1992 1 18000
   Farmville   1992 2 19000
   Farmville   1992 3 21000
   Farmville   1992 4 15000
   run;

You want to create a vertical bar chart for each year, with each bar representing the total sales for a city, with the sales subdivided by quarter. Although there will be two charts, each with its own axis you want a single legend, representing the values for QUARTER, and a single title.

To produce a single centered title and legend, you can create two separate graphs and replay them into a template containing a full-size panel, effectively overlaying the graphs. One graph includes the title, subtitle, and legend, and positions the chart to the left side of the output. The second graph has a subtitle, but no title or legend, and positions the bar chart on the right side. Steps for creating the graphs follow.

First, create a format to be used with the Quarter variable, and specify patterns for the bars in the charts.


   proc format;
    value quarterf
     1='Winter'
     2='Spring'
     3='Summer'
     4='Fall';
   run;
   pattern1 c=black v=s;
   pattern2 c=gray66 v=s;
   pattern3 c=grayaa v=s;
   pattern4 c=graycc v=s;

Now, use the VBAR statement in PROC GCHART to create a chart for 1991.


   goptions ftext=swiss;
   proc gchart;
      where year=1991;
      vbar city/subgroup=quarter sumvar=sales raxis=axis1 maxis=axis2 legend=legend1;
      axis1 origin=(15 pct, 20 pct) label=none;
      axis2 origin=(15 pct, 20 pct) length=30 pct label=none;
      legend1 cshadow=black frame label=none value=(j=l) shape=bar(5,2);
      Title h=8 pct 'Yearly Sales by Quarter';
      title2 h=5 pct m=(25 pct,+0 pct) '1991';
      format quarter quarterf.;
   run;

The statements and options above have the following effect:

  • The WHERE clause selects data for 1991 to be used in the chart.

  • AXIS statements set the origin of both axes and the length of the midpoint axis. The ORIGIN= option on the AXIS statement positions the graph on the left side of the page. The MAXIS and RAXIS options in the VBAR statement point to the AXIS statement definitions.

  • The LEGEND definition specifies a frame and shadow effect and suppresses the legend label. Although the AXIS specifications moved the bar chart to the left side of the graphics area, the legend remains centered. The LEGEND option in the VBAR statement causes the legend definition to be used.

  • The TITLE statement generates a centered main title, while the M= option in the TITLE2 statement positions the text '1990' over the bar chart.

The statements produce the following graph:

The following statements produce the second chart:


   where year=1992;
   vbar city/subgroup=quarter sumvar=sales raxis=axis1 maxis=axis2
      nolegend;
   axis1 origin=(60 pct, 20 pct) label=none;
   axis2 origin=(60 pct, 20 pct) length=30 pct label=none;
   title h= 8 pct ' ';
   title2 h=5 pct m=(70 pct, +0 pct) '1992';
   run;

The statements and options above have the following effect:

  • The WHERE clause selects data for 1992 to be used in the chart.

  • The AXIS1 and AXIS2 definitions set the origin and length of the axes to position the bars on the right side of the page. The vertical coordinates of the ORIGIN= parameters must match those in the first graph to ensure that the charts line up correctly.

  • The NOLEGEND option suppresses the legend for this chart. The overall legend was produced in the first chart.

  • The blank TITLE statement leaves room for the main title created in the previous graph, and the TITLE2 statement positions the text 1992 over the bar chart.

Note that you do not have to reinvoke the GCHART procedure to produce the second chart. RUN-group processing lets you submit a new VBAR statement along with a new WHERE clause and global statements. The statements above produce this graph:

You can then use PROC GREPLAY to display both graphs on the same page. In this case, use the template named WHOLE, from the SASHELP.TEMPLT catalog. The WHOLE template is a single panel that takes up the whole graphics area. Use the TREPLAY statement to replay both graphs into the same panel.


   proc greplay igout=gseg nofs;
   tc sashelp.templt;
   template whole;
   treplay 1:1 1:2;
   quit;

This produces the final graph:

By modifying the program slightly, you can display more than two charts with a single legend and title. You can also use the same approach to combine multiple graphs produced by other SAS/GRAPH procedures.