Announcement

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

  • leading zeros, but no trailing zeros

    Dear all,

    I'm creating graphs, and the y-axis ticks are 0, .05, .1, .15, .2. I want the y-axis ticks displayed as 0, 0.05, 0.1, 0.15, 0.2, i.e., I want to display 0 before decimal places.

    I tried

    line IndustrySize tm, yscale(range(0 0.2)) ylabel(, format(%03.2f) angle(horizontal)) ytitle("{it:IndustrySize}") xtitle("") plotregion(style(none) margin(zero)) saving(IndustrySize, replace)

    but it creates the y-axis ticks as 0.00, 0.05, 0.10, 0.15, 0.20, i.e., it creates 0 before decimal places (as I want), but it creates trailing zeros (which I do not want).

    Is it possible to create graphs with y-axis ticks such that they display 0 before decimal places, but do not create trailing zeros?

    Thank you very much for your help, and I look forward to hearing form you.

    Best,


    John

  • #2
    John,
    Change
    Code:
    format(%03.2f) to format(%03.1f)
    Code:
    #d ;
    line IndustrySize tm,
    yscale(range(0 0.2))
    ylabel(, format(%03.2f) angle(horizontal)) 
    ytitle("{it:IndustrySize}")
    xtitle("") plotregion(style(none) margin(zero))
    saving(IndustrySize, replace);
    #d cr
    Best,
    Alan

    Comment


    • #3
      I'm not sure that there is any Stata numerical format statement that will give you both leading zeroes and omit trailing zeroes. You could try value labels (see below), but others on the list might have devised better workarounds.

      .ÿversionÿ14.2

      .ÿ
      .ÿclearÿ*

      .ÿsetÿmoreÿoff

      .ÿ
      .ÿinputÿdoubleÿIndustrySizeÿbyteÿtm

      ÿÿÿÿÿIndustry~eÿÿÿÿÿÿÿÿtm
      ÿÿ1.ÿ0ÿ1
      ÿÿ2.ÿ0.05ÿ2
      ÿÿ3.ÿ0.1ÿ3
      ÿÿ4.ÿ0.15ÿ4
      ÿÿ5.ÿ0.2ÿ5
      ÿÿ6.ÿend

      .ÿ
      .ÿ*
      .ÿ*ÿBeginÿhere
      .ÿ*
      .ÿgenerateÿintÿisÿ=ÿround(IndustrySizeÿ*ÿ100,ÿ1)

      .ÿlabelÿdefineÿISÿ0ÿ"0"ÿ5ÿ"0.05"ÿ10ÿ"0.1"ÿ15ÿ"0.15"ÿ20ÿ"0.2"

      .ÿlabelÿvaluesÿisÿIS

      .ÿ
      .ÿgraphÿtwowayÿlineÿisÿtm,ÿ///
      >ÿÿÿÿÿÿÿÿÿyscale(range(0ÿ20))ÿylabel(ÿ,ÿvaluelabelÿangle(horizontal)ÿnogrid)ÿ///
      >ÿÿÿÿÿÿÿÿÿytitle("{it:IndustrySize}")ÿxtitle("")ÿ///
      >ÿÿÿÿÿÿÿÿÿplotregion(style(none)ÿmargin(zero))

      .ÿ
      .ÿquietlyÿgraphÿexportÿIndustrySize.png,ÿreplace

      .ÿ
      .ÿexit

      endÿofÿdo-file


      .
      Click image for larger version

Name:	IndustrySize.png
Views:	1
Size:	26.5 KB
ID:	1357237

      Comment


      • #4

        Good question!

        I have often wanted e.g. 0 0.25 0.5 0.75 1 rather than 0.00 0.25 0.50 0.75 1.00 but have mostly just spelled out what I wanted ad hoc.

        The underlying problem here is that not only is there is no official display format that does this but also that users cannot define display formats.

        I hacked at mylabels (SSC) to add what I call a clean format, i.e. trailing zeros and decimal points are elided. If you want leading zeros, you just specify that in a format() option.

        Here is the code (but please note that this is not the version at SSC, yet!):

        Code:
        *! 1.3.0 NJC 20 Sept 2016
        * 1.2.0 NJC 16 August 2012
        * 1.1.0 NJC 2 July 2008
        * 1.0.0 NJC 5 May 2003
        program mylabels
                version 8
                syntax anything(name=values) , Local(str) ///
                [MYscale(str asis) clean Format(str) PREfix(str) SUFfix(str)]  
        
                if (`"`myscale'"' != "" & "`clean'" != "") | (`"`myscale'`clean'"' == "") {
                        di as err "must specify myscale() or clean"
                        exit 198
                }
        
                capture numlist "`values'"
                if _rc == 0 local values "`r(numlist)'"
                
                if !index(`"`myscale'"',"@") & "`clean'" == "" {
                        di as err "myscale() does not contain @"
                        exit 198
                }      
        
                if "`format'" != "" {
                        capture di `format' 1.2345
                }      
        
                if "`clean'" != "" {
                        foreach v of local values {
                                if "`format'" != "" local val : di `format' `v'
                                else local val "`v'"
        
                                if length("`val'") > 1 {
                                        while inlist(substr("`val'", -1, 1), "0", ".") {
                                                local val = ///
                                                substr("`val'", 1, length("`val'") - 1)
                                        }
                                }
                                local mylabels `"`mylabels' `v' "`val'" "'              
                        }
                }
                else {
                        foreach v of local values {
                                local val : subinstr local myscale "@" "(`v')", all
                                local val : di %18.0g `val'
                                if "`format'" != "" local v : di `format' `v'
                                local mylabels `"`mylabels' `val' "`prefix'`v'`suffix'""'
                        }
                }
        
                di as res `"{p}`mylabels'"'
                c_local `local' `"`mylabels'"'
        end


        Here are some examples. The key idea is that the program creates a local macro within the caller program's namespace that you then use in your axis label option:

        Code:
        . mylabels 0(0.2)1, clean local(foo)
        0 "0" .2 ".2" .4 ".4" .6 ".6" .8 ".8" 1 "1"
        
        . mylabels 0(0.25)1, clean local(foo)
        0 "0" .25 ".25" .5 ".5" .75 ".75" 1 "1"
        
        . mylabels 0.005(0.005)0.025, clean local(foo)
        .005 ".005" .01 ".01" .015 ".015" .02 ".02" .025 ".025"
        
        . mylabels 0.005(0.005)0.025, format(%04.3f) clean local(foo)
        .005 "0.005" .01 "0.01" .015 "0.015" .02 "0.02" .025 "0.025"
        The last is the trickiest, but is not rocket surgery. The format %04.3f by itself would render

        Code:
        0.005(0.005)0.025
        as

        Code:
        0.005 0.010 0.015 0.020 0.025
        and the clean option then removes the trailing zeros.

        The pluses and minuses are fairly clear here:

        1. If you are producing a series of similar graphs with a consistent look to axis labels, you will want that automated.

        2. In principle, you can usually type the axis label specification that is equivalent, e.g.

        Code:
        yla(.005 "0.005" .01 "0.01" .015 "0.015" .02 "0.02" .025 "0.025")
        in (some times) fewer characters, which is why (perhaps) I haven't thought of automating this before. (Add stupidity, etc.)

        3. In practice, thinking it out and getting it right often takes me much longer. I usually make silly little mistakes, which is not to say that you could not make silly little mistakes with
        mylabels.
        Last edited by Nick Cox; 20 Sep 2016, 12:09.

        Comment


        • #5
          mylabels is now updated: see http://www.statalist.org/forums/foru...updated-on-ssc

          Comment

          Working...
          X