Announcement

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

  • how to force a certain value range in xlabel?

    I have a year variable in my longitudinal data and I am trying to use xtline to plot a graph. year is my time variable and company is the id variable. While using the command

    "xtline var1 if var2==5, t(year) i(company) overlay" I expect the values of the variable year (1995 to 2016) to appear on the x axis of the graph. However, what is displayed are values from 1 to 21. when I do a tab command (tab year) it correctly shows the years as different values for the year. When I go to the data sheet also, it displays values from 1995 to 2016. But, when I do "codebook year" it returns range as "range: [1,21]"

    Can some one tell me how can I force xtline to display the actual years on the x axis instead of these values from 1 to 21.

    I tried adding "xlabel(1995(2)2016)" option with the xtline command. That is not giving the desired output. It essentially displays the lines corresponding to 1 to 21 in the beginning of the graph and leaves a huge blank space in the graph after 21 with values 1995-2016 clustered at the end of x axis.

    Thaks in advance for your help.

  • #2
    Please show us the result of

    Code:
    summarize year
    Perhaps your data are 1 to 21 and you need to add 1994.

    In any case, if you think you have years 1995 to 2016 that would be 22 distinct values, not 21 values.

    Comment


    • #3
      Originally posted by Nick Cox
      Please show us the result of

      Code:
      summarize year
      Code:
      sum year
      
          Variable |       Obs        Mean    Std. Dev.       Min        Max
      -------------+--------------------------------------------------------
              year |       990        11.5    6.347495          1         22
      The year variable was generated while reshaping the data from wide to long and when I tabulate year it shows all the years from 1995 to 2016. Moreover the year values (1995 to 2016) are also shown in the data editor as well. I have not added these years as value labels.

      Comment


      • #4
        Very likely your values 1 to 22 have value labels 1995 to 2016. So, for your values to be 1995 to 2016 you need to add 1994, as I surmised in #2.

        Code:
        replace year = year + 1994

        Comment


        • #5
          Originally posted by Nick Cox;
          Very likely your values 1 to 22 have value labels 1995 to 2016. So, for your values to be 1995 to 2016 you need to add 1994, as I surmised in #2.

          Code:
          replace year = year + 1994
          Thanks Nick. The solution did work. However, I am still confused as to how these year variables got values assigned from 1 to 22! All I did to create the year variable was

          Code:
          reshape long var1, i(companyname) j(time) string
          encode time, gen(year)
          Obviously, the wide data had variables like var11995 var11996.....var12016.

          Anyways, thanks for the solution.

          Comment


          • #6
            The -encode- command takes string variables and converts them to numeric integer variable, starting at 1 and counting up, and assigns value labels to them. Do read -help encode- for more details.

            Had you not specified the -string- option in your -reshape- command, time would have come in as a numeric variable with values 1995, 1996, ..., 2016 in the first place. Given that you did use -string-, because the strings you got are actually the human-readable versions of the numbers you want to calculate with, converting them to numeric should have been done with -destring-, not -encode-. -destring time, replace- would have given you a numeric variable with values 1995, 1996, etc.

            Many people confuse these commands. Do review the corresponding manual sections. And, until you get used to them and instinctively understand which to use when, I recommend that each time you are tempted to use either -destring- or -encode- you first check the manual to be sure.

            Comment


            • #7
              Thanks.. Really appreciate your response. That solves my confusion.

              Comment


              • #8
                Note that showing us a sample of your data as requested in http://www.statalist.org/forums/help#stata would have shown the problem immediately.

                Comment

                Working...
                X