Announcement

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

  • Missing ticks and labels with graph twoway and yscale

    Kia ora - when I run the following commands the resulting graph does not automatically fill in ticks and labels between 0 and 20 on the y-axis. I can specify ticks and labels using axis scale options within graph twoway, but I have a number of similar graphs I want to produce and I would like Stata to automatically produce ticks and labels on the y-axis. The reason I am using graph twoway instead of graph bar is so I can include an additional plot of confidence intervals overlain over the bars (as shown in http://www.ats.ucla.edu/stat/stata/faq/barcap.htm).

    sysuse auto
    collapse (mean) mpg, by(foreign)
    graph twoway (bar mpg foreign), yscale(range(0))

    Has anyone encountered this issue before and has a solution? Thanks for your help, Jonathan

  • #2
    Beware of dynamite: http://biostat.mc.vanderbilt.edu/wik...de/Poster3.pdf
    See also discussions of detonator plots here.

    Code:
    sysuse auto, clear 
    collapse (mean) mpg, by(foreign)
    graph twoway (bar mpg foreign) , ysc(range(0 .)) yla(0(5)25)

    Comment


    • #3
      Thank you for your reply, Nick. I will take your advice regarding detonator plots into consideration. Regarding my original post, I am curious to know if Stata can automatically determine appropriate ticks and labels to use with graph twoway? I have written a do-file that creates 40 graphs similar to the example I posted, and the y-axis range varies considerably from graph to graph. I don't want to specify the ylabels for each graph within the do-file. Cheers, Jonathan

      Comment


      • #4
        For a similar problem see #6 in http://www.statalist.org/forums/foru...on-top-of-plot

        James Hardin's command nicenum (STB) seems to do what you need. For bar charts of your kind I'd insist on zero being a label. Here is nicenum applied to the auto data.

        Code:
        sysuse auto , clear
        
        foreach v of var price-foreign {
            nicenum labels = 0 `v'
            di "`v'{col 20}" $labels
        }
        
        price              0 5000 10000 15000 20000
        mpg                0 10 20 30 40 50
        rep78              0 1 2 3 4 5
        headroom           0 1 2 3 4 5
        trunk              0 10 20 30
        weight             0 1000 2000 3000 4000 5000
        length             0 100 200 300
        turn               0 20 40 60
        displacement       0 100 200 300 400 500
        gear_ratio         0 1 2 3 4
        foreign            0 .2 .4 .6 .8 1
        nicenum still works fine after 20+ years. Now many Stata programmers would prefer its output to be a saved result or a local macro in the caller program's space, but those weren't standard or even possible when James wrote it. Also, what display filters out here are some commas in the macro which sometimes need to be removed, but that is easy enough.

        You clearly need to install nicenum before you can use it.

        Code:
        search nicenum
        will point to its download location.

        So, you cycle over your variables, call up this program and insert the global macro in a call to your labels option. As said, its commas need to be removed (and replaced with spaces).

        Here is a silly example to draw bar charts of mean whatever by foreign in the auto data.

        Code:
        sysuse auto, clear
        
        foreach v of var price-gear_ratio {
            egen means = mean(`v'), by(foreign)
            nicenum labels = 0 means
            global labels : subinstr global labels "," " ", all
            twoway bar means foreign, yla($labels)
            more    
            drop means
        }
        A long-standing but slow-moving project of mine is a more general program to do nice numbers for logarithmic, square root, cube root, reciprocal, logit and folded root scales as well as raw scales. That may look like an arbitrary list, except that it's my favourite transformations.
        Last edited by Nick Cox; 24 Aug 2016, 02:52.

        Comment


        • #5
          Brilliant! nicenum worked perfectly. Thanks for your help.

          Comment

          Working...
          X