2016-03-31 24 views
1

我想繪製一個時間序列圖,指定兩個X軸的參考線,分別爲X & Y軸。我可以只顯示時間(X)軸的參考線(如下圖所示)。時間序列圖中的參考線

我用的命令是twoway (tsline egg_prod), tline(2004 2007 2012)

現在我想展示的平均線各段。即2004 - 2007年間平均產蛋量爲2008 - 2012年的&。

我發佈了一個最小的數據集供您參考。以下是我用於dataex的代碼。

clear 
input int year long egg_production 
2000 918000 
2001 941000 
2002 886000 
2003 885012 
2004 874596 
2005 864552 
2006 901176 
2007 915600 
2008 1.0e+06 
2009 1.1e+06 
2010 1.1e+06 
2011 1.2e+06 
2012 1.2e+06 
2013 1.9e+06 
end 

有人可以建議我應該遵循的方式嗎?

enter image description here

編輯:

我現在想樹蔭對應於每個標識的時間段的區域。

我試過recast(area)選項,但遇到一些問題。

1)我想要陰影區域觸摸陰謀的頂部邊緣&。我無法找到一個方法。

2)我不想看到陰影區域的圖例。所以我使用了legend(off),但這意味着與平均值相關的圖例也被省略了。你能否建議一種方法來找出這些問題?

graph twoway scatteri 2 2004 2 2007, recast(area) fcolor(gs14) lcolor(maroon) legend(off) /// 
|| scatteri 2 2008 2 2012, recast(area) fcolor(gs14) lcolor(maroon) legend(off) /// 
|| connected egg year, tline(2004 2007 2008 2012) /// 
|| scatteri `mean1' 2004 `mean1' 2007, recast(line) /// 
|| scatteri `mean2' 2008 `mean2' 2012, recast(line) /// 
ytitle(Egg production (millions)) xtitle("") xla(2000(5)2010 2013) xtic(2001/2012) /// 
scheme(s2color) yla(, ang(h)) /// 
legend(order(2 "2004-07 mean `text1' m" 3 "2008-12 mean `text2' m") pos(11) ring(0) col(1)) 

回答

4

這裏使用的主要訣竅是通過繪製兩對點,然後使用twoway scatterirecast(line)連接它們添加的每個線段。你自然需要先計算手段。

clear 
input int year long egg_production 
2000 918000 
2001 941000 
2002 886000 
2003 885012 
2004 874596 
2005 864552 
2006 901176 
2007 915600 
2008 1.0e+06 
2009 1.1e+06 
2010 1.1e+06 
2011 1.2e+06 
2012 1.2e+06 
2013 1.9e+06 
end 

replace egg_production = egg_p/1e6 

su egg if inrange(year, 2004, 2007), meanonly 
local mean1 = r(mean) 
local text1 : di %3.2f `mean1' 
su egg if inrange(year, 2008, 2012), meanonly 
local mean2 = r(mean) 
local text2 : di %3.2f `mean2' 

twoway connected egg year /// 
|| scatteri `mean1' 2004 `mean1' 2007, recast(line) /// 
|| scatteri `mean2' 2008 `mean2' 2012, recast(line) /// 
ytitle(Egg production (millions)) xtitle("") xla(2000(5)2010 2013) xtic(2001/2012) /// 
scheme(s1color) yla(, ang(h)) /// 
legend(order(2 "2004-07 mean `text1' m" 3 "2008-12 mean `text2' m") pos(11) ring(0) col(1)) 

enter image description here

小點:除非您使用它的所有圖形

  1. 默認塔塔計劃s2color其藍色的背景是尷尬。各種替代方案中,使用不同的方案是最簡單的。

  2. 我測量的遲鈍單位(誰喜歡看像1.0E + 06的圖形?號)和軸標題和標籤的工作。 (2000年到2013年,誰需要解釋「年份」?)

  3. 使用圖例來解釋手段遠不是唯一甚至最好的選擇。您可能更願意使用text()添加文本。

  4. 你的月經2004-2007和2007-2012重疊,我假設你沒有說你說的話。如果你這樣做,更改代碼很簡單。由於數據是年度總數,因此條形圖可能也會有吸引力,代價是不得不從零開始(您可能更喜歡其他理由)。

  5. 如果你有水平線段,垂直線似乎是多餘的,但你知道如何將它們放回

編輯:應對新的問題。

使用plotregion(margin(zero))堅持陰影區域貫穿整個plotregion

legend()應該是on但您只需選擇要顯示哪些元素,現在是#4和#5。您現在可能想要移動圖例。

graph twoway scatteri 2 2004 2 2007, recast(area) fcolor(gs14) lcolor(maroon) /// 
|| scatteri 2 2008 2 2012, recast(area) fcolor(gs14) lcolor(maroon) /// 
|| connected egg year, tline(2004 2007 2008 2012) /// 
|| scatteri `mean1' 2004 `mean1' 2007, recast(line) /// 
|| scatteri `mean2' 2008 `mean2' 2012, recast(line) /// 
ytitle(Egg production (millions)) xtitle("") xla(2000(5)2010 2013) xtic(2001/2012) /// 
scheme(s2color) yla(, ang(h)) plotregion(margin(zero)) /// 
legend(order(4 "2004-07 mean `text1' m" 5 "2008-12 mean `text2' m") pos(11) ring(0) col(1))