Announcement

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

  • Scientific notation on histogram with frequency option

    I need to use the frequency option with a histogram. Many observations are clustered in a bin at one end of the x range, which gives a high count for that bin. By default, large frequencies are shown using scientific notation, but I would like to replace this on the y axis with normal numbers (e.g., 1,000 instead of 1.0e+04). Problem replication:

    Code:
    set obs 100000
    set seed 1
    gen prob = 0.1 * uniform()
    replace prob = 0.99 in 1
    hist prob, s(0) w(0.02) freq
    Thank you.

  • #2
    I note that 1e4 is not 1000!


    Code:
    . di 1e4
    10000
    But I guess you know that really.

    To insist that the numbers are shown as you want, you can just spell out

    Code:
    ... yla(0 5000 "5000" 10000 "10000" 15000 "15000" 20000 "20000") 
    or even

    Code:
    ... yla(0 5e3 "5000" 1e4 "10000" 15e3 "15000" 2e4 "20000") 
    Another approach, which is one step to automation for a series of similar graphs, is to use mylabels from SSC.

    In terms of your code

    Code:
    clear  
    set obs 100000
    set seed 1
    gen prob = 0.1 * uniform()
    replace prob = 0.99 in 1
    hist prob, s(0) w(0.02) freq
    
    * install just once
    ssc inst mylabels
    
    mylabels 0(5000)20000, local(labels)
    
    hist prob, s(0) w(0.02) freq yla(`labels', ang(h))
    mylabels was discussed earlier today, which shows that reading Statalist as well as writing to it tells you about stuff that can be useful. http://www.statalist.org/forums/foru...-actual-values
    Last edited by Nick Cox; 07 Oct 2016, 11:36.

    Comment


    • #3
      Thanks, Nick. I didn't know about mylabels, so I appreciate the tip, and sorry for not spotting it in the earlier thread. Too bad the format option isn't available, but I'll take what I can get!

      As an aside, and despite this being my first post, I've been using Statalist to answer my questions for well over a decade, and I want to thank you for your tireless dedication to the cause. I would have been stuck on many occasions without your posts. So, thank you!

      Comment


      • #4
        Thanks for the thanks, but my post missed the simplest solution of all, and your mention of format pointed the way:

        Code:
        hist prob, s(0) w(0.02) freq yla(, format(%5.0f) ang(h))

        Comment


        • #5
          Perfect. Thanks again!

          Comment

          Working...
          X