Announcement

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

  • Cannot display the second x-axis appropriately in twoway scatter

    Dear friends/helpers
    I'd like to show y vs x for two IDs in two lines, marking the x values of id1 in the x-axis(1) and the x values of id2 in the x-axis(2).
    However, I couldn't display them correctly. Your help and suggestions are really appreciated.

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(id visit_date) float(x y)
    1067 19478 19478         .
    1067 19541 19541         2
    1067 19604 19604       1.5
    1067 19674 19674 1.3214285
    1067 19744 19744  1.763158
    1067 19814 19814    1.8125
    1067 19877 19877 1.6842105
    1067 20052 20052 2.0853658
    1071 19464 19464         .
    1071 19534 19534         4
    1071 19597 19597 2.1052632
    1071 19695 19695 2.4848485
    1071 19786 19786  3.478261
    1071 19849 19849 4.0545454
    1071 19940 19940 4.0441175
    1071 20003 20003  3.922078
    end
    format %tdDD/NN/CCYY visit_date
    ------------------ copy up to and including the previous line ------------------
    l id visit_date x y if id==1067 | id==1071,clean
    tw sc y x if id==1067, c(l) ///
    xlabel(19541 19604 19674 19744 19814 19877 20052, ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) ///
    xsc(r(19400 20005)) ysc(r(1 4)) ylab(1(.5)4) sav(g1,replace)

    sc y x if id==1071, c(l) lcolor(blue) mcolor(blue) ///
    xlabel( 19534 19597 19695 19786 19849 19940 20003, ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) ///
    xsc(r(19400 20005)) ysc(r(1 4)) ylab(1(.5)4) sav(g2,replace)

    tw sc y x if id==1067, c(l) ///
    xlabel(19541 19604 19674 19744 19814 19877 20052, ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) ///
    xsc(r(19400 20005)) ysc(r(1 4)) ylab(1(.5)4) sav(g1,replace)

    sc y x if id==1071, c(l) lcolor(blue) mcolor(blue) ///
    xlabel( 19534 19597 19695 19786 19849 19940 20003, ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) ///
    xsc(r(19400 20005)) ysc(r(1 4)) ylab(1(.5)4) sav(g2,replace)

    graph combine g1.gph g2.gph, c(1) xsize(16) ysize(20)
    *they are correct, but want to overlay them with 2 x-axes in a single plot

    tw sc y x if id==1067, c(l) yaxis(1 ) xaxis(1 ) ///
    xlabel(19541 19604 19674 19744 19814 19877 20052, ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) ///
    xsc(r(19400 20005)) ysc(r(1 4)) ylab(1(.5)4) ///
    || sc y x if id==1071, c(l) yaxis(1) xaxis(2) lcolor(blue) mcolor(blue) ///
    xlabel( 19534 19597 19695 19786 19849 19940 20003, ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) legend(off)
    * the x range is incorrect, the x-axis(2) not shown appropriately

    Thank you.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(id visit_date) float(x y)
    1067 19478 19478         .
    1067 19541 19541         2
    1067 19604 19604       1.5
    1067 19674 19674 1.3214285
    1067 19744 19744  1.763158
    1067 19814 19814    1.8125
    1067 19877 19877 1.6842105
    1067 20052 20052 2.0853658
    1071 19464 19464         .
    1071 19534 19534         4
    1071 19597 19597 2.1052632
    1071 19695 19695 2.4848485
    1071 19786 19786  3.478261
    1071 19849 19849 4.0545454
    1071 19940 19940 4.0441175
    1071 20003 20003  3.922078
    end
    format %tdDD/NN/CCYY visit_date
    
    tw sc y x if id==1067, c(l) yaxis(1 ) xaxis(1 ) ///
    xlabel(19541 19604 19674 19744 19814 19877 20052, ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) ///
    xsc(r(19400 20020) axis(2)) ysc(r(1 4)) ylab(1(.5)4) ///
    || sc y x if id==1071, c(l) yaxis(1) xaxis(2) lcolor(blue) mcolor(blue) ///
    xlabel( 19534 19597 19695 19786 19849 19940 20003, axis(2) ///
    format("%tdDD/NN/CCYY") angle(45) labsize(vsmall)) legend(off)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	49.5 KB
ID:	1735342

    Comment


    • #3
      The dates are not that far apart, so I would consider a common axis for both lines. Then you can place labels at the top and bottom as you wish.

      Comment


      • #4
        Hi Andrew, thank you so much for the help! Thank you very much for your time and the suggestion.
        I couldn't find my thread when I started today, so re-posted it. Sorry about that. Cheers,
        Lisa

        Comment

        Working...
        X