2015-06-21 68 views
0

我试图绘制在同一图表上的两组线图:SAS Gplot覆盖线图

/* 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

我想出了一个近似的解决方案,利用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; 
+0

感谢。将“color = ltgray style = 33”选项添加到axis3参数“完全隐藏”第二个y轴。 – user2568648