2015-06-21 72 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