Hello
I am trying to produce a line graph (by group) which includes a table underneath with the number of observations in each of the groups. I would like this to be automated as much as possible to save me manually adding text to each of the graphs.
So far I have managed to get the information on to the graph by adding a straight scatter graph & using the marker labels to show the information I want (see the attached graph using dummy data - in practice my sample size varies by arm/week).
However, this text is still above the x-axis. Is there a way to get this table to show underneath the graph/axis? (like in the 2nd example attached from a journal). It also is a bit arbitrary to select where on the y-axis this data should show, so I wonder if I am missing something.
This is the code I have so far (using a dataset from the Stata website):
Thank you for any help
Bryony
I am trying to produce a line graph (by group) which includes a table underneath with the number of observations in each of the groups. I would like this to be automated as much as possible to save me manually adding text to each of the graphs.
So far I have managed to get the information on to the graph by adding a straight scatter graph & using the marker labels to show the information I want (see the attached graph using dummy data - in practice my sample size varies by arm/week).
However, this text is still above the x-axis. Is there a way to get this table to show underneath the graph/axis? (like in the 2nd example attached from a journal). It also is a bit arbitrary to select where on the y-axis this data should show, so I wonder if I am missing something.
This is the code I have so far (using a dataset from the Stata website):
Code:
set scheme white_tableau // installed from SSC (schemepack)
* use & refine
webuse pig.dta, clear
gen group = id>24
bys id (week): gen weight_change = weight - weight[1]
lab def group 0 "Control" 1 "Intervention"
lab val group group
* collapse
collapse (count) count=weight_change (median) med=weight_change (p25) p25=weight_change (p75) p75=weight_change, by(group week)
* y-axis where data table should appear
gen y0 = -5
gen y1 = -7
* graph (method 1)
twoway (connected med week if group==0, color(red)) ///
(connected med week if group==1, color(midblue)) ///
(rcap p25 p75 week if group==0, color(red)) ///
(rcap p25 p75 week if group==1, color(midblue)) ///
(scatter y0 week if group==0, m(i) mlabel(count) mlabpos(0)) ///
(scatter y1 week if group==1, m(i) mlabel(count) mlabpos(0)), ///
text(-5 -1 "Control:" -7 -1 "Intervention:", size(vsmall) place(e)) ///
ytitle("Median weight change (g), IQR") ///
xtitle("Week") ///
ylabel(0(20)60) ///
legend(order(1 "Control" 2 "Intervention"))
Thank you for any help
Bryony

Comment