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: st: Fw: Foreach loop for graphs: ylabel & legend box

From   Friedrich Huebler <>
Subject   Re: st: Fw: Foreach loop for graphs: ylabel & legend box
Date   Thu, 10 Jan 2013 11:54:31 -0500


You can add space at the top of the graph by examining the range of
values in your y-variable. In the example below the range of the
vertical axis is extended by 20% of the maximum value of mpg. You
still have to add labels but that can be automated as well.

. sysuse auto
. collapse mpg, by(rep78)
. sum mpg
. local ymax = ceil(r(max)/5*6)
. twoway line mpg rep78, yscale(r(0 `ymax')) legend(on ring(0) pos(11))


On Thu, Jan 10, 2013 at 11:19 AM, Jacob Järås <> wrote:
> Dear Stata list members,
> I'm experiencing certain difficulties in parts of the code below when I
> try to create an automated process for producing graphs.
> I'm trying to plot incidence and mortality for various types of cancer by
> icd7 for both men and women in the same graph.
> Altogether there are about 50 graphs.
> The first problem that I encounter is that when I'm making a graph of, for
> example, prostate cancer, the labels of women appear in the plot region.
> I'd like to show only the labels of men and have the labels of women
> removed automatically, since there isn't and line corresponding to it in
> the graph.
> Similarly, the same happens whenever there is a type of cancer that only
> occurs to women. The labels of men are shown in the plot region
> as well as the labels of women and I'd like to remove the labels of men.
> The second issue that I'm trying to solve has to do with the range of the
> values on the axis of y since the number of new cases per year depends
> very much on the type of cancer.
> I want to reserve space on the upper left corner of the plot region for
> the legend box, for consistency. I try to make this happen by using the
> "Suggest # of ticks" in "Axis properties".
> Apparently this doesn't work very well because it seems like whenever
> there is a decreasing trend, there just isn't enough space for both, the
> legend and the lines, at the same time, since the time series
> trend line start on the same area where I have the legend box. The result
> is that a part of the beginning of the line end up hidden behind/under the
> legend box.
> So I'd like to find a way to reserve space or a portion of the height of
> the graph on "ring(0) position(11)" without having any trend lines hidden
> below the legend box, regardless of where the line begins.
> If anyone have suggestions or ideas on the above it would be greatly
> appreciated!
> --------------------------------------------------------------------------------------------------------------------
> levelsof icd7, local(levels)
> local j=2
> foreach i of local levels {
> graph twoway (line inc year if sex==1 & filter==1 & icd7==`i',
> color(black) lwidth(vthin) lpattern(solid)) || ///
>         (line inc year if sex==2 & filter==1 & icd7==`i', color(black)
> lwidth(medthick) lpattern(solid)) || ///
>         (line mort year if sex==1 & filter==1 & icd7==`i', color(black)
> lwidth(medthin) lpattern(longdash))  || ///
>         (line mort year if sex==2 & filter==1 & icd7==`i', color(black)
> lwidth(medthick) lpattern(dash)), ///
> legend(size(small) color(black) position(11) ring(0) cols(1)
> region(lcolor(white)) symxsize(10) rowgap(.3) ///
>         label(1 "Incidence men") label(2 "Incidence women") label(3
> "Mortality men") label(4 "Mortality women")) ///
> xlabel(1955(5)2015, labsize(small)) xtitle("Year",size(medsmall)
> margin(small)) ///
> ylabel(#12, labsize(small) format(%3.0f) angle(horizontal) grid gmin gmax
> ///
>         glwidth(thin) glcolor(gs13) glpattern(longdash)) ///
> ytitle("per 100 000", size(medsmall) margin(small))
> graphregion(color(white)) ///
> caption("{it:Figure `j'a: `:lab (icd7) `i''}", size(medsmall))
> graph save "Path\Figure `j'a - `:lab (icd7) `i''.gph", replace
> local j=`j'+1
> }
> --------------------------------------------------------------------------------------------------------------------
> Thank you very much
> Jacob

*   For searches and help try:

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