Announcement

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

  • How to ask stata to round the labels in a histogram?

    Hi ,
    I am using the following code to make a graph, but: the decimals in the graph are annoying; if I sum the numbers the result does not give 100%.
    Is there a way to round the decimals in the label?

    histogram NormSocialCapitalINDEX if SoluzioneAbitativa==3, width(10) start(0) percent addlabel normal ylabel(0(2)38) xlabel(0(10)100) color(green) lcolor(black) addlabopts(mlabsize(medsmall))

    Thanks




  • #2
    Use option yvarformat, like this:

    Code:
    sysuse auto, clear
    
    histogram mpg if foreign == 1, width(10) start(0) percent addlabel normal ylabel(0(2)38) xlabel(0(10)100) color(green) lcolor(black) addlabopts(mlabsize(medsmall) yvarformat(%4.1f))

    Comment


    • #3
      To what Ken wrote I will add that you can never be sure that the individual percentages displayed will add to 100, because of rounding. Consider 99 observations with 33 values of 1, 33 values of 2, and 33 values of 3. The percentages will all be displayed as 33 or 33.3 or ... and will sum to 99 or 99.9 or ... .

      Comment


      • #4
        I had cause to cite https://statweb.stanford.edu/~cgates...freedman79.pdf in a recent thread, and here we are again.

        Comment


        • #5
          Thank you Ken, it work perfectly.
          But William: is it normal that the sum does not make 100 even if I do not round? This is the thing that weird me

          And thank you Nick for the advice...

          Comment


          • #6
            And a curiosity: where is it that I find the code to round to two decimal or the other rounding possibilities?

            Comment


            • #7
              Use a different display format to round to a different number of decimal places.

              Comment


              • #8
                is it normal that the sum does not make 100 even if I do not round? This is the thing that weird me
                Yes it is normal. It is a matter of precision. Just like you can only write 100/3 to a limited number of digits when it is actually 33.3333... continuing forever, Stata can only store a limited number of digits. And when you use pen and paper to write 33.333...3 three times and then add them together, you will get 99.999...9 as a result.

                For a good discussion of precision in Stata and the issues involved see the output of
                Code:
                help precision
                and this discussion on the Stata blog.

                https://blog.stata.com/2012/04/02/th...-to-precision/

                Comment

                Working...
                X