Announcement

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

  • Change xlabel in a graph

    Hello everyone,

    I'm working with a time series data set and I'm making some graphs, using the tsline command. However I'm working with quarterly data and I'd like to change the xlabel of the graphs, from 2017q1, to T1-2017 (for every date...), however I don't know how to do it. I started doing:

    Code:
    tsline discriminacion, title("Evolución denuncias ingresadas (2017-2022)") subtitle("Discriminación") ytitle("Número de denuncias" " ") ylabel(0(100)500) xtitle(" " "Trimestres") xlabel(tq(2017q1) "T1-2017" tq(2017q2) "T2-2017" tq(2017q3) "T3-2017" tq(2017q4) "T4-2017" tq(2018q1) "T1-2018") recast(connected) tline(2020q2, lpattern(dash))
    However, the code isn't working. And also I think that the way I'm doing it, it isn't efficient at all, because the dates are from 2017q1, to 2022q3. I would really appreciate your help!

  • #2
    There are perhaps two problems here, one clearly is that your code is not working and the other may be that what you want is itself problematic.

    The way to get tsline to behave here is to specify a particular display format.

    Code:
    help datetime display formats
    and here the only real quirk is expecting a literal T which is natural enough in Portuguese, for example.

    Without a data example we can fake an example, here for 6 complete years.

    Code:
    * 2017q1 2022q4 
    clear 
    set obs 24 
    gen time = yq(2016, 4) + _n 
    
    format time %tq!T-q_CCYY
    
    gen whatever = rnormal()
    tsset time 
    
    tsline whatever, scheme(s1color) xla(228/251, ang(v)) name(QTS, replace)
    What you ask for can be met only by a busy mess with vertical axis labels.

    Click image for larger version

Name:	QTS.png
Views:	1
Size:	31.6 KB
ID:	1694606


    I recommend something more like this:

    Code:
    forval y = 2017/2022 { 
        local begin = yq(`y', 1) - 0.5 
        local ticks `ticks' `begin'
        local mid = yq(`y', 2) + 0.5 
        local labels `labels' `mid' "`y'"
    }
    
    local ticks `ticks' `=yq(2023, 1) - 0.5'
    
    local qmin = yq(2017, 1)
    local qmax = yq(2022, 4)
    
    forval q = `qmin'/`qmax' { 
        local text : di %tqq `q'
        local qla `qla' `q' "`text'"
    }
    
    line whatever time, scheme(s1color) ///
    xaxis(1 2) xla(`labels', tlc(none)) xtic(`ticks', tlength(*4) grid glc(gs8)) xla(`qla', axis(2) grid glc(gs12) glw(vthin)) xtitle("", axis(2)) xtitle("", axis(1)) name(QTS2, replace)
    The principles are easy, starting with ticks at the end of longer intervals and labels without ticks in the middle. The practice -- takes more practice.



    Click image for larger version

Name:	QTS2.png
Views:	1
Size:	45.4 KB
ID:	1694605



    Even this looks too complicated to me, but I wanted to show some technique. More at https://www.stata-journal.com/articl...article=gr0030


    Comment


    • #3
      Thanks so much Nick, it worked. However, how would you do it for a panel data set? Using the first method you showed me? I still have my data from 2017q1 to 2022q3.

      Code:
       
       2017q1 2022q4  clear  set obs 24  gen time = yq(2016, 4) + _n   format time %tq!T-q_CCYY  gen whatever = rnormal() tsset time   tsline whatever, scheme(s1color) xla(228/251, ang(v)) name(QTS, replace)
      Thanks!!

      Comment


      • #4
        For a panel dataset, the usual options apply, including

        1. Superimposing panel traces on a single graph.

        2. Separating panels, one to each graph.

        3. Separating panels, a few to each graph.

        4. Front-and-back plots. https://journals.sagepub.com/doi/ful...6867X211025838

        The time axis tricks apply across all these possibilities.

        Comment

        Working...
        X