Announcement

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

  • What is Stata's full algorithm for automatically labelling axes on plots?

    Hi all,

    For reasons that aren't really worth going into, I'm trying to reproduce the algorithm that Stata uses to automatically label plot axes. I understand from here and here that the first bit of the algorithm is to start with the range of the variable and divide it into (about) 5 equally spaced labels. But I think there are other steps n the process, because it also always chooses round numbers (at an appropriate order of magnitude) to display. I'm wondering, is there a command that will produce these values for you automatically for any variable? Or a standard algorithm I could implement?

    My instinct was to calculate the range of the variable and divide it into 5 groups, and then maybe use the size of those groups and the floor function in some combination to determine the right level of rounding. By I'm a little stuck at that point. Any thoughts or guidance?

  • #2
    I suggest that you install the user community contributed package mylabels from Nick Cox, Durham University, like:
    Code:
    ssc install mylabels, replace
    h mylabels
    and consult the references that are included in the help file.
    You might want to read the ado file itself for inspiration.
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Thanks for the mention of mylabels, but nicelabels -- also currently best downloaded from SSC -- is the more relevant command here.

      Stata's algorithm is close to but not identical to the nice numbers algorithm of Paul S. Heckbert. nicelabels is in turn an implementation of that algorithm, and I know that Stata' algorithm isn't identical to Heckbert's because nicelabels occasionally gives different answers.

      See also

      https://www.statalist.org/forums/for...dable-from-ssc

      my recent London talk https://www.stata.com/meeting/uk22/slides/UK22_Cox.pptx

      and (if you are reading this is 2023 or later) my paper in Stata Journal 22(4)

      Comment


      • #4
        If you just want to reproduce Stata's choice of scale, then you can us _natscale. It is build-in, so you cannot see the algorithm. However, if you just need to numbers, then _natscale will give them to you.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Many thanks, all. This is a big help!

          Comment

          Working...
          X