Announcement

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

  • Equal spacing between marker ticks

    Hello Stata List forum members,

    I am creating a two-way line graph in STATA 17.0 using a continuous variable on the y-axis and a coded categorical variable on the x-axis. I am having trouble with the spacing on the x-axis where the ticks are not evenly spaced and I can't seem to find a way to make them even. Would anyone be able to help with this? This is the code I am using:

    Code:
    drop if Objective!="B2a" 
    drop if NfL==.
    
    local B2bNfLylab="0(20)100"
    local axislabelformat labsize(medium) tlwidth(medthick) angle(0)
    local B2axlab = "5 6 12 13"
    local B2axaxis xlabel(5 "0d" 6 "3d" 12 "0d" 13 "3d")
    local B2aNfLy="0 70"
    local axislabelformat labsize(medium) tlwidth(medthick) angle(0)
    
    twoway (line NfL sample_delay1 if disease_grp==0, c(L) lcolor("`INF_grey'")) ///
            (line NfL sample_delay1 if disease_grp==1, c(L) lcolor("`INF_gold'")) ///
            (line NfL sample_delay1 if disease_grp==2, c(L) lcolor("`INF_Red'")),  ///
            ylabel(`B2aNfLylab', `axislabelformat') ///
            xlabel(`B2axlab', valuelabel `axislabelformat') ///
            `B2axaxis' ///
            yscale(range(`B2aNfLy')) ///
            legend(off) ytitle("NfL pg/mL") xtitle("Sample delay") /// 
            `axislineformat' /// 
            name(practice_Spag,replace) 
            
            gr_edit .plotregion1.style.editstyle boxstyle(linestyle(color(none))) editcopy
    I have attached an image of the graph and here is sample data to use:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5 Objective double NfL str7 ID_visit float process_delay str7 sample_delay long sample_delay1 float disease_grp
    "B2a" 6.78976348216652 "019_002" 0 "Ven_S_0" 12 0
    "B2a" 6.05453975047093 "019_002" 3 "Ven_S_3" 13 0
    "B2a" 6.43117929188989 "019_002" 0 "Cap_S_0"  5 0
    "B2a" 7.77724284703385 "019_002" 3 "Cap_S_3"  6 0
    "B2a" 46.0262954338041 "023_000" 0 "Ven_S_0" 12 2
    "B2a" 45.3400963334667 "023_000" 3 "Ven_S_3" 13 2
    "B2a" 42.1146407423491 "023_000" 0 "Cap_S_0"  5 2
    "B2a" 44.5097361884643 "023_000" 3 "Cap_S_3"  6 2
    "B2a" 6.50151907855725 "034_002" 0 "Ven_S_0" 12 0
    "B2a" 6.69151993456443 "034_002" 3 "Ven_S_3" 13 0
    "B2a"  6.6533028955191 "034_002" 0 "Cap_S_0"  5 0
    "B2a" 6.70771212362222 "034_002" 3 "Cap_S_3"  6 0
    "B2a" 6.10433716670134 "035_001" 0 "Ven_S_0" 12 0
    "B2a"  5.7426620129731 "035_001" 3 "Ven_S_3" 13 0
    "B2a" 6.40042989719446 "035_001" 0 "Cap_S_0"  5 0
    "B2a" 7.40253128905567 "035_001" 3 "Cap_S_3"  6 0
    "B2a" 17.0881359385239 "039_000" 0 "Ven_S_0" 12 1
    "B2a" 17.5497884659482 "039_000" 3 "Ven_S_3" 13 1
    "B2a" 15.1326740035172 "039_000" 0 "Cap_S_0"  5 1
    "B2a" 17.6452026407081 "039_000" 3 "Cap_S_3"  6 1
    "B2a" 14.5800076367392 "040_000" 0 "Ven_S_0" 12 1
    "B2a" 15.1644764097372 "040_000" 3 "Ven_S_3" 13 1
    "B2a" 14.5638184570952 "040_000" 0 "Cap_S_0"  5 1
    "B2a" 17.5479438185644 "040_000" 3 "Cap_S_3"  6 1
    "B2a" 16.0990814088212 "042_000" 0 "Ven_S_0" 12 1
    "B2a" 15.9458315219729 "042_000" 3 "Ven_S_3" 13 1
    "B2a" 15.3864839671494 "042_000" 0 "Cap_S_0"  5 1
    "B2a" 15.0329304453604 "042_000" 3 "Cap_S_3"  6 1
    "B2a" 38.8828240890249 "048_000" 0 "Ven_S_0" 12 1
    "B2a" 42.7880049447568 "048_000" 3 "Ven_S_3" 13 1
    "B2a" 35.0047203335076 "048_000" 0 "Cap_S_0"  5 1
    "B2a" 39.1660922425035 "048_000" 3 "Cap_S_3"  6 1
    "B2a" 19.2651316050738 "050_000" 0 "Ven_S_0" 12 1
    "B2a" 20.4256461574558 "050_000" 3 "Ven_S_3" 13 1
    "B2a" 17.5064017366281 "050_000" 0 "Cap_S_0"  5 1
    "B2a" 19.7032597838733 "050_000" 3 "Cap_S_3"  6 1
    "B2a" 17.4070376465001 "054_000" 0 "Ven_S_0" 12 0
    "B2a" 17.7275810869912 "054_000" 3 "Ven_S_3" 13 0
    "B2a" 15.9835372637685 "054_000" 0 "Cap_S_0"  5 0
    "B2a" 16.3277934086917 "054_000" 3 "Cap_S_3"  6 0
    "B2a"  45.221089826051 "055_000" 0 "Ven_S_0" 12 2
    "B2a" 46.9620374658654 "055_000" 3 "Ven_S_3" 13 2
    "B2a"  47.770735349467 "055_000" 0 "Cap_S_0"  5 2
    "B2a" 48.6682827654008 "055_000" 3 "Cap_S_3"  6 2
    "B2a" 17.2000014535273 "056_000" 0 "Ven_S_0" 12 0
    "B2a" 17.5059862305317 "056_000" 3 "Ven_S_3" 13 0
    "B2a" 16.2012062130292 "056_000" 0 "Cap_S_0"  5 0
    "B2a" 17.3096523454328 "056_000" 3 "Cap_S_3"  6 0
    "B2a" 66.5034806350149 "057_000" 0 "Ven_S_0" 12 2
    "B2a" 68.8328046878072 "057_000" 3 "Ven_S_3" 13 2
    "B2a" 58.9675373743417 "057_000" 0 "Cap_S_0"  5 2
    "B2a" 58.4941986859436 "057_000" 3 "Cap_S_3"  6 2
    "B2a" 40.7057150501344 "058_000" 0 "Ven_S_0" 12 2
    "B2a" 45.7902819263404 "058_000" 3 "Ven_S_3" 13 2
    "B2a" 40.8156966385452 "058_000" 0 "Cap_S_0"  5 2
    "B2a" 40.2456975017882 "058_000" 3 "Cap_S_3"  6 2
    "B2a" 28.4304298836526 "060_000" 0 "Ven_S_0" 12 2
    "B2a" 29.8003511730568 "060_000" 3 "Ven_S_3" 13 2
    "B2a" 26.3127097283489 "060_000" 0 "Cap_S_0"  5 2
    "B2a" 26.8758220956767 "060_000" 3 "Cap_S_3"  6 2
    "B2a" 16.7480572229323 "061_000" 0 "Ven_S_0" 12 0
    "B2a" 19.4205510531086 "061_000" 3 "Ven_S_3" 13 0
    "B2a" 18.0594905682777 "061_000" 0 "Cap_S_0"  5 0
    "B2a" 18.1681549556479 "061_000" 3 "Cap_S_3"  6 0
    "B2a" 5.79983039306694 "062_000" 0 "Ven_S_0" 12 0
    "B2a"  5.6427251195098 "062_000" 3 "Ven_S_3" 13 0
    "B2a"  5.9606477518031 "062_000" 0 "Cap_S_0"  5 0
    "B2a" 5.77776693593664 "062_000" 3 "Cap_S_3"  6 0
    "B2a" 13.9201886666513 "063_000" 0 "Ven_S_0" 12 0
    "B2a" 11.6334913176376 "063_000" 3 "Ven_S_3" 13 0
    "B2a" 12.0943081676757 "063_000" 0 "Cap_S_0"  5 0
    "B2a" 13.3670843384248 "063_000" 3 "Cap_S_3"  6 0
    "B2a" 7.06439821908379 "064_000" 0 "Ven_S_0" 12 0
    "B2a"  8.5743497169679 "064_000" 3 "Ven_S_3" 13 0
    "B2a" 7.83106699654396 "064_000" 0 "Cap_S_0"  5 0
    "B2a"  8.1415417713203 "064_000" 3 "Cap_S_3"  6 0
    "B2a"  6.2413575708949 "065_000" 0 "Ven_S_0" 12 0
    "B2a" 5.42715192652326 "065_000" 3 "Ven_S_3" 13 0
    "B2a" 7.52413292798331 "065_000" 0 "Cap_S_0"  5 0
    "B2a" 7.55295331786773 "065_000" 3 "Cap_S_3"  6 0
    end
    label values process_delay delay
    label def delay 0 "No delay", modify
    label def delay 3 "3-day delay", modify
    label values sample_delay1 sample_delay1
    label def sample_delay1 5 "Cap_S_0", modify
    label def sample_delay1 6 "Cap_S_3", modify
    label def sample_delay1 12 "Ven_S_0", modify
    label def sample_delay1 13 "Ven_S_3", modify
    label values disease_grp status
    label def status 0 "Controls", modify
    label def status 1 "PreHD", modify
    label def status 2 "Manifest HD", modify

    In the graph, I am also unsure why some of the data lines have breaks in the lines, they should be connected for each ID_visit - can anyone see why there would be a problem with this?

    Thank you in advance for your help.
    Click image for larger version

Name:	Screenshot 2023-05-30 at 10.27.31.png
Views:	1
Size:	530.8 KB
ID:	1715461

  • #2
    I don't understand the main question. You are asking for labels (with ticks) at positions 5 6 12 13 and you are getting them. The gap between 6 and 12 is thus 6 times that between 5 and 6 or that between 12 and 13. That is, you may think of 5 6 12 13 as ordered labels, but twoway takes them literally.

    Otherwise I would check whether each identifier has 4 non-missing values and that observations are in suitable sort order for your purpose.

    Comment


    • #3
      Hello Nick,

      I see what you mean and I thought this might be the reason. But the positions 5 6 12 13 are coded as "Cap_S_0" , "Cap_S_3" , "Ven_S_0" , "Ven_S_3" , via the sample_delay1 variable which is on the x-axis, I thought it would evenly space the coded variables.

      Do you think there is a way to get around the idea that STATA would take the numbers literally?

      Comment


      • #4
        You're hoping that Stata will somehow decode what the values mean -- but Stata is useless on semantics, just excellent at understanding its own syntax, or at least of knowing whether you followed its rules. As far as it is concerned the value labels are just text to be shown.

        Why not map 5 6 12 13 to 1 2 4 5 or whatever numbers you prefer for your intended effect?

        Comment


        • #5
          I understand of course - I thought this might be the issue. The problem I am having is the sample_delay1 variable is coded for many other variations of the examples above and includes sets of 4 re-labelled in numbers from 1 - 18.

          Do you think I need to make a new variable for each different coded sample_delay1 groups I want to use for each graph labelled 1 2 3 4 so they are spread even?

          Comment


          • #6
            I am not sure that I understand the issue, but it might be helpful that egen's group() will map to values 1 up which you might want to re-apply to different subsets of 4 values.

            Code:
            egen thisgroup = group(whatever) if inlist(whatever, 5, 6, 12, 13)

            Comment


            • #7
              Thank you Nick, I think this might help.

              Would you know why some of the data lines have breaks in the lines, they should be connected for each ID_visit?

              Comment


              • #8
                On your question in #7: I addressed that earlier. I couldn't see any problems in your data example.

                Otherwise I would check whether each identifier has 4 non-missing values and that observations are in suitable sort order for your purpose.

                Comment

                Working...
                X