Announcement

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

  • Force align two x axes to same scale

    I am working with time series data (using Stata 14.0) and trying to show three scatterplots on the same graph. They use two different, but closely related, horizontal axes. I would like to force align the two horizontal axes in a way that makes their relationship clear. I’ve searched Statalist and other resources to try to find the solution, but to no avail; I’m not sure if this means there is no solution or I just don’t know what terms to use.

    I have provided a small sample dataset at the bottom of this message, using -dataex-. I also provided my code for the graphs, below.

    The three graphs are:

    (a, b) the value of an outcome for every observation, where vertical axis measures the value of the outcome and horizontal axis measures the date of the observation.

    (c) the average value of all outcomes within a given 90-day period, as a scatterplot with a connecting line. Vertical axis is identical to (a) and (b), but horizontal axis is the 90-day period, defined as quarters before or after the treatment date. Thus, if the treatment date for a group is 19359, then within that group an observation has qfromtreatdate=0 if inrange(date, 19359,19448) and qfromtreatdate=1 if inrange(date,19449,19538), and so on.

    (Scatterplots (a) and (b) are different because I am using two different colors to show treated and control observations. This is not the source of the problem… in fact it helps make the problem visually apparent from the graph.)

    For the data in this example, the treatment date is 19359, so I would like the 0 in the upper axis to line up directly over where 19359 would appear on the lower axis (no need to make this show up as a tick in the lower axis). That would also mean the rose dots would all be to the right of 0 on the upper axis (or exactly at 0), and the blue dots would all be to the left.

    Is this possible and if so, how? Many thanks.

    Code for graph:
    Code:
    sort qfromtreatdate
    tw           (scatter outcome date if treat==0, xaxis(1) mcolor(ebblue) msize(vsmall)) ///
                    (scatter outcome date if treat==1, xaxis(1) mcolor(erose) msize(vsmall)) ///
                    (scatter avg_outcome_per_q qfromtreatdate, connect(1) xaxis(2) msize(small))
    Sample data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float outcome byte(treat qfromtreatdate) float avg_outcome_per_q int date
     .59332687 0 -1  -.6042102 19284
     .59332687 0 -1  -.6042102 19297
      6.802406 0 -1  -.6042102 19299
     -.6733446 0 -1  -.6042102 19320
     2.2907097 0 -1  -.6042102 19331
    -4.6051702 0 -1  -.6042102 19341
     3.5822265 0 -1  -.6042102 19358
    -4.6051702 0 -1  -.6042102 19358
      1.841884 1  0 -.11875007 19359
    -.09431064 1  0 -.11875007 19359
      3.656098 1  0 -.11875007 19359
     -2.207275 1  0 -.11875007 19360
     4.4648733 1  0 -.11875007 19360
    -4.6051702 1  0 -.11875007 19380
     -1.486424 1  1   .3863915 19488
    end

  • #2
    One option is to center the date x-axis on on 19359 (create a new variable equal to date minus 10359) and then re-label the ticks on the x-axis with the appropriate dates:
    ....xlab(0 "19359" 50 "..." 100 "..." -50 "..." ...)

    We can automate the generation of that line with a loop (see Stata tip 59: Plotting on any transformed scale by Nick Cox):

    **NOTE: the connect() suboption is l --lower case el, as in line-- not the number one

    Code:
    gen resc_date=date-19359
    
                    
    foreach i of numlist -100(50)150 {
        local j= 19359-`i'
        local xlab `xlab' `i' `"`j'"'
        }                
        
        
    tw           (scatter outcome resc_date if treat==0, xaxis(1) xlab(`xlab') mcolor(ebblue) msize(vsmall)) ///
                    (scatter outcome resc_date if treat==1, xaxis(1) xlab(`xlab')  mcolor(erose) msize(vsmall)) ///
                    (scatter avg_outcome_per_q qfromtreatdate, connect(l) xaxis(2) msize(small))
    Last edited by Carole J. Wilson; 14 Apr 2016, 22:13. Reason: Added cite to Stata tip 59
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment

    Working...
    X