Announcement

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

  • xscale with line graphs

    Hi,

    I have a time-series dataset with some variables and where time is in years (1870-2017). To be precise, I use the Schularick 2017 macro dataset.

    The problem I am facing is that line graphs I produce with the standard syntax (line y-var year) come with an x-axis starting in 1850 and ending in 2050. This makes the graphs too wide and more difficult to read.

    I tried the xscale command as well as the tscale options, for line, toway, and tsline commands. none worked, with different errors.

    Code:
    line gdp year, xscale(1900-2000) *option 1900-2000 not allowed
    tsline gdp, tscale(1900-2000) *option 1900-2000 not allowed
    I could not find yet an easy, intuitive solution for that problem, although my understanding is that it is a rather trivial problem?

    Thus, my question is, can someone suggest a simple code-line for a line-graph where I can set the x-axis in an intuitive way?

    I would like to eventually build this into a linecall code.

    best wishes,
    Chris

  • #2
    The correct syntax would be something like
    Code:
    xscale(r(1900(20)2000))
    But you may still run into problems because xscale will never reduce the scale such that data is omitted. So if you have data before 1900 or after 2000, you'll also need an if condition along the lines of
    Code:
    if year>1900 & year<2000

    Comment


    • #3
      I think Ali Atia is getting a little confused between xscale() and xlabel(), but the latter offers various solutions, as this code will show

      Code:
      clear
      set obs 148
      range year 1870 2017
      gen whatever = 42
      line whatever year
      line whatever year, xla(1870(20)2010) xtick(1880(20)2020)

      Comment


      • #4
        As far as I can tell, #1's main request was to limit the axis to between 1900 and 2000, which is best achieved with an if condition rather than either xscale or xlabel. Although my use of r(1900(20)2000) rather than r(1900 2000) would have been more appropriate in xlabel, in this case it luckily works out because Stata defaults to a 20 year interval anyway. Combining both answers might lead to a command like:

        Code:
        twoway line whatever year if year>1900 & year<2000, xla(1900(20)2000) xtick(1900(10)2000)
        Last edited by Ali Atia; 26 May 2021, 11:58.

        Comment


        • #5
          Ali Atia I agree: if the question is to limit what is shown to years 1900 to 2000, then you need an if condition and -- indeed -- neither xscale() nor xlabel() will have the side-effect of omitting data.

          Comment


          • #6
            Thank you so much for your replies!

            I indeed wanted to know both, how to omit years and how to reduce the x-axis scale.

            The
            if condition is an obvious, and indeed super neat, solution for omitting years. I think I lacked the correct terminology to find this solution by myself. Thanks for showing me the solution.

            The xlabel()command does the trick to reduce the x-axis, so that the x-axis goes from 1870-2017 (with data-set ranging from 1870-2017) instead of the x-axis going from 1850-2050. This can be seen very nicely in Nick's example. Thanks a lot for showing me this!

            On a sidenote, I am still wondering about the xscale()command. With Nick's "whatever" code, the xscale()option does not change the x-axis. Also with my data, I have not managed to change the x-axis with the xscale()option.

            Comment


            • #7
              xscale() is an option (as you say), not a command (as you also say). That's terminology, but what it is; in Stata options are always and only ever details of commands.

              We've already pointed out that xscale() does not ever omit data; at most it lengthens an axis; See discussion at

              Code:
              help axis scale options

              Comment


              • #8
                Hi,

                I actually never solved the issue and gave up on it in the meantime. Although I would still be interested to solve the issue. The issue remains the same as above: I have panel data from 1870-2015. xtline produces graphs where the x-axis (time) ranges from 1850-2050. There is not a single data point between 1850-1870, or between 2017-2050. Is there a way to produce a graph where the x-axis starts at 1850, and ends at 2017?

                thank you so much in advance.

                Click image for larger version

Name:	Screen Shot 2022-12-05 at 21.38.07.png
Views:	1
Size:	595.8 KB
ID:	1692147

                Comment


                • #9
                  The solution is what was said earlier. I just copied and pasted the code at #3 with a trivial change and got labels as specified.

                  Code:
                  clear set obs 148 range year 1870 2017 gen whatever = 42 line whatever year line whatever year, xla(1870(25)2020)
                  I confirm that without the
                  xla() call you get labels as in #8. If you can't reproduce this then we need to see the code you are using. We may also need to see the results of summarize on your data.

                  Comment


                  • #10
                    Sorry that #9 got mangled (which was the forum software's fault) and that I didn't check it (which was my fault).

                    This script shows some possibilities and you can run it yourself.

                    Code:
                    clear
                    set scheme s1color
                    
                    set obs 148
                    range year 1870 2017
                    gen whatever = sum(runiform())
                    
                    line whatever year, name(G1, replace)
                    
                    line whatever year, xla(1850(25)2000 2017) name(G2, replace)
                    Stata's default for your data is based on a preference for "nice numbers". The precise algorithm that (official) Stata uses is a little complicated but a first approximation is as explained in

                    Hardin, J.W. 1995. Calculate nice numbers for labeling or drawing grid lines. Stata Technical Bulletin 25, 2-3. (Also in STB Reprints Volume 5, 19-20.) https://www.stata.com/products/stb/journals/stb25.pdf whereby Stata looks for multiples of 1, 2 or 5.

                    1850(50)2050 (5 labels) is chosen in preference to 1860(20)2020 or 1840(20)2020 (9 or 10 labels). My suggestion is in the last command above.
                    Last edited by Nick Cox; 06 Dec 2022, 03:01.

                    Comment


                    • #11
                      Thank you very much for your reply.

                      your code indeed works, and the graph now looks like this, which is quite close to what I want (first graph). However, and this might also be interesting for others, using grstyle package for graphs seems to overwrite your xla suggestion, producing again a graph that's too wide (second graph below).

                      the code is:
                      Code:
                      grstyle init
                      grstyle set legend , nobox stack
                      grstyle set imesh, horizontal compact minor
                      grstyle set color hue, n(17): p#lineplot
                      xtline hpr , overlay ytitle(Real House Prices) xtitle(Year) xla(1870(15)2015 2020)  legend( position(6) size(small) cols(8))
                      summarize year gives:
                      Click image for larger version

Name:	Screen Shot 2023-01-20 at 16.01.46.png
Views:	1
Size:	24.3 KB
ID:	1698021


                      First Graph with
                      set scheme s1color:
                      Click image for larger version

Name:	Screen Shot 2023-01-20 at 15.56.22.png
Views:	1
Size:	429.7 KB
ID:	1698022
                      Second graph with code above and grstyle:
                      Click image for larger version

Name:	Screen Shot 2023-01-20 at 15.59.58.png
Views:	1
Size:	459.5 KB
ID:	1698023


                      any ideas or solutions?
                      many thanks for your help and support with this.

                      Comment


                      • #12
                        grstyle (source please) is a fine package I have never used, so I will pass on this.

                        Comment

                        Working...
                        X