Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: st: plot yline for different values on each sub plot

From   Timothy Colbourn <>
To   Statalist <>
Subject   Re: Re: st: plot yline for different values on each sub plot
Date   Tue, 19 Apr 2011 11:28:06 +0200

Thanks Nick,

This method (plotting several twoway graphs at one - some bar and one a line) works well. For those interested my new code is:

twoway (bar livebirth month, barw(0.4)) (bar stillbirth month, barw(0.4)) (bar nnd month, barw(0.4)) ///
	(bar mvital month, barw(0.4)) (line expbirths month, lcolor(red)), ///
	ylabel(0(50)350, labsize(small) angle(0)) ytitle("frequency") xlabel(569(2)611, labsize(vsmall) angle(90)) ///
	xtitle("Month") by(, title("Births and deaths in each RCT arm", size(small)) note("")) ///
	legend(order(1 "Livebirths" 2 "Stillbirths" 3 "Neonatal Deaths" 4 "Maternal Deaths" 5 "Expected Births") size(vsmall)) by(arm)

The only issue is that instead of the 4 bars for each of the different bar variables (livebirth, stillbirth, nnd and mvital) being plotted beside each other for each month they are plotted on top of each other. Although I think I may remove stillbirth, nnd and mvital from these graphs anyway as they are small numbers, so it is not an issue for me at this time..


Tim Colbourn
Research Fellow and part-time PhD student,
Centre for International Health and Development,
UCL Institute of Child Health,
30 Guilford Street,

based full-time in Malawi
Tel: Office: +265 1 758 476
       Mobile: +265 888 159 231

> Date: Fri, 15 Apr 2011 11:57:32 +0100
> From: Nick Cox <>
> Subject: Re: st: plot yline for different values on each sub plot
> I couldn't get the editing approach to work. That just means what it
> says: I am not at all familiar with all that the Graph Editor can so.
> This code indicates some technique that might help. Most of it just
> sets up an example. The key idea is that a different horizontal line
> can be drawn for a variable that is constant in each panel. It is not
> a -yline()- and is drawn only over the range of the data.
> sysuse auto, clear
> contract foreign rep78, zero
> graph bar (asis) _freq , over(rep78) by(foreign)
> egen mean = mean(_freq), by(foreign)
> twoway bar  _freq rep78, barw(0.5) xla(1/5) || line mean rep78, by(foreign)
> On Fri, Apr 15, 2011 at 10:16 AM, Nick Cox <> wrote:
>> I don't know an easy way of doing this. You might try plotting all
>> four lines in all panels and then deleting three you don't need in the
>> Editor. Or you might draw each graph separately and then use -graph
>> combine-. Or you might rewrite using -twoway bar-.
>> Either way, your macro manipulation is not going to work as you
>> intend. Stata interprets your
>> `=    '
>> stuff precisely once and _before_ the -graph- command gets to work. It
>> certainly does not do that separately for each separate graph, and it
>> will always interpret a reference to a variable as a reference to its
>> value in the first observation.
>> Of course, wanting a different yline in each panel is a perfectly
>> reasonable request, but I think you will need to do it in one of the
>> ways mentioned above.
>> Nick
>> On Fri, Apr 15, 2011 at 9:56 AM, Timothy Colbourn
>> <> wrote:
>>> I have a bar graph plotted 4 times in one for each different arm of the trial I am analysing: by(arm)
>>> I have ploted a yline showing the expected number of births in each arm: yline(`=expbirths') which is a reference against the observed births bars plotted for each month (x axis). The problem is that Stata plots the yline on each of the 4 sub plots at the same value - the value for the first sub plot (arm) - rather than at the value for each arm on each subplot. I have tried yline(`=expbirths by(arm)')  but it does the same thing (with no error message - i.e. it essentially ignores the by(arm). The full syntax for the graph is:
>>> graph bar (asis) livebirth stillbirth nnd mvital, over(month, label( labsize(vsmall) angle(90))) ///
>>>        ylabel(0(50)550, labsize(small) angle(0)) yline(`=expbirths (arm)') ytitle("frequency") ///
>>>        by(, title("Births and deaths in each RCT arm", size(small)) note("")) ///
>>>        legend(order(1 "Livebirths" 2 "Stillbirths" 3 "Neonatal Deaths" 4 "Maternal Deaths") size(vsmall)) by(arm)
>>> Is it possible to plot the yline for each value of expbirths on each subplot?

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index