Announcement

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

  • Issue with displaying text in twoway graph

    Hello,

    I 'd like to plot a twoway graph of 2 histogram and add some summary statistics direct on the graph using previous local command but the text is not displayed when I run the command. Can someone help me ?

    *plot LP distribution by outcome, with summary statistics

    sum linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, detail
    local mean_0 = r(mean)
    local median_0 = r(p50)
    local sd_0 = r(sd)
    local lq_0 = r(p25)
    local uq_0 = r(p75)

    sum linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 1, detail
    local mean_1 = r(mean)
    local median_1 = r(p50)
    local sd_1 = r(sd)
    local lq_1 = r(p25)
    local uq_1 = r(p75)

    twoway (hist linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, dens color(navy%40) lcolor(black) width(0.1)) ///
    (hist linear_predictor_PREVENT_ASCVD if PREVENT_CVD== 1, dens color(dkgreen%40) lcolor(black) width(0.1)) ///
    (kdensity linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, lcolor(navy) bwidth(0.35)) ///
    (kdensity linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 1, lcolor(dkgreen) bwidth(0.35)), ///
    xtitle("Linear predictor value") scheme(s1color) ///
    legend(order(1 "No ASCVD event within 10 years" 2 "ASCVD event within 10 years" )) ///
    text(1 0.8 ///
    "No ASCVD:" ///
    " Mean = `: display %3.2f `mean_0''" ///
    " Median = `: display %3.2f `median_0''" ///
    " SD = `: display %3.2f `sd_0''" ///
    " IQR = `: display %3.2f `lq_0'' to `: display %3.2f `uq_0''" ///
    " " ///
    "ASCVD:" ///
    " Mean = `: display %3.2f `mean_1''" ///
    " Median = `: display %3.2f `median_1''" ///
    " SD = `: display %3.2f `sd_1''" ///
    " IQR = `: display %3.2f `lq_1'' to `: display %3.2f `uq_1''" ///
    , place(se) just(left))

  • #2
    Local macros have local scope. See https://journals.sagepub.com/doi/10....36867X20931028 for more on this.

    If Stata can't see the definitions of local macros, you will indeed get no text displays. You are probably defining the local macros in one place and running the graph code in another place. One place might be one chunk of a do-file or do-file editor contents and another place might be another such chunk.

    Running code in chunks makes the definitions of locals invisible to the code that refers to them.

    We can't run your code in the absence of a data example and above all we can't know precisely how you ran your code, but that's a fairly confident guess at the problem.
    Last edited by Nick Cox; 05 Mar 2024, 03:37.

    Comment


    • #3
      Thank you for your quick reply. I am new to the forum, my apologies. I will try to post below a sample of my data and code in a "code" format.
      All the code below is written in a do file editor as follows. I've tried running it in one go but it doesn't add the text to the graph either.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(linear_predictor_PREVENT_ASCVD PREVENT_CVD)
       -3.233408 0
      -4.6451254 0
      -3.1846154 0
      -2.6316755 0
       -3.499827 0
        -4.18054 0
       -3.463553 0
      -1.7733662 0
       -2.950184 0
       -2.843935 0
       -3.172227 0
       -3.758789 0
      -4.0210805 0
      -2.3435814 0
       -5.624711 0
      -1.8474748 0
      -1.6222174 1
      -2.8556025 1
      -2.1604683 1
      -2.2975612 1
      -2.5952985 1
       -2.864228 1
       -1.579227 1
      end
      label values PREVENT_CVD YN
      label def YN 0 "No", modify
      label def YN 1 "Yes", modify
      
      *plot LP distribution by outcome, with summary statistics
      
      sum linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, detail
      local mean_0 = r(mean)
      local median_0 = r(p50)
      local sd_0 = r(sd)
      local lq_0 = r(p25)
      local uq_0 = r(p75)
      
      sum linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 1, detail
      local mean_1 = r(mean)
      local median_1 = r(p50)
      local sd_1 = r(sd)
      local lq_1 = r(p25)
      local uq_1 = r(p75)
      
      twoway (hist linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, dens color(navy%40) lcolor(black) width(0.1)) ///
      (hist linear_predictor_PREVENT_ASCVD if PREVENT_CVD== 1, dens color(dkgreen%40) lcolor(black) width(0.1)) ///
      (kdensity linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, lcolor(navy) bwidth(0.35)) ///
      (kdensity linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 1, lcolor(dkgreen) bwidth(0.35)), ///
      xtitle("Linear predictor value") scheme(s1color) ///
      legend(order(1 "No ASCVD event within 10 years" 2 "ASCVD event within 10 years" )) ///
      text(1 0.8 ///
      "No ASCVD:" ///
      " Mean = `: display %3.2f `mean_0''" ///
      " Median = `: display %3.2f `median_0''" ///
      " SD = `: display %3.2f `sd_0''" ///
      " IQR = `: display %3.2f `lq_0'' to `: display %3.2f `uq_0''" ///
      " " ///
      "ASCVD:" ///
      " Mean = `: display %3.2f `mean_1''" ///
      " Median = `: display %3.2f `median_1''" ///
      " SD = `: display %3.2f `sd_1''" ///
      " IQR = `: display %3.2f `lq_1'' to `: display %3.2f `uq_1''" ///
      , place(se) just(left))

      Comment


      • #4
        text(1 0.8 ///
        "No ASCVD:" ///
        In your example, at least, the coordinates do not match up. You have y=1, x=0.8. Trying a different set within the y and x ranges:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(linear_predictor_PREVENT_ASCVD PREVENT_CVD)
         -3.233408 0
        -4.6451254 0
        -3.1846154 0
        -2.6316755 0
         -3.499827 0
          -4.18054 0
         -3.463553 0
        -1.7733662 0
         -2.950184 0
         -2.843935 0
         -3.172227 0
         -3.758789 0
        -4.0210805 0
        -2.3435814 0
         -5.624711 0
        -1.8474748 0
        -1.6222174 1
        -2.8556025 1
        -2.1604683 1
        -2.2975612 1
        -2.5952985 1
         -2.864228 1
         -1.579227 1
        end
        label values PREVENT_CVD YN
        label def YN 0 "No", modify
        label def YN 1 "Yes", modify
        
        *plot LP distribution by outcome, with summary statistics
        
        sum linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, detail
        local mean_0 = r(mean)
        local median_0 = r(p50)
        local sd_0 = r(sd)
        local lq_0 = r(p25)
        local uq_0 = r(p75)
        
        sum linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 1, detail
        local mean_1 = r(mean)
        local median_1 = r(p50)
        local sd_1 = r(sd)
        local lq_1 = r(p25)
        local uq_1 = r(p75)
        
        twoway (hist linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, dens color(navy%40) lcolor(black) width(0.1)) ///
        (hist linear_predictor_PREVENT_ASCVD if PREVENT_CVD== 1, dens color(dkgreen%40) lcolor(black) width(0.1)) ///
        (kdensity linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 0, lcolor(navy) bwidth(0.35)) ///
        (kdensity linear_predictor_PREVENT_ASCVD if PREVENT_CVD == 1, lcolor(dkgreen) bwidth(0.35)), ///
        xtitle("Linear predictor value") scheme(s1color) ///
        legend(order(1 "No ASCVD event within 10 years" 2 "ASCVD event within 10 years" )) ///
        text(2.8 -5.95 ///
        "No ASCVD:" ///
        " Mean = `: display %3.2f `mean_0''" ///
        " Median = `: display %3.2f `median_0''" ///
        " SD = `: display %3.2f `sd_0''" ///
        " IQR = `: display %3.2f `lq_0'' to `: display %3.2f `uq_0''" ///
        " " ///
        "ASCVD:" ///
        " Mean = `: display %3.2f `mean_1''" ///
        " Median = `: display %3.2f `median_1''" ///
        " SD = `: display %3.2f `sd_1''" ///
        " IQR = `: display %3.2f `lq_1'' to `: display %3.2f `uq_1''" ///
        , place(se) just(left))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	41.8 KB
ID:	1745936

        Comment


        • #5
          An orthogonal detail is that hyphens or short dashes are not minus signs. You would get better minus signs using {&minus}. See help text.

          To see the difference add to any graph you like an option something like

          Code:
          subtitle("{&minus}7, not -7")
          and then delete it!

          Comment


          • #6
            Thank you for your help !

            Comment

            Working...
            X