Announcement

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

  • Line graphs: How do I begin plots/labels at the beginning of the x axis?

    Greetings,

    I'm running Stata 15.1 on OSX and using longitudinal data from the General Social Survey (1972-2016). Data for the variable I'm trying to graph is only available for the years 2002, 2006, 2010, and 2014. I'd like to examine the change in male and female responses across this time range. To do so, I entered the following:
    Code:
    twoway (line  SEXHAR_12MO_WOMEN SEXHAR_12MO_MEN year_sexhar12mo, lwidth(thick thick)), xlabel(2002 2006 2010 2014) legend(order(1 "Women" 2 "Men")) xtitle("Year") title("Last 12 Months") plotregion(fcolor(white)) graphregion(fcolor(white)) ytitle("Percent")
    This results in a line graph that looks like this:
    Click image for larger version

Name:	Last12mo.jpg
Views:	1
Size:	62.4 KB
ID:	1450604


    As you can see, the x labels and plots begin towards the end of the x-axis. If possible, I'd like them to reposition them towards the beginning of it. Can anyone explain to me how to do so? Thanks in advance.

    Example data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(SEXHAR_12MO_WOMEN SEXHAR_12MO_MEN year_sexhar12mo)
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
            . .02644833 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
            . .02644833 2002
    .06142507         . 2002
    .06142507         . 2002
    .06142507         . 2002
    end


  • #2
    When I've had similar problems, they've usually been the result of having overlooked values of the X axis variable that were not within the range I expected. Stata's pretty severe about the axis reflecting the full range of values; options that try to "trim" the outermost values on the axis inevitably fail. If you want to eliminate points from the plot you pretty much have to use an if clause or otherwise see to it the the plot command doesn't encounter them.

    So the first thing I would do is
    Code:
    tab year_sexhar12mo, missing
    to be sure there isn't an observation with a date value of 1960 or so, with, of course, missing values for the Y-axis variables.

    Comment

    Working...
    X