SAS Gplot覆盖线图

时间:2015-06-21 10:41:49

标签: plot sas

我试图在同一张图表上绘制两组折线图:

    /* 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 = 1或i = 2的数据,但是当我尝试在同一图表上绘制两个系列时,会出现两条额外的线条(在下面用粗略绘制的箭头突出显示) i = 1的系列的值,其中i = 2的第一个值。

enter image description here

如何防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

我想出了一个利用plot2和分类语法y*x=i的近似解决方案。恕我直言(在广泛的RTFM和技术论文搜索过程之后),您将所有图表放入一个图表的原始请求不能简单地完成,因为

  • by语句用于生成DISTINCT图。
  • 分类语法y*x=class_variable与绘图选项/ overlay不兼容。共存时会显示以下警告消息:
  

警告:指定的OVERLAY选项与Y * X = Z类型图冲突   请求。忽略OVERLAY选项

因此,唯一的选择是plot2。因此,此处的解决方案仅限于

  • 2个y轴变量(在这种情况下为y& z)
  • classfication变量的不同值的数量(在本例中为i)是无限的

修订后的代码和图表如下所示。您可以根据需要执行其他调整。但请注意,由于语法y*x=i的性质,图例不可避免地会发生变化。希望这个解决方案应该足够好。

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;