Announcement

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

  • Adjust twoway plot to display full x-axis

    Hi Stata Users,

    I have a problem with my twoway graph. Unfortunately, the x-axis cuts off the label of 01jan2022 which looks really weird.
    I have tried to adjust it but unfortunately it didn't work.
    Do you have any suggestions on how to rescale the x-axis or the graph so that the total label of 01jan2022 gets displayed in the graph?

    Thank you very much in advance!

    Attached Files

  • #2
    maybe you want option margin(),
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte y int date
    1 20454
    1 20820
    1 21185
    1 21550
    1 21915
    end
    
    tw line y date, xla(,form(%td))
    
    tw line y date, xla(,form(%td)) graphr(margin(5 10 5 5))

    Comment


    • #3
      You should please give code and a data example. Fortunately, we can fake the latter.

      For four years, just about, 2018 to 2021, consider this self-contained script which contains various solutions.

      Code:
      clear
      set scheme s1color 
      set obs `=366 + 3 * 365'
      gen date = mdy(12,31,2017) + _n
      l in L
      format date %td
      gen whatever = 42
      
      * problem! 
      line whatever date, xtitle("") name(G1, replace) 
      
      * solution 1: add more space (noting that `=    ' evaluates a constant on the fly) 
      
      line whatever date, xsc(r(`=mdy(10, 1, 2017)' `=mdy(3, 31, 2022)')) xtitle("") name(G2, replace)
      
      * solution 2: don't do that, then 
      
      local text 
      local ticks 
      
      forval y = 2018/2021 {
      local text `text' `=mdy(7, 1, `y')' "`y'"
      local ticks `ticks' `=mdy(1,1,`y')'
      }
      * one more tick for Jan 1 2022 
      local ticks `ticks' `=mdy(1,1,2022)'
      
      line whatever date, xla(`text', tlcolor(bg)) xticks(`ticks', tlength(*5)) xtitle("") name(G3, replace)
      
      graph combine G1 G2 G3, col(1)
      Click image for larger version

Name:	years.png
Views:	1
Size:	16.7 KB
ID:	1650640


      Lots of details here.

      1. Which reader needs an x axis title here?

      2. You can ask that the x axis starts earlier and ends later.

      3. For this kind of data I prefer years to be shown as such in the middle of each interval (blank out those ticks) and end years to be shown as ticks (lengthen those ticks).

      The main trick here is written up at https://www.stata-journal.com/articl...article=gr0030

      See also https://www.stata-journal.com/articl...article=gr0079

      4. Other approaches are possible: e.g. change display format to avoid defaults such as1jan2018 and add a grid with key dates if they exist.

      Comment

      Working...
      X