Announcement

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

  • line graph with "misleading" horizontal axis

    I would like to make a simple line graph of tax rates by income levels. However, the intervals between income levels are idiosyncratic; they are $20,000, $35,000, $50,000, $75,000, $150,000, $250,000, $500,000, and $1,000,000. When I graph these, Stata squishes most of the income levels at the left end of the graph, but I would like each income level spaced evenly on the horizontal axis, as though they are categorical.

    The command I used is

    Code:
    twoway (bar etr agi, sort), xlabel(20000 35000 50000 75000 100000 150000 250000 500000 1000000)
    Below are the data.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float etr long agi
            0   20000
     .5114286   35000
         3.13   50000
        4.128   75000
        4.425  100000
    4.6953335  150000
       4.8476  250000
        4.818  500000
       5.2106 1000000
    end

  • #2
    Correction: the relevant command is "line", not "bar". See below.

    Code:
    twoway (line etr agi if state=="Virginia", sort), xlabel(20000 35000 50000 75000 100000 150000 250000 500000 1000000)

    Comment


    • #3
      Correction 2: given the data example, the "if" condition should be omitted.

      Code:
      twoway (line etr agi, sort), xlabel(20000 35000 50000 75000 100000 150000 250000 500000 1000000)

      Comment


      • #4
        Code:
        ssc install sencode, replace
        tostring agi, gen(agi_s)
        sencode agi_s, gen(agi_n) gsort(agi)
        twoway (line etr agi_n), xla(1/9,valuelabe)
        Note: code installs and uses Roger Newson's -sencode- from SSC

        Comment


        • #5
          That works well, and is a simple fix. Thank you Ali!

          Comment


          • #6
            Here's another way to do it. Note also labmask (Stata Journal).


            Code:
            clear
            input float etr long agi
                    0   20000
             .5114286   35000
                 3.13   50000
                4.128   75000
                4.425  100000
            4.6953335  150000
               4.8476  250000
                4.818  500000
               5.2106 1000000
            end
            
            gen x = _n 
            
            forval i = 1/9 { 
                label def x `i' "`=agi[`i']'", add 
            }
            
            label val x x  
            
            line etr x, xla(1/9, valuelabel) xtitle(agi)

            Comment


            • #7
              Thank you Nick. This is nice, and doesn't require creating any additional variables. The only drawback from my perspective is that I would like to add commas to the labels to make them easier to read; e.g., "1,000,000" instead of "1000000". I tried formatting agi as %9.0fc before creating the labels, but that did not change the labels.

              Comment


              • #8
                You need to fix that upstream when defining value labels in the loop. But you don’t have much space to add lots of commas.

                Comment


                • #9
                  Like this:

                  Code:
                  clear
                  input float etr long agi
                          0   20000
                   .5114286   35000
                       3.13   50000
                      4.128   75000
                      4.425  100000
                  4.6953335  150000
                     4.8476  250000
                      4.818  500000
                     5.2106 1000000
                  end
                  
                  gen x = _n 
                  
                  forval i = 1/9 { 
                      local this : di %9.0fc agi[`i']
                      label def x `i' "`this'", add 
                  }
                  
                  label val x x  
                  
                  line etr x, xla(1/9, valuelabel labsize(small)) xtitle(agi) xsc(r(. 9.2))
                  Shouldn't the connect() style be c(J)?

                  Comment

                  Working...
                  X