Announcement

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

  • Multiple lines using lgraph

    Hi,
    I need help in drawing multiple lines using lgraph. I am a stata beginner and need help for my thesis.
    I have three y variables (ENV, SOC, CGV) all with the same units and year as x-variable.
    I use the following command to generate a line graph plotting mean of each variable in a given year:
    lgraph ENV YEAR
    lgraph SOC YEAR
    lgraph CGV YEAR

    Is there a way i show all the lines together in one graph?

    Thanks.

  • #2
    Welcome to the Stata Forum / Stalist.

    Please read the FAQ, mainly on how to share data and command.

    I gather - lgraph - is a user-written program, whose author is Timothy Mak.

    Share data - or at least a small parcel - increases the odds of a helpful reply.
    Best regards,

    Marcos

    Comment


    • #3
      I frequently use Timothy Lak's -lgraph- option with panel data. It uses the -collapse- option (with a hidden -preserve-) and draws a -connected- graph with the -by- option. The help file describes but provides no examples on how to add an additional plot to the graph. While I eventually figured it out, it is just as easy to use the -collapse- and -connected- options directly. Here's the way you would implement it for your problem.

      preserve
      collapse (mean) ENV SOC CGV , by(t)
      twoway (connected ENV YEAR, legend(all)) (connected SOC YEAR, legend(all)) (connected CGV YEAR, legend(all))
      restore

      Comment


      • #4
        I guess that you meant

        Code:
        preserve
        collapse (mean) ENV SOC CGV , by(YEAR)
        twoway (connected ENV YEAR, legend(all)) (connected SOC YEAR, legend(all)) (connected CGV YEAR, legend(all))
        restore
        as the collapse command (not an option) won't produce a variable YEAR if it didn't exist. (lgraph (from SSC) is also a command, while connected is a subcommand.)

        More interestingly, your syntax can be simplified further. Setting aside a preserve and restore, this reproducible example shows how:

        Code:
        webuse grunfeld, clear
        collapse invest mvalue kstock, by(year)
        twoway connected invest mvalue kstock year


        Comment


        • #5
          Thanks Nick--a much cleaner -twoway- statement. That "t" instead of "YEAR" was a typo.

          Comment

          Working...
          X