Announcement

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

  • How to move the position of x labels?

    In the attached graph, x labels (2017m1 - 2020m1) are placed below the ticks. I wanted to move the position of x labels so that they are arranged between the ticks. Any advice would be appreciated.

    Graph.gph

  • #2
    See this Stata tip by Nick Cox: https://journals.sagepub.com/doi/pdf...867X0800700410
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Please read https://www.statalist.org/forums/help#stata which explains (1) please do give a data example (2) please do post graphs as .png not .gph. The forum software doesn't understand Stata file formats!

      The sample graph is 3 years or so with two monthly time series starting in January 2017. So, we can fake some data to show technique.

      Code:
      clear
      set obs 36
      set seed 314159
      gen mdate = ym(2016, 12) + _n
      * format mdate %tm
      gen y = rnormal(2, 1) + 0.1 * mdate
      
      foreach y in 2016 2017 2018 2019 {
          if `y' > 2016 local mid = ym(`y', 6) + 0.5
          local mids `mids' `mid'
          
          local end = ym(`y', 12) + 0.5
          local ends `ends' `end'
      }
      
      
      levelsof mdate if inlist(month(dofm(mdate)), 1, 7), local(levels)
      
      line y mdate, xaxis(1,2) xla(`levels', axis(2) nogrid format(%tmMon)) xla(`mids', format(%tmCCYY) nogrid noticks axis(1)) xli(`ends', lp(solid) lw(thin) lc(gs12)) xtick(`ends', tlength(*3)) xtitle("", axis(1)) xtitle("", axis(2))
      Notes:

      1. Attaching a monthly date format to the monthly date variable messes up the rest of the trickery, and I don''t know why. Any way, although that is a good idea otherwise, here I comment that out.

      2. The main idea otherwise is to show year labels in the middle of each year -- calculated as halfway between month 6 (June) and month 7 (July) -- plus grid lines at the beginning or end of the year, calculated as halfway between month 12 and the following month 1. You may need extra code to cope with the ends of the data. Adding 0.5 is the way to keep your grid distinct from your data.

      3. Entirely optional is to add some text indicating (some of) the months. (36 or so labels for 36 or so months would be over the top.)

      4. A pet peeve of mine (I have others) is that if you have a clear time labelling, then a corresponding axis title is unnecessary, even if it is a simple title like "Year". Stata variable names like mdate are fine between you and Stata but neither useful nor entertaining for graph readers.

      5. For more details see https://journals.sagepub.com/doi/pdf...36867X19874264
      Click image for larger version

Name:	intervals.png
Views:	1
Size:	36.4 KB
ID:	1714363




      (In the graph posted in #1 there are spurious backwards connections implying that the data are not quite in sort order.)
      Last edited by Nick Cox; 21 May 2023, 04:41.

      Comment


      • #4
        (If the data ended at the end of 2019, I might add a Dec. label for 2019.)
        Last edited by Nick Cox; 21 May 2023, 05:03.

        Comment

        Working...
        X