Announcement

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

  • niceloglabels available from SSC

    Thanks as always to Kit Baum, a new program niceloglabels is now available from SSC. It's written for Stata 11 up.

    The purpose of the command is (surprise) nice log labels, meaning nice axis labels when using logarithmic scales.

    Here is a reproducible example showing the problem.

    Code:
    sysuse census, clear
    local noref rlopts(lc(none)) 
    quantile pop, ysc(log) `noref' name(G1)
    
    niceloglabels pop , local(yla) style(125)
    quantile pop, ysc(log) yla(`yla', ang(h)) `noref' name(G2)
    
    niceloglabels pop , local(yla) style(125) powers
    quantile pop, ysc(log) yla(`yla', ang(h)) `noref' name(G3)
    The first graph G1 shows what often happens with log scales in graph when the variable concerned varies over a wide range, as it does with state populations in the United States. ysc(log) means only that graph is smart enough not to try to show 0 on a log scale; otherwise the labels are good for a linear scale, but can be too bunched up to appeal, or even not readable at all.

    For some years, no some decades, I just fixed this on the fly by thinking up better labels ad hoc. But writing a helper program always seemed like a small project worth doing.


    Click image for larger version

Name:	nicelog_G1.png
Views:	1
Size:	22.9 KB
ID:	1416098


    The purpose of niceloglabels is to suggest nice(r) labels but you have to tell it your idea of nice, as a style of labels you prefer. I specified 125 as a style here and if that's cryptic then example output should make it clearer:

    Code:
    . niceloglabels pop , local(yla) style(125)
    500000 1000000 2000000 5000000 10000000 20000000
    The leading digits cycle over 1 2 5 within the range of the data. The result is better but still not ideal. One solution not shown here would be to scale to millions, and then show 0.5 1 2 5 10 20 as labels.


    Click image for larger version

Name:	nicelog_G2.png
Views:	1
Size:	22.2 KB
ID:	1416099


    Another solution is to use power notation. The second niceloglabels call emits the syntax labels using superscript notation

    Code:
    . niceloglabels pop , local(yla) style(125) powers
    500000 "5x10{sup:5}"  1000000 "10{sup:6}"   2000000 "2x10{sup:6}"   5000000 "5x10{sup:6}"  10000000 "10{sup:7}"   20000000 "2x10{sup:7}"
    This is where niceloglabels may start to seem useful. Even if you recall the syntax typing out the {sup: } stuff is not the most fun you can have without laughing. It's tedious and error-prone. All you have to do is put the label syntax in a local macro (a kind of bag, if you've not met that idea before) and then refer to the local macro in the graph call. Rather oddly, Stata doesn't seem to support the multiplication sign as a special symbol (tell me what I missed), so I am just using lower case x.

    Click image for larger version

Name:	nicelog_G3.png
Views:	1
Size:	21.6 KB
ID:	1416100


    There is more. You can specify a range and the program will suggest labels for that range:


    Code:
    . niceloglabels 1e2 1e9, local(yla) style(1) powers
    100 "10{sup:2}"  1000 "10{sup:3}"  10000 "10{sup:4}"  100000 "10{sup:5}"  1000000 "10{sup:6}"  10000000 "10{sup:7}"  100000000 "10{sup:8}"  1000000000 "10{sup:9}"
    I needed that for real with a country population variable that ranged from 57 (Pitcairn) to 1387 million (China).

    An earlier try at this problem some years ago didn't reach launch stage, partly because it seemed hard to find consensus about nice labels on logarithmic scales. Several styles can be found in the literature and even more adhockery hinging mostly on some pragmatism not showing labels that are too close to their neighbours.

    As issued now, niceloglabels supports these styles

    1 means powers of 10 such as ..., 0.1, 1, 10, 100, 1000, ...
    13 means cycling such as ..., 0.3, 1, 3, 10, 30, 100, 300, ...
    15 means cycling such as ..., 0.5, 1, 5, 10, 50, 100, 500, ...
    125 means cycling such as ..., 0.1, 0.2, 0.5, 1, 2, 5, 10, ...
    147 means cycling such as ..., 0.1, 0.4, 0.7, 1, 4, 7, 10, ...

    2 means powers of 2 such as ..., 1, 2, 4, 8, 16, ...

    but if there are principled alternatives, or even alternatives that are strong conventions in some fields, I am interested to hear of them.

  • #2
    For what it's worth, the unicode character for the multiplication sign is U+00D7, and (if your browser supports it) looks like this: ×
    The space around the x is actually part of the character, so you don't even need to add any "esthetic" whitespaces.

    I don't remember from which version Stata supports unicode character display.

    Code:
    quantile pop, ysc(log) rlopts(lc(none)) ylabel(1 "1×10{sup:0}", angle(horizontal))
    Click image for larger version

Name:	multSign.png
Views:	1
Size:	61.2 KB
ID:	1416117

    Comment


    • #3
      Jesse: That's a good tip. Thanks.

      Comment


      • #4
        Now written up at https://www.stata-journal.com/articl...article=gr0072

        Comment


        • #5
          Rather oddly, Stata doesn't seem to support the multiplication sign as a special symbol (tell me what I missed), so I am just using lower case x.
          Nick Cox Apologies for necroing this thread, but you can use {c 215} to get a proper multiplication sign. (e.g. "2{c 215}10{sup:6}".) I know this works as far back as Stata 12.1, and I think it works significantly further back than that. Since niceloglabels is version 11+, I think it's safe to use here.

          Also, if some lovely person out there with Stata 11 (or before) wants to run this command to confirm it returns an "×", that'd be great:
          Code:
          di "{c 215}"

          Comment


          • #6
            Nils: Yes, given news that it works for 11, and also across different varieties of Stata, that would be a good fix. I don't want to implement something and find that it is sensitive to the alphabet in use and/or Stata flavour chosen.

            Thanks for the signal.

            Comment


            • #7
              Originally posted by Nils Enevoldsen View Post
              Also, if some lovely person out there with Stata 11 (or before) wants to run this command to confirm it returns an "×", that'd be great:
              Code:
              di "{c 215}"
              Works in Stata 9.2, 10.1, and 11.2.

              Best
              Daniel

              Comment


              • #8
                So far, so good, but we're all in Western Europe and using a certain alphabet. I'd need e.g. people from China or Russia to tell me it works for them.

                Comment

                Working...
                X