user2568648
user2568648

Reputation: 3191

SAS Gplot overlay line plots

I am trying to plot two sets of line graphs on the same chart:

    /* make data */

data test ;
 do i = 1 to 2 ;
   do x = 1 to 5 ; 
      y = i*x ;
      z = 0.5*i*x ;
 output;
   end ;
 end ;
 run ;

 data test ;
 set test ;
 if i =1 then y = -1*y ;
 if i =1 then z = -1*z ;
 run ;

/* set graph style */

* X axis *; axis1 order = (0 to 5 by 1)
                  label = ("x" h=1)
                  minor = (number=4)
                  value = (h=1)
                  offset = (0,0);
* Y axis *; axis2 label = ("y/z" j=c h=1)
                  minor = (number=1)
                  value = (h=1)
                  offset = (0,0);

symbol1 color=BL interpol=join width=1.25 value="" line=20;
symbol2 color=VIB interpol=join width=1.25 value="" line=1;

legend1 position=(top right inside)
        value=("y" "z") 
        label=(position=top justify=center 'Var')                                                                                                                      
        across=1;

/* plot */

 proc gplot data=test;                                                                                                                 
   plot y*x z*x / overlay noframe vaxis=axis2 haxis=axis1 autovref legend=legend1
                                 cvref=ltgray autohref chref=ltgray lhref=33 lvref=33;                                                                                                   
   where i in (1,2) ;                                                                                                               
run;                                                                                                                                    
quit;

I can plot the data for either i=1 or i=2 without problems, but when I try to plot the two series on the same graph, two extra lines appear (highlighted with badly drawn arrows below) which link the last value of the series i=1 with the first values of i=2.

enter image description here

How can I prevent this from happening?

Upvotes: 1

Views: 2278

Answers (1)

Bill Huang
Bill Huang

Reputation: 4648

I came up with an approximate solution exploiting plot2 and the classification syntax y*x=i. IMHO (after an extensive process of RTFM and technical paper search), your original request of putting all the plots into one graph cannot be simply done since

  • The by statement is designed for producing DISTINCT graphs.
  • The classification syntax y*x=class_variable is INCOMPATIBLE with plot option / overlay. The following warning message is displayed when they coexist:

WARNING: OVERLAY option specified conflicts with Y*X=Z type plot request. OVERLAY option ignored

Therefore, the only option left is plot2. Hence the solution here is limited to

  • 2 y-axis variables (y & z in this case)
  • the number of distinct values of the classfication variable (i in this case) is unlimited

The revised code and graph are presented below. You can perform miscellaneous adjustments as you wish. However, note that the legends are inevitably changed due to the nature of syntax y*x=i. Hopefully this solution should be good enough.

result

/* make data */

data test ;
    do i = 1 to 2 ;
        do x = 1 to 5 ;
            y = i * x ;
            z = 0.5 * i * x ;
            output;
        end ;
    end ;
run;

data test ;
    set test ;
    if i =1 then y = -1*y ;
    if i =1 then z = -1*z ;
run ;

/* set graph style */

* X axis *; axis1 order = (0 to 5 by 1)
                  label = ("x" h=1)
                  minor = (number=4)
                  value = (h=1)
                  offset = (0,0);
* Y axis *; axis2 order = -5 to 10 by 1
                  label = ("y/z" j=c h=1)
                  minor = (number=1)
                  value = (h=1)
                  offset = (0,0);

* Y axis for plot2 (hidden, same scale as axis2) * ;
            axis3 order = -5 to 10 by 1
                  label = (" ")
                  minor = none
                  major = none
                  value = none
                  offset = (0,0);

symbol1 color=BL interpol=join width=1.25 value="" line=20;
symbol2 color=VIB interpol=join width=1.25 value="" line=1;

/* legend changed */
legend1 position=(top right inside)
        value=("i=1" "i=2") 
        label=(position=top justify=center 'y')
        across=1;

/* extra settings added for plot2 */
symbol3 color=GREEN interpol=join width=1.25 value="" line=20;
symbol4 color=RED interpol=join width=1.25 value="" line=1;

legend2 position=(top left inside)
        value=("i=1" "i=2")
        label=(position=top justify=center 'z')
        across=1;

/* plot */

proc gplot data=test;
    plot y*x=i / noframe vaxis=axis2 haxis=axis1 autovref legend=legend1
                   cvref=ltgray autohref chref=ltgray lhref=33 lvref=33;
    plot2 z*x=i / noframe vaxis=axis3 legend=legend2;
run;
quit;

Upvotes: 2

Related Questions