Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Local loop line graph

    Hello, I am trying to make a line graph with 25 lines, one for each department, but I keep getting 25 graphs with one line each. I want to find a way to make the one line graph or a way to overlay or overlap the 25 graphs. thank you in advance

    forvalues i = 1(1)25 {
    local lines line g_math año if departamento==`i'

    #delimit ;
    twoway
    (`lines'),
    xlabel(2008[1]2016)
    legend(off)
    ti("Evolución de puntajes")
    subti("Promedio a nivel departamento-año")
    yti("Puntaje en matemática (año base 2014)")
    xti("Año")
    xsize(7)
    ysize(10);
    #delimit cr

    graph save graf`i' , replace

    }

  • #2
    You will not succeed with this approach. This is one of the rare situations in Stata where a wide layout of the data is needed. What you need to do is -reshape- the data into wide layout, so that you have g_math1, g_math2, … g_math25, and then you can issue a single -graph twoway line- command with all of those as dependent variables. I assume your data set currently has a unique observation for each combination of departamento and
    año. So the code would go something like this:

    Code:
    reshape wide g_math, i(año) j(departamento)
    graph twoway line g_math* año
    You may need to modify this code to deal with other variables in the data set appropriately, and you will need to apply various options to the -graph twoway line- command similar to those you showed above.

    That said, it is pretty unusual data that will create a single graph panel of 25 lines that isn't just an unreadable "spaghetti graph."

    Comment


    • #3
      xtline should work here after xtset. The graph is likely to be a mess and the legend will gobble up a a lot of space but that is not the immediate question.

      Comment


      • #4
        Nick is right that -xtline- will do this. But there are two caveats I would add to his comment. First, to get the kind of graph Ms. Vidalon wants, she will need to use the -overlay- option with -xtline-, Without that, by default, -xtline- will also give her 25 separate panels. Also, in her original code, she specifies -legend(off)-, which she can also specify with -xtline-, and that will eliminate the legend itself and also mitigate the compression of the graph into a tiny band that the legend would create.

        Still, as both of us have pointed out, she should be careful what she wishes for, because I suspect she will not be happy once she gets it.

        Comment


        • #5
          You can also use "addplot" which can be downloaded from ssc. I think this would help.

          Comment


          • #6
            With 25 series I would look for a way to put 6 or 7 in each of 4 panels, or 12 or 13 in each of two. Much more detail in a paper in press for Stata Journal 19(4).

            Comment


            • #7
              Alternatively Maria could look at https://www.statalist.org/forums/for...nel-data-graph
              ---------------------------------
              Maarten L. Buis
              University of Konstanz
              Department of history and sociology
              box 40
              78457 Konstanz
              Germany
              http://www.maartenbuis.nl
              ---------------------------------

              Comment


              • #8
                Maarten's theme is echoed in other places, such as https://www.statalist.org/forums/for...ailable-on-ssc

                Comment

                Working...
                X