Announcement

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

  • Creating multiple vertical lines in one graph using dates and by

    In brief: within each subplot generated by by() using two-way line, I'd like to add possibly more than one vertical line corresponding to a specific date. Here's a sketch of my data structure:

    market date y event
    1 1/1/01 3 1/3/01
    1 1/2/01 2 1/3/01
    1 1/3/01 4 1/3/01
    1 1/4/01 3 1/3/01
    1 1/5/01 5 1/3/01
    2 1/1/01 6 1/4/01
    2 1/2/01 7 1/4/01
    2 1/3/01 7 1/4/01
    2 1/4/01 3 1/4/01
    2 1/5/01 3 1/4/01


    To create my basic graph, I use:

    twoway (line y date), by(market)

    My question: for each subplot in the resulting figure, how do I add a vertical line at "1/3/01" in the first subplot and at "1/4/01" in the second subplot? However, instead of having two distinct markets, I have a few hundred. My plan is to split the plotting statement across groups of markets, but I still need some automated way to add vertical lines (rather than just hardcoding the values in xline() myself for each market).

    I'm open to reconfiguring the data as necessary. If this has an easy solution, one wrinkle I'll mention is that I actually have "event1" and "event2", and so would eventually like to add multiple vertical lines to each subplot, preferably using different colors. I'm relatively new to graphing in Stata so figured this could be left to later...

    Many thanks and happy holidays,

    Brett




  • #2
    Ok, so I should have worked longer on this first. Here is my take on solving this. I would welcome any suggestions if there is a more elegant solution

    levelsof market, local(mlevels)
    tempfile file1
    save `file1', replace

    foreach val of local mlevels {
    use `file1' if market == `val', replace
    local e1 = event1[1]
    local e2 = event2[1] /* if I had a second event column */
    tsline y, tline(`e1', lcolor(red)) tline(`e2', lcolor(blue)) name(market_`dmaval')
    }​

    graph combine market_1 market_2

    The only wrinkle in the above is that I have to write out the graph combine statement, which will be annoying since I have 200 markets. But the rest seems to do most of what I need.



    Comment


    • #3
      I don't know what is more efficient: i) loading the data multiple times with a condition or ii) loading it once and inserting the condition within the -tsline- statement. If this is bothering you, you can try the second form:

      Code:
      clear
      set more off
      
      input ///
      market str15 date y str15(event1 event2)
      1 "1/1/01" 3 "1/3/01" "1/8/01"
      1 "1/2/01" 2 "1/3/01" "1/8/01"
      1 "1/3/01" 4 "1/3/01" "1/8/01"
      1 "1/4/01" 3 "1/3/01" "1/8/01"
      1 "1/5/01" 5 "1/3/01" "1/8/01"
      2 "1/1/01" 6 "1/4/01" "1/3/01"
      2 "1/2/01" 7 "1/4/01" "1/3/01"
      2 "1/3/01" 7 "1/4/01" "1/3/01"
      2 "1/4/01" 3 "1/4/01" "1/3/01"
      2 "1/5/01" 3 "1/4/01" "1/3/01"
      end
       
      gen date2 = date(date,"MD20Y")
      gen event11 = date(event1,"MD20Y")
      gen event22 = date(event2,"MD20Y")
      
      format %td date2 event11 event22
      
      drop date event?
      rename (date2 event11 event22) (date event1 event2)
      
      list
      
      xtset market date
      
      *-----
      
      levelsof market, local(mlevels)
      
      foreach val of local mlevels {
          
          // graphs
          tsline y if market == `val', ///
              tline(`=event1[1]', lcolor(red)) ///
              tline(`=event2[1]', lcolor(blue)) ///
              name(market_`val', replace)
          
          // build -graph combine- syntax
          local mysyntax `mysyntax' market_`val'
      }
      
      graph combine `mysyntax'
      You can build the syntax for -graph combine- within the loop. Use -forvalues- instead of -foreach-, if possible.
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment

      Working...
      X