Reputation: 495
I am trying to connect two dots to represent a confidence interval of the following dataset.
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
I have been using the following.
twoway scatter Y Y_upper_ci Y_lower_ci X, ///
connect(l) sort ///
title("Main Title") ///
subtitle("Subtitle") ///
ytitle(Y) ///
xtitle(X)
I thought connect(l)
would take care of this, but it only connects the Y and not the Y_upper_ci
to the Y_lower_ci
.
Also, how can I have the legend only return the label on Y
and not Y_upper_ci
and Y_lower_ci
?
Upvotes: 3
Views: 19215
Reputation: 2694
Here are several options:
// 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)
// 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)
/// 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)
// 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)
// 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)
Upvotes: 6
Reputation: 37208
This is read the fine manual stuff. Start with
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)
Some people prefer rspike
to rcap
. I'd recommend legend(off)
and putting suitable text in the Figure caption you would supply for a paper.
Upvotes: 2