Announcement

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

  • Line plot with event markers

    I have daily stock price data for many different companies.
    For each company, there is at least one "event" date that I am interested in.
    I would like to create a line plot for a given company (group_id), that has a marker only on the event date(s).

    So far the only solutions I found were:
    1. I defined a binary event variable (equal to 1 for an event, and 0 otherwise) and used it as a weight, which leads to a huge circle around the event datapoint. It is almost impossible to identify the exact point it is circling.
      Code:
      scatter price date [w=event] if group_id == 1, msymbol(Oh) connect(l)
    2. I defined the event variable to equal "event" for an event, and "" otherwise, and used it as a label, which is also hard to pinpoint when looking at over a year of data.
      Code:
      scatter price date if group_id == 1, mlabel(event) msymbol(none) connect(l)
    Optimally, I would like a small dot/circle on event datapoints.

    Please let me know if this is feasible.

    Thank you so much!

  • #2
    Like this? It uses the letter "o" for a circular marker on the 123rd observation.
    Code:
    sysuse sp500
    generate eventmarker=""
    replace eventmarker="o" in 123
    twoway (connected open date, msymbol(none) mlabel(eventmarker) mlabsize(huge) mlabcolor(cranberry))
    David Radwin
    Senior Researcher, California Competes
    californiacompetes.org
    Pronouns: He/Him

    Comment


    • #3
      This is similar to the second solution I tried.
      The "o" is still not circling the actual point, but is centered off to the side.
      It seems I will have to settle for a workaround.

      Thank you!

      Comment


      • #4
        Yehuda Davis You can use the if qualifier to add reference to specific events.
        Code:
        sysuse sp500, clear
        g event=mod(_n,8)==1
        twoway line open date || scatter open date if event==1, mcolor(cranberry)

        Comment


        • #5
          Oded Mcdossi That's perfect!

          I didn't realize you could overlay two plots like that.

          Thank you so much!

          Comment


          • #6
            You can specify where to position the label using mlabpos, including right on top of the data point, as in this example:
            Code:
            sysuse sp500
            generate eventmarker=""
            replace eventmarker="o" in 123
            twoway (connected open date, msymbol(none) mlabel(eventmarker) mlabsize(huge) mlabcolor(cranberry) mlabpos(0))
            David Radwin
            Senior Researcher, California Competes
            californiacompetes.org
            Pronouns: He/Him

            Comment


            • #7
              Cool! Thanks!

              Comment

              Working...
              X