Announcement

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

  • Editing the x-axis via code

    Hello,

    I have a basic understanding of graphs and have been making changes via the Graph editor.
    Following is the starting command for a series of graphs I have been using:

    Code:
    twoway (line Made Year2)(line Ended Year2), ytitle(Number of persons)
    However, periodically I need to update these graphs and was seeking advice regarding
    how I would perform the following tasks via code:

    1) drop the leading 0 (currently edited via range/delta) from the data below

    2) select the number of ticks on the x axis

    3) Include only odd or even values for the x axis and rename these (e.g 1 becomes 2011)



    Any assistance is appreciated

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 Year byte(Made Ended) long Year2
    "11_1" 54 60  1
    "11_2" 58 39  2
    "12_1" 45 43  3
    "12_2" 70 72  4
    "13_1" 54 48  5
    "13_2" 56 29  6
    "14_1" 38 41  7
    "14_2" 63 45  8
    "15_1" 60 42  9
    "15_2" 62 33 10
    end
    label values Year2 Year2
    label def Year2 1 "11_1", modify
    label def Year2 2 "11_2", modify
    label def Year2 3 "12_1", modify
    label def Year2 4 "12_2", modify
    label def Year2 5 "13_1", modify
    label def Year2 6 "13_2", modify
    label def Year2 7 "14_1", modify
    label def Year2 8 "14_2", modify
    label def Year2 9 "15_1", modify
    label def Year2 10 "15_2", modify

  • #2
    I am not completely clear on what you want, but these look like half years, so I would advise working with half-yearly dates and then choosing the display format you want. Some technique:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 Year byte(Made Ended) long Year2
    "11_1" 54 60  1
    "11_2" 58 39  2
    "12_1" 45 43  3
    "12_2" 70 72  4
    "13_1" 54 48  5
    "13_2" 56 29  6
    "14_1" 38 41  7
    "14_2" 63 45  8
    "15_1" 60 42  9
    "15_2" 62 33 10
    end
    label values Year2 Year2
    label def Year2 1 "11_1", modify
    label def Year2 2 "11_2", modify
    label def Year2 3 "12_1", modify
    label def Year2 4 "12_2", modify
    label def Year2 5 "13_1", modify
    label def Year2 6 "13_2", modify
    label def Year2 7 "14_1", modify
    label def Year2 8 "14_2", modify
    label def Year2 9 "15_1", modify
    label def Year2 10 "15_2", modify
    
    
    gen Hyear = yh(2010, 2) + Year2
    format Hyear %th_CY
    line Made Ended Hyear, xla(`=yh(2011,1)'(2)`=yh(2015,2)') xtitle("")

    Comment


    • #3
      Hello NIck,

      Thanks. The data are indeed values at the end of each semester (June 30 and December 31 for the years 2011- 2015). I need to test the code against actual data graph but your educated guess does exactly what I want. As a modification, if I want to label each six month period on the x-axis, how would I amend your code?

      I'd seen various commands like ttitle and xlabel but these seemed to work with specific graph formats.

      Regards

      Bob

      Comment


      • #4
        Something like

        Code:
        format Hyear %th_CY_h  
        line Made Ended Hyear, xla(`=yh(2011,1)'/`=yh(2015,2)') xtitle("")
        See also http://www.stata-journal.com/sjpdf.h...iclenum=gr0030
        Last edited by Nick Cox; 02 Feb 2016, 05:21.

        Comment


        • #5
          Many thanks. A quick test suggests the semester (e.g.1) needs to be in a smaller font or demarcated from the year in some way 2011 -1 or 2011 #1 . Neither of these symbols are accepted. I'll read the article in the morning.

          Regards

          Bob

          Comment


          • #6
            Indeed. It was clear even from your small example that there isn't enough space to label each half-year easily. The paper has an example of showing quarters only within yearly intervals, which we could adapt.

            The idea is to put ticks at year ends and yearly labels (with invisible ticks) in between.

            For half-years as well as other kinds of dates Stata has an origin of 0 for the first date in 1960. So the first half-year of 1960 is 0 and the second half-year is 1. The first half of any year will always be an even number date and the second half an odd number date. Therefore we should place ticks at even numbers - 0.5, meaning -0.5, 1.5, 3.5 or whichever similar numbers occur in your dataset.

            The first and last ticks to be shown can be calculated from the data, with separate logic if the first (last) half-yearly date is odd (even). Tedious but not rocket surgery.

            Similarly we should show yearly labels half-way between each two half-year dates.

            Code:
            clear
            input str4 Year byte(Made Ended) long Year2
            "11_1" 54 60  1
            "11_2" 58 39  2
            "12_1" 45 43  3
            "12_2" 70 72  4
            "13_1" 54 48  5
            "13_2" 56 29  6
            "14_1" 38 41  7
            "14_2" 63 45  8
            "15_1" 60 42  9
            "15_2" 62 33 10
            end
            label values Year2 Year2
            label def Year2 1 "11_1", modify
            label def Year2 2 "11_2", modify
            label def Year2 3 "12_1", modify
            label def Year2 4 "12_2", modify
            label def Year2 5 "13_1", modify
            label def Year2 6 "13_2", modify
            label def Year2 7 "14_1", modify
            label def Year2 8 "14_2", modify
            label def Year2 9 "15_1", modify
            label def Year2 10 "15_2", modify
            
            /// Stata half-yearly dates
            gen Hyear = yh(2010, 2) + Year2
            
            /// where to put the ticks?
            /// find smallest and largest values and go outwards
            /// mod(number, 2) is 1 (true) if number is odd
            su Hyear, meanonly
            local tmin = cond(mod(r(min), 2), r(min) - 1.5, r(min) - 0.5)
            local tmax = cond(mod(r(max), 2), r(max) + 0.5, r(max) + 1.5)
            
            /// where to put the labels? each is plotted halfway in interval
            gen CYear = yofd(dofh(Hyear))
            levelsof CYear, local(years)
            
            local call
            foreach y of local years {
                local pos = (yh(`y', 1) + yh(`y', 2))/2
                local call `call' `pos' "`y'"
            }
            
            twoway connected Made Ended Hyear, ///
            xtick(`tmin'(2)`tmax', tlength(*2)) ///
            xtitle("") ///
            xlabel(`call', tlength(*0.5) tlcolor(none))
            Click image for larger version

Name:	halfyearly.png
Views:	1
Size:	11.3 KB
ID:	1325160


            Last edited by Nick Cox; 02 Feb 2016, 06:33.

            Comment


            • #7
              Hello Nick,

              Many thanks. I can now see that the quite a bit of code is required to produce what can be done via the Graph Editor.The symbols make it clear what is happening within a year so that makes interpretation easier than what I had one.


              Bob

              Comment


              • #8
                Indeed. I didn't comment on that but I used twoway connected because I thought the little extra emphasis -- these two points fell within this year -- should be useful.

                Comment


                • #9
                  It was useful. Thanks.

                  Comment

                  Working...
                  X