Announcement

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

  • Round the value of label on graph

    Hi everyone!

    I used the next command to draw a graph:
    Code:
    twoway (rcap ci_up ci_low event) (line penalty_men event) (scatter penalty_men event if event == 0 | event == 10, mlabel(penalty_men))
    The trouble is that the values of the variable "penalty_men" can have several digits after the decimal point, but I prefer that the graph will be as "clean" as possible and only two or three digits will appear so for example the number -.3038417 will appear as -.303 or -.304. I tried to use the option "mlabformat()" but I'm not sure what to write in the parenthesis. Can you Please help me to complete the next code?:
    Code:
    twoway (rcap ci_up ci_low event) (line penalty_men event) (scatter penalty_men event if event == 0 | event == 10, mlabel(penalty_men) mlabformat())
    Thank you already,

    Fitzgerald

  • #2
    See

    Code:
    help format
    If you do not want to show the zero before the decimal point, you can use the general format:

    Code:
    di %3.2g 0.38786
    Res.:

    Code:
    . di %3.2g 0.38786
     .39
    However, if a zero appears immediately after the decimal point, this format may revert to exponential notation:

    Code:
    di %3.2g 0.038786
    Res.:

    Code:
    . di %3.2g 0.038786
     3.9e-02
    To ensure consistency, you can create a string variable using a fixed format with a restriction that leading zeros are not displayed.


    Code:
    sysuse auto, clear
    replace price = price/1e+4
    gen lab= cond(int(price)==0, substr(string(price, "%3.2f"), 2, .), string(price, "%3.2f"))
    scatter mpg weight if rep78==3, mlab(lab)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	41.3 KB
ID:	1775670

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      See

      Code:
      help format
      If you do not want to show the zero before the decimal point, you can use the general format:

      Code:
      di %3.2g 0.38786
      Res.:

      Code:
      . di %3.2g 0.38786
      .39
      However, if a zero appears immediately after the decimal point, this format may revert to exponential notation:

      Code:
      di %3.2g 0.038786
      Res.:

      Code:
      . di %3.2g 0.038786
      3.9e-02
      To ensure consistency, you can create a string variable using a fixed format with a restriction that leading zeros are not displayed.


      Code:
      sysuse auto, clear
      replace price = price/1e+4
      gen lab= cond(int(price)==0, substr(string(price, "%3.2f"), 2, .), string(price, "%3.2f"))
      scatter mpg weight if rep78==3, mlab(lab)
      [ATTACH=CONFIG]n1775670[/ATTACH]
      Hi Andrew, thank you.

      So I can't simply specify something in the options of the scatter command that will change the format the label is displayed? I have to create new variable?

      Thanks again,

      Fitz

      Comment


      • #4
        You can. I asked you to review

        Code:
        help format
        For the example:

        Code:
        sysuse auto, clear
        replace price = price/1e+4
        scatter mpg weight if rep78==3, mlab(price) mlabformat(%3.2g)
        However, I highlighted an issue with this approach in #2.

        Comment


        • #5
          The nub of the matter may well be just "use %4.3f". If you are wondering what display formats do, a good quick way to experiment is to fire up display:

          .
          Code:
           di %4.3f -.3038417
          -0.304
          
          . di %04.3f -.3038417
          -0.304
          where evidently the abbreviation di is allowed but not compulsory.

          I think part of what Andrew Musau is driving at is that we have an example -.3038417 but we can't tell otherwise what is the range of this variable and whether you need some different format or combination of formats to get what you want across the range.

          Comment

          Working...
          X