Announcement

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

  • How to add reference lines (yline) for different ID?

    Hi all in Statalist,

    I need help with having two reference lines in the same plot.

    In our study, we analyzed blood samples (with Sampletype (=Sample) in duplicates (Run 1 and 2) in a reference analyzer (X) and a test analyzer (Y). T3, MoK, LyS etc. are analytes measured in the blood samples by the analyzers. We need to show that the results lie within reference limits. I hereby attach an example of my data:
    Code:
    Sample   Id   Sampleno    Run     Date    Day    T3_X     T3_Y   MoK_X  MoK_Y   LyS_X   LyS_Y    etc.
    ----------------------------------------------------------------------------------------------------
                                              
    001      X         1       1       23/1     1     89       90.4   31.2   32.1    12      12.3    
    001      X         1       2       23/1     1     92.8     91.5   31.9   31.3    13.1    12.5    
    002      X         2       1       23/1     1     86.4     83.1   30.4   30.2    12.3    12.5    
    002      X         2       2       23/1     1     86.7     84.9   31     30.5    12.2    12.7    
    003      X         3       1       23/1     1     84.7     85.4   31.1   31.5    12.1    12.9    
    003      X         3       2       23/1     1     85.8     86.1   31.6   31.1    12.8    12.7  
    001      Y         1       1       24/1     2     89       90.4   31.2   32.1    12      12.3    
    001      Y         1       2       24/1     2     92.8     91.5   31.9   31.3    13.1    12.5    
    002      Y         2       1       24/1     2     86.4     83.1   30.4   30.2    12.3    12.5    
    002      Y         2       2       24/1     2     86.7     84.9   31     30.5    12.2    12.7    
    003      Y         3       1       24/1     2     84.7     85.4   31.1   31.5    12.1    12.9    
    003      Y         3       2       24/1     2     85.8     86.1   31.6   31.1    12.8    12.7    
    etc.
    I plot the T3_X analyte according to the following:
    Code:
    twoway (scatter T3_X Day if Sample ==001 & Run==1), by(Id) play (Template_Background) ylabel(2(0.1)2.4, angle(h) grid) yline(2.04 2.28)
    The "yline" above is for the Id=X. The reference limits for Id=Y are 1.96 & 2.2 "ylabel(1.96 2.2). I wonder how to have the limits for both Id X & Y in the same plot? (For help: suggest that we run Id X from Day 1 to 20 and changed to Id Y from Day 21 to 30)

  • #2
    First, how you presented the data makes it difficult to replicate your example. -dataex- makes it easy for others to help.

    That said, there are two ways to proceed. Use the Graph Editor (or by the Editor by command line) to alter the values on one of the graphs. The other option is to produce each graph separately and the combine them.

    For example:
    Code:
    sysuse auto,clear
    //Example 1
    scatter price mpg, by(fore) xline(22 26 )
    gr_edit plotregion1.plotregion1[1]._xylines[2].z = 16
    gr_edit plotregion1.plotregion1[1]._xylines[1].z = 33
    
    //Example 2
    qui sum price
    local min = r(min)
    local max = r(max)
    local xlabel : variable label mpg
    local ylabel : variable label price
    scatter price mpg if fore == 1, name(gr2,replace) xtitle("") title(Foreign)  /// 
      ytitle("") ylabel(5000(5000)15000)  yscale(range(`min' `max')) xline(22 26) /// 
      yscale(off) xlabel(10(10)40)
    scatter price mpg if fore == 0, name(gr1,replace) xtitle("") title(Domestic) ///
     ylabel(5000(5000)15000) ytitle("")   yscale(range(`min' `max')) xline(16 33) /// 
     xlabel(10(10)40)
    graph combine gr1 gr2, name(gr3,replace) b1title(`xlabel') l1title("`ylabel'")

    Comment


    • #3
      Thank you Scott for the examples. I tried to modify the codes in your first example to fit my data but the plot did not change (I am a new Stata user). My data look like this:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte Sampletype str8 ID byte Run str19 Date byte Day float(T3 MoK)
      1 "Lot1" 4 "2018-11-15T16:23:23"  1 2.19 68.9
      1 "Lot1" 2 "2018-11-15T11:56:26"  1 2.16 68.5
      1 "Lot1" 1 "2018-11-15T10:12:22"  1 2.14 68.5
      1 "Lot1" 5 "2018-11-15T16:27:26"  1 2.16 68.4
      1 "Lot1" 3 "2018-11-15T11:58:36"  1 2.19 68.4
      1 "Lot1" 2 "2018-11-16T10:25:46"  2 2.19 68.5
      1 "Lot1" 3 "2018-11-16T15:33:08"  2 2.21 68.8
      1 "Lot1" 1 "2018-11-16T10:11:58"  2 2.21 68.7
      1 "Lot1" 4 "2018-11-16T15:34:13"  2 2.24   69
      1 "Lot1" 4 "2018-11-19T15:10:38"  3 2.18   69
      1 "Lot1" 3 "2018-11-19T15:09:39"  3 2.16 69.3
      1 "Lot1" 2 "2018-11-19T10:05:14"  3 2.15 68.9
      1 "Lot1" 1 "2018-11-19T10:04:05"  3 2.15 68.8
      1 "Lot1" 4 "2018-11-20T16:12:55"  4 2.22 69.3
      1 "Lot1" 1 "2018-11-20T08:34:00"  4  2.2 69.1
      1 "Lot1" 3 "2018-11-20T16:10:46"  4  2.2 69.5
      1 "Lot1" 2 "2018-11-20T08:34:57"  4 2.17 69.2
      1 "Lot1" 2 "2018-11-21T08:22:59"  5 2.16 69.4
      1 "Lot1" 4 "2018-11-21T15:50:52"  5 2.17 69.8
      1 "Lot1" 3 "2018-11-21T15:49:39"  5 2.19 69.5
      1 "Lot1" 5 "2018-11-21T15:52:59"  5 2.21 69.5
      1 "Lot1" 1 "2018-11-21T08:21:28"  5  2.2 69.3
      1 "Lot1" 3 "2018-11-22T16:42:36"  6  2.2 70.2
      1 "Lot1" 1 "2018-11-22T08:13:02"  6 2.17 69.4
      1 "Lot1" 4 "2018-11-22T16:43:54"  6 2.19 70.2
      1 "Lot1" 2 "2018-11-22T08:15:03"  6 2.23 69.4
      1 "Lot1" 3 "2018-11-23T15:25:24"  7 2.16 68.9
      1 "Lot1" 1 "2018-11-23T09:13:44"  7 2.13 68.9
      1 "Lot1" 2 "2018-11-23T09:14:42"  7 2.18 69.4
      1 "Lot1" 2 "2018-11-23T09:14:42"  7 2.18 69.4
      1 "Lot1" 4 "2018-11-23T15:26:57"  7 2.17   69
      1 "Lot1" 1 "2018-11-26T08:15:46"  8  2.2 69.7
      1 "Lot1" 2 "2018-11-26T08:16:50"  8 2.16 69.8
      1 "Lot1" 4 "2018-11-26T15:55:12"  8 2.17 69.4
      1 "Lot1" 3 "2018-11-26T15:53:19"  8 2.17 69.1
      1 "Lot1" 3 "2018-11-27T15:38:09"  9 2.21 69.2
      1 "Lot1" 4 "2018-11-27T15:39:45"  9  2.2   69
      1 "Lot1" 2 "2018-11-27T08:35:44"  9 2.15 69.2
      1 "Lot1" 1 "2018-11-27T08:33:02"  9  2.2 69.2
      1 "Lot1" 1 "2018-11-28T08:50:13" 10 2.17 69.2
      1 "Lot1" 2 "2018-11-28T08:51:11" 10 2.24 69.4
      1 "Lot1" 4 "2018-11-28T16:08:35" 10 2.15 69.5
      1 "Lot1" 3 "2018-11-28T16:07:34" 10 2.15 69.4
      1 "Lot1" 4 "2018-11-29T16:39:41" 11  2.2 69.7
      1 "Lot1" 3 "2018-11-29T16:38:44" 11 2.15 69.7
      1 "Lot1" 2 "2018-11-29T09:47:05" 11 2.18 69.5
      1 "Lot1" 1 "2018-11-29T09:45:16" 11 2.21 68.8
      1 "Lot1" 3 "2018-11-30T14:47:37" 12  2.2 69.3
      1 "Lot1" 1 "2018-11-30T09:04:59" 12 2.19 69.4
      1 "Lot1" 4 "2018-11-30T14:48:59" 12 2.16 69.6
      1 "Lot1" 2 "2018-11-30T09:07:11" 12 2.16 69.2
      1 "Lot1" 1 "2018-12-03T08:43:35" 13 2.19 69.7
      1 "Lot1" 2 "2018-12-03T08:44:45" 13 2.21 70.1
      1 "Lot1" 4 "2018-12-03T16:12:51" 13 2.16 70.3
      1 "Lot1" 3 "2018-12-03T16:11:45" 13 2.19 70.2
      1 "Lot1" 3 "2018-12-04T15:05:19" 14 2.11 70.5
      1 "Lot1" 2 "2018-12-04T08:47:16" 14 2.16 69.8
      1 "Lot1" 4 "2018-12-04T15:07:47" 14 2.15 70.4
      1 "Lot1" 1 "2018-12-04T08:44:53" 14 2.17 69.5
      1 "Lot1" 1 "2018-12-05T08:42:18" 15  2.2   70
      1 "Lot1" 4 "2018-12-05T15:10:28" 15 2.16   70
      1 "Lot1" 3 "2018-12-05T15:09:29" 15 2.15 70.1
      1 "Lot1" 2 "2018-12-05T08:43:17" 15 2.14 69.6
      1 "Lot1" 1 "2018-12-06T08:51:24" 16 2.18 69.8
      1 "Lot1" 4 "2018-12-06T16:02:35" 16 2.15 69.8
      1 "Lot1" 2 "2018-12-06T08:52:23" 16 2.16 69.8
      1 "Lot1" 3 "2018-12-06T16:01:32" 16 2.16 69.4
      1 "Lot2" 3 "2018-12-07T13:37:30" 17 2.09 62.2
      1 "Lot2" 3 "2018-12-07T13:37:30" 17 2.09 62.2
      1 "Lot2" 4 "2018-12-07T13:38:35" 17 2.11 62.5
      1 "Lot1" 2 "2018-12-07T08:18:48" 17 2.21 70.3
      1 "Lot1" 1 "2018-12-07T08:17:52" 17  2.2 70.2
      1 "Lot2" 3 "2018-12-13T15:51:12" 18 2.09   62
      1 "Lot2" 4 "2018-12-13T15:52:47" 18  2.1 62.2
      1 "Lot2" 2 "2018-12-13T08:30:14" 18  2.1 62.7
      1 "Lot2" 1 "2018-12-13T08:29:03" 18 2.12 62.7
      1 "Lot2" 1 "2018-12-14T08:17:51" 19 2.07 62.6
      1 "Lot2" 2 "2018-12-14T08:19:10" 19 2.11 62.7
      1 "Lot2" 3 "2018-12-14T15:39:34" 19 2.15 62.1
      1 "Lot2" 4 "2018-12-14T15:41:30" 19 2.12 61.9
      1 "Lot2" 3 "2018-12-17T16:48:50" 20 2.11 62.4
      1 "Lot2" 4 "2018-12-17T16:50:07" 20 2.12 62.3
      1 "Lot2" 2 "2018-12-17T07:56:39" 20  2.1 63.3
      1 "Lot2" 1 "2018-12-17T07:53:27" 20 2.08   63
      1 "Lot2" 4 "2018-12-18T15:40:59" 21  2.1   63
      1 "Lot2" 2 "2018-12-18T07:58:58" 21 2.09 62.8
      1 "Lot2" 1 "2018-12-18T07:57:22" 21 2.07 62.4
      1 "Lot2" 3 "2018-12-18T15:38:23" 21 2.09 63.1
      1 "Lot2" 2 "2018-12-19T08:37:55" 22 2.12 63.6
      1 "Lot2" 4 "2018-12-19T14:39:19" 22 2.07 63.1
      1 "Lot2" 3 "2018-12-19T14:38:12" 22  2.1 63.3
      1 "Lot2" 1 "2018-12-19T08:36:43" 22 2.08 63.6
      1 "Lot2" 4 "2018-12-20T15:01:38" 23 2.12   63
      1 "Lot2" 3 "2018-12-20T15:00:12" 23 2.04 63.1
      1 "Lot2" 1 "2018-12-20T08:12:06" 23 2.08 63.9
      1 "Lot2" 2 "2018-12-20T08:13:04" 23  2.1 64.1
      1 "Lot2" 1 "2018-12-21T07:32:59" 24 2.07 63.4
      1 "Lot2" 1 "2018-12-21T07:32:59" 24 2.07 63.4
      1 "Lot2" 2 "2018-12-21T07:34:01" 24 2.07 63.6
      end
      label values Sampletype Sampletype
      label def Sampletype 1 "Control L", modify
      I plot T3 and Day according to the following:
      Code:
      twoway (scatter T3 Day if Sampletype==1 & Run==1), ylabel(2(0.1)2.3) yline(2.04 2.28)
      Lot1 has the limits 2.04 and 2.28 specified in the code. Lot2 has another limits (1.98 and 2.22). I need to have both limits in one graph with and without "by (ID)".
      Thank you in advance.

      Comment


      • #4
        Thanks for the -dataex- example. Below is one way; note, I extended the y-range otherwise the reference line would be outside the graph area:

        Code:
        twoway (scatter T3 Day if Sampletype==1 & Run==1), ylabel(2(0.1)2.3) /// 
             yscale(range(1.98 2.3)) yline(2.04 2.28) by(ID, note(""))
        gr_edit plotregion1.plotregion1[2]._xylines[2].z = 2.22
        gr_edit plotregion1.plotregion1[2]._xylines[1].z = 1.98

        Comment


        • #5
          Here's another way to approach the problem. See https://www.stata-journal.com/sjpdf....iclenum=gr0023 for more on the use of separate for this kind of problem.

          Code:
          separate T3, by(ID) veryshortlabel
          
          scatter T3? Day if Sampletype==1 & Run==1, ms(Oh +) mc(red blue) ylabel(1.95(0.05)2.25, ang(h)) ///
          yscale(r(1.92 .)) ///
          text(1.95 9 "Lot 1", color(red)) text(1.95 21 "Lot 2", color(blue)) ///
          || scatteri 2.04 1 2.04 17, recast(line) lc(red)    ///
          || scatteri 2.22 1 2.22 17, recast(line) lc(red)    ///
          || scatteri 2.22 18 2.22 24, recast(line) lc(blue)  ///  
          || scatteri 1.98 18 1.98 24, recast(line) lc(blue) ytitle(T3 (some units go here)) ///
          legend(off) xtitle(Day) xla(1(3)22)

          Comment


          • #6
            Thank you Scott and Nick for your help! I've learned a lot from your valuable codes.

            Comment

            Working...
            X