【问题标题】:Reference lines in time series plots时间序列图中的参考线
【发布时间】:2016-03-31 05:24:30
【问题描述】:

我想绘制一个时间序列图,指定 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

有人可以建议我应该遵循的方式吗?

编辑:

我现在想为每个确定的时间段对应的区域加阴影。

我尝试了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))

【问题讨论】:

    标签: plot stata


    【解决方案1】:

    这里使用的主要技巧是通过绘制两对点来添加每个线段,然后使用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)) 
    

    小点:

    1. 带有蓝色背景的默认 Stata 方案 s2color 很尴尬,除非您将它用于所有图形。在各种备选方案中,使用不同的方案是最简单的。

    2. 我研究过愚蠢的测量单位(谁更喜欢在图表上看到像 1.0e+06 这样的数字?)以及轴标题和标签。 (当它被标记为 2000 到 2013 时,谁需要解释“年”?)

    3. 使用图例来解释方法远非唯一甚至最好的选择。您可能更喜欢使用text() 添加文本。

    4. 您的 2004-2007 年和 2007-2012 年的时期重叠,我假设您的意思不是您所说的。如果您这样做了,那么更改代码就很容易了。

    5. 由于数据是年度总计,条形图也可能很有吸引力,但代价是必须从零开始(您可能更喜欢其他理由)。

    6. 如果你有水平线段,垂直线看起来是多余的,但你知道如何把它们放回去。

    编辑:对新问题的回应。

    使用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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 2018-09-11
      • 1970-01-01
      • 2014-12-17
      • 2021-04-26
      • 2020-04-26
      • 1970-01-01
      相关资源
      最近更新 更多