Announcement

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

  • xtline with color changed at a given point of time

    Hello,
    Suppose I have a dataset with person ID, day, and calories. I have another variable called ``treated'' which equals 1 if a person got some treatment and 0 otherwise.
    I am thinking of having an xtline graph by ID, but for each line, I want it to be of a different color for different values of ``treated''.
    Here is what I did:

    clear all
    sysuse xtline1.dta,clear
    keep if person<3
    *creating variable ``treated''
    bysort person:gen temp=_n
    drop if temp>100
    gen treated=0
    replace treated=1 if person==1 & temp>50
    replace treated=1 if person==2 & temp>70
    drop temp

    xtset person day, daily
    xtline calories,overlay

    And here is what I got: (see g1.png in the attachment)

    And here is what I want: (see g2).

    I searched online but got answer yet. Any suggestions will be appreciated.
    Thanks!
    Marco


    Attached Files

  • #2
    I think I might be able to do the job manually using the graph editor, but it would be great if I can use code. Thanks!

    Comment


    • #3
      Still waiting for answers!

      Comment


      • #4
        Perhaps the following will help:

        Code:
        xtline calories, overlay addplot(line calories day if treated, connect(a))

        Comment


        • #5
          Originally posted by Robert Picard View Post
          Perhaps the following will help:

          Code:
          xtline calories, overlay addplot(line calories day if treated, connect(a))
          Thanks! addplot looks like a very cool option!

          Comment


          • #6
            Originally posted by Robert Picard View Post
            Perhaps the following will help:

            Code:
            xtline calories, overlay addplot(line calories day if treated, connect(a))
            Hey Robert,
            What if there are two levels of treatment and I want a different color for each level?

            Now the code becomes:
            clear all
            sysuse xtline1.dta,clear
            keep if person<3
            *creating variable ``treated''
            bysort person:gen temp=_n
            drop if temp>100
            gen treated=0
            replace treated=1 if person==1 & temp>30
            replace treated=1 if person==2 & temp>50
            replace treated=2 if person==1 & temp>50
            replace treated=2 if person==2 & temp>70
            drop temp

            xtset person day, daily
            xtline calories,overlay

            Here is something I am trying to get:
            ​​​​​​​
            Attached Files

            Comment


            • #7
              you can "add" as many plots as you want in addplot, for example:

              xtline calories, overlay addplot(line calories day if treated==1, connect(a) || line calories day if treated==3, connect(a))

              Comment


              • #8
                Originally posted by Ariel Karlinsky View Post
                you can "add" as many plots as you want in addplot, for example:

                xtline calories, overlay addplot(line calories day if treated==1, connect(a) || line calories day if treated==3, connect(a))
                Thanks Ariel,
                However, I got an additional line between two individuals for treated==1 .(See attached please). Any idea how to remove it?
                Attached Files

                Comment


                • #9
                  One more way I can think of is just to use twoway graph, but this does not seem to be a very smart idea....

                  Comment


                  • #10
                    Any thoughts?

                    Comment


                    • #11
                      Try the following:

                      xtline calories, overlay addplot(line calories day if treated==1&person==1, lcolor(green) connect(a) || line calories day if treated==1&person==2, lcolor(green) connect(a)||line calories day if treated==2, lcolor(orange) connect(a))
                      Last edited by Andrew Musau; 31 Mar 2017, 16:14.

                      Comment


                      • #12
                        That was so important for me! Thanks guys!

                        Comment

                        Working...
                        X