Announcement

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

  • Graph: text not centered within textbox

    Two times I am trying to write text within a textbox into a graph. However, although the commands for both text boxes are identical (except for the text), only the first text is centered correctly, while the second is not (if I exchange the sequence of the commands for the text boxes, again the first is correctly centered while the second is not).

    I am not sure whether there is an error on my part or whether this is a problem with Stata. Here is the (somewhat lengthy) code I am using (see my comments marked in red):
    Code:
    * set scheme lean1 (install scheme if necessary):
    cap set scheme lean1
    if _rc {
      net install gr0002_3, from("http://www.stata-journal.com/software/sj4-3")
      set scheme lean1
    }
    
    local m0 = 68
    local m1 = 69
    local sd = 3.1
    local  n = 100
    local se = `sd'/sqrt(`n')
    local alpha = 0.05
    local z = invnorm(1-`alpha'/2)
    local lower = `m0'-`z'*`se'
    local upper = `m0'+`z'*`se'
    if `m0' < `m1' {
      local beta = normal((`upper'-`m1')/`se') - ///
                   normal(-(`m1'-`lower')/`se')
      local crit = `upper'
      local area1 = max(`lower',`m1'-4*`se')
      local area2 = `crit'
    }
    else {
      local beta = normal((`m1'-`lower')/`se') - ///
                   normal(-(`upper'-`m1')/`se')
      local crit = `lower'
      local area1 = `crit'
      local area2 = min(`m1'+4*`se',`upper')
    }
    local power = 1-`beta'
    
    local tcrit : di round(`crit',0.01)
    local tbeta : di round(`beta',.001)
    local tpower : di round(`power',.001)
    
    local xtit1 = "kritischer Wert = `tcrit', {&alpha}{subscript:zweiseitig} = `alpha', "
    local xtit2 = "{&beta} = `tbeta', Power = `tpower'"
    local subt1 = "{it:n} = `n', H{subscript:0}: {&mu} = `m0', "
    local subt2 = "H{subscript:1}: {&mu} = `m1', {&sigma} = `sd'"
    
    twoway function y=normalden(x,`m0',`se'), ///
                    range(`=`m0'-4*`se'' `=`m0'+4*`se'') ///
                    recast(area) color("255 255 212") || ///
           function y=normalden(x,`m1',`se'), ///
                    range(`=`m1'-4*`se'' `=`m1'+4*`se'') ///
                    recast(area) color("255 255 212") || ///
           function y=normalden(x,`m0',`se'), ///
                    range(`=`m0'-4*`se'' `=`m0'-`z'*`se'') ///
                    recast(area) color(blue) || ///
           function y=normalden(x,`m0',`se'), ///
                    range(`=`m0'+`z'*`se'' `=`m0'+4*`se'') ///
                    recast(area) color(blue) || ///
           function y=normalden(x,`m1',`se'), range(`=`area1'' `=`area2'') ///
                    recast(area) color("240 59 32") || ///
           function y=normalden(x,`m0',`se'), ///
                    range(`=`m0'-4*`se'' `=`m0'+4*`se'') color(black) ///
                    lw(vthin) lp(solid) || ///
           function y=normalden(x,`m1',`se'), ///
                    range(`=`m1'-4*`se'' `=`m1'+4*`se'') color(black) ///
                    lw(vthin) lp(solid) ///
           title("Power eines Einstichproben-{it:z}-Tests", size(medsmall)) ///
           subtitle("`subt1'`subt2'", size(small)) ///
           xtitle(`"`xtit1'`xtit2'"', size(small)) xlabel(`m0' `m1', labs(small)) ///
           text(0 `m0' "{&mu}0", size(vsmall) place(s)) ///
           text(0 `m1' "{&mu}1", size(vsmall) place(s)) ///
           text(0 `=`crit'' `"`tcrit'"', size(vsmall) place(s) col(blue)) ///
           ytitle("Wahrscheinlichkeitsdichte", size(small)) ylab(, labs(small)) ///
           text(0.6 68 "Akzeptanz-" "region H{subscript:0}", size(6pt) place(c) /// // <-- correctly centered
                box bc(white) fc(white) lstyle(none) bex) ///
           text(0.6 69 "Akzeptanz-" "region H{subscript:1}", size(6pt) place(c) /// // <-- not centered
                box bc(white) fc(white) lstyle(none) bex) ///
           legend(off) scheme(lean1)

  • #2
    I get the following graph running the code (Stata 16.1, Windows 10). Both look centered to me.
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	91.6 KB
ID:	1614393

    Comment


    • #3
      How did you post the graph? Did you attach a .png-file?

      Whether I am using Stata 16.1 or Stata 17.0 -- in my system (Linux, Ubuntu 18.04) text in the second box is always not centered. But by accident I found, that text in all odd boxes (first, third, ...) are centered, whereas in all even boxes it is not. Thus I found a work-around with text centered within the boxes I want to display by inserting a second text box command (writing an empty string into nowhere) following each text box command (see comments below):
      Code:
      local m0 = 68
      local m1 = 69
      local sd = 3.1
      local  n = 100
      local se = `sd'/sqrt(`n')
      local alpha = 0.05
      local z = invnorm(1-`alpha'/2)
      local lower = `m0'-`z'*`se'
      local upper = `m0'+`z'*`se'
      if `m0' < `m1' {
        local beta = normal((`upper'-`m1')/`se') - ///
                     normal(-(`m1'-`lower')/`se')
        local crit = `upper'
        local area1 = max(`lower',`m1'-4*`se')
        local area2 = `crit'
      }
      else {
        local beta = normal((`m1'-`lower')/`se') - ///
                     normal(-(`upper'-`m1')/`se')
        local crit = `lower'
        local area1 = `crit'
        local area2 = min(`m1'+4*`se',`upper')
      }
      local power = 1-`beta'
      
      local tcrit : di round(`crit',0.01)
      local tbeta : di round(`beta',.001)
      local tpower : di round(`power',.001)
      
      local xtit1 = "kritischer Wert = `tcrit', {&alpha}{subscript:zweiseitig} = `alpha', "
      local xtit2 = "{&beta} = `tbeta', Power = `tpower'"
      local subt1 = "{it:n} = `n', H{subscript:0}: {&mu} = `m0', "
      local subt2 = "H{subscript:1}: {&mu} = `m1', {&sigma} = `sd'"
      
      twoway function y=normalden(x,`m0',`se'), ///
                      range(`=`m0'-4*`se'' `=`m0'+4*`se'') ///
                      recast(area) color("255 255 212") || ///
             function y=normalden(x,`m1',`se'), ///
                      range(`=`m1'-4*`se'' `=`m1'+4*`se'') ///
                      recast(area) color("255 255 212") || ///
             function y=normalden(x,`m0',`se'), ///
                      range(`=`m0'-4*`se'' `=`m0'-`z'*`se'') ///
                      recast(area) color(blue) || ///
             function y=normalden(x,`m0',`se'), ///
                      range(`=`m0'+`z'*`se'' `=`m0'+4*`se'') ///
                      recast(area) color(blue) || ///
             function y=normalden(x,`m1',`se'), range(`=`area1'' `=`area2'') ///
                      recast(area) color("240 59 32") || ///
             function y=normalden(x,`m0',`se'), ///
                      range(`=`m0'-4*`se'' `=`m0'+4*`se'') color(black) ///
                      lw(vthin) lp(solid) || ///
             function y=normalden(x,`m1',`se'), ///
                      range(`=`m1'-4*`se'' `=`m1'+4*`se'') color(black) ///
                      lw(vthin) lp(solid) ///
             title("Power eines Einstichproben-{it:z}-Tests", size(medsmall)) ///
             subtitle("`subt1'`subt2'", size(small)) ///
             xtitle(`"`xtit1'`xtit2'"', size(small)) xlabel(`m0' `m1', labs(small)) ///
             text(0 `m0' "{&mu}0", size(vsmall) place(s)) ///
             text(0 `m1' "{&mu}1", size(vsmall) place(s)) ///
             text(0 `=`crit'' `"`tcrit'"', size(vsmall) place(s) col(blue)) ///
             ytitle("Wahrscheinlichkeitsdichte", size(small)) ylab(, labs(small)) ///
             text(0.6 `m0' "Akzeptanz-" "region H{subscript:0}", size(6pt) place(c) ///
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(-0.2 `=`m0'-1' " ", size(6pt) place(c) ///  // <-- "empty" text box
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(0.6 `m1' "Akzeptanz-" "region H{subscript:1}", size(6pt) place(c) ///
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(-0.2 `=`m0'-1' " ", size(6pt) place(c) ///  // <-- "empty" text box
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(0.05 `=`m0'+`z'*`se'+0.07' "{&alpha}/2", size(6pt) place(c) ///
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(-0.2 `=`m0'-1' " ", size(6pt) place(c) ///  // <-- "empty" text box
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(0.05 `=`m0'-`z'*`se'-0.07' "{&alpha}/2", size(6pt) place(c) ///
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(-0.2 `=`m0'-1' " ", size(6pt) place(c) ///  // <-- "empty" text box
                  box bc(white) fc(white) lstyle(none) bex) ///
             text(0.05 `=`crit'-(sign(`m1'-`m0')*0.07)' " {&beta} ", size(6pt) place(c) ///
                  box bc(white) fc(white) lstyle(none) bex) ///
             legend(off) scheme(lean1)
      Although this work-around solves the current problem, I think that this is odd and suspect that it is a Stata issue (perhaps in combination with my operating system?). What is interesting in this respect: The same issue occurs if I try to insert text boxes using the graph editor.
      Last edited by Dirk Enzmann; 12 Jun 2021, 12:29. Reason: Due to copy & past some code was destroyed -- this code should run now.

      Comment


      • #4
        How did you post the graph? Did you attach a .png-file?
        Yes.

        I think that this is odd and suspect that it is a Stata issue (perhaps in combination with my operating system?)
        It seems that the issue does not arise in Windows. Maybe you can contact Tech Support and inquire.

        Comment

        Working...
        X