Stata - 散点图置信区间

时间:2014-08-21 04:08:38

标签: graph stata scatter-plot

我正在尝试连接两个点来表示以下数据集的置信区间。

Y          Y_upper_ci          Y_lower_ci           X
10           12                    8                1
20           22                    14               2
30           37                    22               3
40           42                    33               4
50           53                    48               5

我一直在使用以下内容。

twoway scatter Y Y_upper_ci Y_lower_ci X,  ///
  connect(l) sort ///
  title("Main Title") ///
  subtitle("Subtitle") ///
  ytitle(Y) ///
  xtitle(X)

我认为connect(l)会处理此事,但它只会连接Y而不是Y_upper_ciY_lower_ci

另外,如何让图例仅返回Y而不是Y_upper_ciY_lower_ci上的标签?

2 个答案:

答案 0 :(得分:6)

以下是几个选项:

// prepare some data
clear all
input Y          Y_upper_ci          Y_lower_ci           X
10           12                    8                1
20           22                    14               2
30           37                    22               3
40           42                    33               4
50           53                    48               5
end

// first graph
twoway rcap Y_upper_ci Y_lower_ci X, lstyle(ci) ||   ///
       scatter Y X, mstyle(p1)                       ///
       legend(order(2 "Y" ))                         ///
       note("with 95% confidence interval")          /// 
       name(rcap, replace)

enter image description here

// second graph
twoway rspike Y_upper_ci Y_lower_ci X, lstyle(ci) || ///
       scatter Y X, mstyle(p1)                       ///
       legend(order(2 "Y" ))                         ///
       note("with 95% confidence interval")          ///
       name(rspike, replace)

enter image description here

/// third graph
twoway rline Y_upper_ci Y_lower_ci X, lstyle(ci) ||  ///
       scatter Y X, mstyle(p1)                       ///
       legend(order(2 "Y" ))                         ///
       note("with 95% confidence interval")          ///
       name(rline, replace)

enter image description here

// fourth graph
twoway line Y_upper_ci Y_lower_ci X, lstyle(p2 p3) || ///
       scatter Y X, mstyle(p1)                        ///
       legend(order(3 "Y" ))                          ///
       note("with 95% confidence interval")          ///
       name(line, replace)

enter image description here

// fifth graph
twoway rarea Y_upper_ci Y_lower_ci X , astyle(ci) || ///
       scatter Y X, mstyle(p1)                       ///
       legend(order(2 "Y" ))                         ///
       note("with 95% confidence interval")          ///
       name(rarea, replace)

enter image description here

答案 1 :(得分:2)

这是阅读精细手册的东西。从

开始
clear 
input Y Y_upper_ci Y_lower_ci X
10     12       8         1
20     22      14         2
30     37      22         3
40     42      33         4
50     53      48         5
end 
twoway rcap Y_upper_ci Y_lower_ci X || scatter Y X, ytitle(Y) xtitle(X) legend(off) 

有些人更喜欢rspikercap。我建议legend(off)并在图片标题中添加合适的文字,以便为纸张提供。