Announcement

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

  • Help making complex graph

    Hi there, I'm relatively new to stata and I'm struggling to incorporate multiple variables into a single graph.

    The dataset consists of individuals who either have data from visit 1, visit 2, or both. I would like to plot age1 (from visit 1) against x1 and age2 against x2 (from visit 2). I would like to distinguish whether the data is from visit 1 or 2 by presenting visit 1 as a square and visit 2 as a circle. I would then like to connect the pair-wise longitudinal data points with a line. Finally, I would like to then colour each data-point according to another variable which can be either 0, 1 or 2 (for visit 1 this is y1, and for visit 2 this is y2).

    Is it possible to do this on stata and does anyone have any advice on how to do this?

    Many thanks in advance

  • #2
    Sounds possible but a data example is sorely and surely needed to allow experiment. Check out the paired-coordinate commands of twoway.

    Comment


    • #3
      This should get you started:

      Code:
      clear
      input id visit age x
      1 1 7.0 3
      1 2 8.0 4
      2 1 6.2 6
      2 2 7.1 3
      3 1 6.5 3
      3 2 8.0 6
      4 1 6.0 5
      end
      
      reshape wide x age, i(id) j(visit)
      
      twoway (scatter age1 x1, msymbol(S))(scatter age2 x2) ///
      (pcarrow x1 age1 x2 age2)
      Also, read the FAQ, and pay attention in following section 12.

      Comment


      • #4
        Thank you for your help. Having read the FAQs I have provided the additional info below for others.

        The data-set consists of 10 subjects. Each subject has a group, and has age, score, and level from two different time-points (example data is shown below).
        subject group age1 score1 level1 age2 score2 level2
        1 1 43 15 0 47 20 1
        2 1 32 35 1 33 40 2
        3 1 50 65 0 . . .
        4 0 26 35 4 30 38 4
        5 0 40 33 4 42 43 4
        6 1 18 12 0 23 18 0
        7 0 60 40 4 63 54 4
        8 1 65 34 0 69 39 1
        9 0 . . . 29 27 4
        10 1 88 9 0 90 14 0

        What I was aiming to achieve:
        1. Plot score vs age from both time-points on a single graph
        2. Plot data from time-point 1 as a square, and time-point 2 as a circle
        3. Represent level using the colour of the data-points (note that controls can't have a level which is specific to individuals with the disease, so will be left black)
        4. Join longitudinal data-points with a line (note not all data is longitudinal)

        How I made this plot with the help of the above comments:

        twoway ///
        (scatter score1 age1 if level1 == 0, msymbol(S) mcolor(orange)) ///
        (scatter score1 age1 if level1 == 1, msymbol(S) mcolor(green)) ///
        (scatter score1 age1 if level1 == 2, msymbol(S) mcolor(blue)) ///
        (scatter score1 age1 if group == 0, msymbol(S) mcolor(black)) ///
        (scatter score2 age2 if level2 == 0, mcolor(orange)) ///
        (scatter score2 age2 if level2 == 1, mcolor(green)) ///
        (scatter score2 age2 if level2 == 2, mcolor(blue)) ///
        (scatter score2 age2 if group == 0, mcolor(black)) ///
        (pcarrow score1 age1 score2 age2, lcolor(black) lwidth(thin) mcolor(black) mlwidth(thin) mangle(0)), ///
        ysize(6) xsize(6) graphregion(color(white)) plotregion(color(white) margin(zero)) xtitle("Age") ytitle("Score") xlabel() ylabel(, nogrid) name(score, replace) legend(off)

        Comment

        Working...
        X