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: appending graphs

From   David Elliott <>
Subject   Re: st: appending graphs
Date   Sun, 27 Jun 2010 10:59:18 -0300


What you need to do is accumulate the code for each plot line in a macro:

====begin code====
// number of colors below must be >= max # of subjects
local su_color cranberry navy midgreen dkorange sand teal red ltblue

quietly levelsof school, local(schools) // will contain school "1" "2" "3" ...

foreach sc of local schools {
  quietly levelsof  subject, local(subjects)  // will contain subject
"1" "2" "3" ...
  local gr_code  // reset empty
  foreach su of local subjects {
    // change code depending on whether school and subject variables
are numeric or string
    // note how we get the lines different colors
    local colorstyle : word `su' of `su_color'
    local gr_code tsline mean if school==`sc' & subject==`su' ,
lcolor(`colorstyle') || `gr_code'
  twoway `gr_code' /*, twoway options you want go here*/
====end code====

You don't append the graphs, you append the graph code BEFORE the
graph command.  I do this all the time and it works a charm for those
situations where you have variable numbers of plotted lines.  Note how
you can get each subject to always show up with it's own color line -
great for getting consistent legends across graphs.  And speaking of
legends, you can use the same process to accumulate code for a legend

DC Elliott

On 25 June 2010 16:23, Lim,  Raymond <> wrote:
> Hi Statalisters,
> Is there a way to append graphs? I want to automate producing graphs by a categorical variable, with with different number of lines.
> For example, Say you are creating time series graph of GPA by school and by subject (subject 1...20, each school offering a combination of them). Thus for each school, you have a graph with 3 to 5 different lines representing GPA in different subjects across time. The command would be something along the lines of
> #delimit ;
> tsset school year;
> twoway (tsline mean if school==1 & subject==7)
>         (tsline mean if school==1 & subject==8)
>         (tsline mean if school==1 & subject==9);
> twoway (tsline mean if school==2 & subject==4)
>         (tsline mean if school==2 & subject==8);
> twoway (tsline mean if school==2 & subject==1)
>         (tsline mean if school==2 & subject==2);
> Etc...
> I want to write a loop that will add lines to the graph as long as there are subjects that still need to graphed. I was thinking about using a for-loop, but I need to repeat "(tsline mean if school==__ & subject==__)" until all the subjects are accounted for.
> Thanks for any input!
> -Raymond
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

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