Announcement

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

  • Format without leading zero but with fixed number of decimal places

    Dear Statalist colleagues,

    I am a co-author on a medical journal article that includes some Kaplan-Meier curves (sts graph). The journal editor requested that the vertical axis labels not include leading zeros (apparently APA and other style guidelines suggest omitting leading zeros for any quantities--like proportions, probabilities, and correlation coefficients--that cannot exceed 1). While there is a format for adding leading zeros (e.g., %0f3.2), there doesn't seem to be one for suppressing them. The %#.#g format seems to do this by default but doesn't allow for a fixed number of digits after the decimal place, which I find more visually attractive. To summarize:

    Code:
    sts graph
    gives labels 0.00, 0.25, 0.50, 0.75, and 1.00.

    Code:
    sts graph, ... ylabel(,format(%4.0g))
    gives labels 0, .25, .5, .75, and 1.

    However, I can't find a way to produce labels 0 , .25, .50, .75, and 1.

    Is there a format that will produce these labels? Is there another way? Is my desire for fixed number of decimal places misguided?

    Thanks for your help.

    SIncerely,
    Joe Canner

  • #2
    How about using the approach in the following (nonsense) example:

    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . scatter mpg price
    
    . scatter mpg price, ylabel(10 ".50" 15 ".73" 30 "3.75")

    Comment


    • #3
      Stephen

      Thanks for your response; I feel kind of silly because I actually thought of something like that but didn't pursue it far enough.

      I started by trying this:

      Code:
      label def prob 0 "0" 0.25 ".25" 0.50 ".50" 0.75 ".75" 1 "1"
      and got an error ("may not label .25") so I assumed that the following would not work:

      Code:
      sts graph, ...  ylabel(0 "0" 0.25 ".25" 0.50 ".50" 0.75 ".75" 1 "1")
      when in fact it does exactly what I need it to do.

      A format would be preferable, of course, but at least there's a way around it.

      Regards,
      Joe

      Comment


      • #4
        Joe: I suspect some typos

        You get 0, .25, .5, .75, and 1 but you can't find a way to produce labels 0 , .25, .50, .75, and 1?

        I'll take your title as summary.

        You can always spell what you want as text, e.g.

        Code:
        yla(0 "0.00" .25  ".25"  .5 ".50" .75 "0.75" 1 "1.00"

        Here is a hack at a program to omit leading zeros:

        Code:
        program joeformat 
           version 11.2 
            syntax anything, local(str) format(format) 
            
            * error message if not a numlist 
            numlist "`anything'" 
            local anything `r(numlist)' 
            
            foreach x of local anything { 
                local fx : di `format' `x' 
                if `x' >= 0 & `x' < 1 local fx = substr("`fx'", 2,.) 
                local show `show'' `x' "`fx'"  
            } 
            
            di `"`local'"'  
            c_local `local' `show' 
        end

        For example,

        Code:
        sysuse auto 
        
        joeformat 0(0.25)1, local(myla) format(%3.2f)
         0 ".00"  .25 ".25"  .5 ".50"  .75 ".75"  1 "1.00" 
        
        scatter foreign weight, yla(`myla')

        It is hard for a program to beat the direct approach here.

        Comment


        • #5
          Nick,

          Thanks; I think you and Stephen suggested similar solutions, which I was too impatient to find on my own.

          I don't think there was a typo, but I am waffling back and forth as to whether I want .00, .25, .50, .75, and 1.00 or 0, .25, .50, .75. and 1. ".00" looks very strange to me, but the second option violates the aesthetic principle I was trying to adhere to. In truth, what I really don't understand is why the journal (or why the APA) doesn't like leading zeros. The APA says they are useful when the number can exceed 1, but that doesn't explain why you shouldn't use them otherwise.

          Do you have any insight into the historical reasons why the default y-axis label format for sts graph is %f4.2 and/or why there is no built-in format that allows for a fixed number of decimal places and no leading zeros? Is this some that StataCorp should fix, or is this a quirk of mine that I should live with?

          Regards,
          Joe

          Comment


          • #6
            Joe:

            I guess that I must have misread what you wanted (to know how to do!) because it seemed that any insistence (by third parties) on omitting leading zeros and a fixed number of decimal places would automatically imply .00 for 0, a convention which I consider bizarre taste.

            I would guess very simply that Bill Gould, originally and to the present chief designer of Stata, prefers lack of clutter, so that leading zeros and trailing zeros look dispensable and that's where the default comes from.

            My personal view is that explicit zeros are helpful and I often find myself inserting them. It's hard for me to see that saving space or deleting characters to simplify display is a good reason to prefer to omit them.

            The present set of formats is rich but still omits many styles that people ask for here. One is automatic addition of "%" on every label, which I hate but which many people like (or are obliged to produce, different thing!).

            I've provided a (partial) work-around in mylabels (SSC). The more general point is that while users cannot define formats, any programmed way of producing axis label text (or text to display somewhere) is a way of getting to the same place.

            You may have missed the thread http://www.statalist.org/forums/foru...trailing-zeros which raises some similar issues.

            Comment


            • #7
              So is the conclusion here that Stata does not support a format that produces a fixed number of decimal places AND no leading zero? These "hacks" are not very general and require a lot of extra work to get the desired format across a variety of tables and graphs.

              Comment

              Working...
              X