Announcement

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

  • Adding dots to the top of a line in graph twoway

    Hi all, I am trying to put dots on certain months similar to the graph below. Do you know how to do this in twoway line? My current code looks like
    Code:
    twoway line goodservices activemonth_final if activemonth_final>=`=tm(2018m1)', xline(`=tm(2020m3)') xlabel(`=tm(2018m1)' (5) `=tm(2020m11)')
    So my time is in stata month day time and I would like to put dots on every March of each year. Let me know what you think!
    Click image for larger version

Name:	jpube.PNG
Views:	1
Size:	139.8 KB
ID:	1628835

  • #2
    One approach -- you can add scatter with an if clause for a variable that flags the desired observations as shown below.

    Code:
    clear
    
    set obs 60 
    gen month = tm(2014-12) + _n 
    format month %tmMonCCYY
    
    * march and april months 
    gen flag = cond(inlist(month(dofm(month)), 3,4), 1, 0)
    gen x = _n + rnormal()
    
    twoway line x month || scatter x month if flag

    Comment


    • #3
      Thank you!!

      Comment


      • #4
        Do you know what I would do if I also wanted to shade areas like they have in that graph?

        Comment


        • #5
          See
          https://blog.stata.com/2020/02/13/ad...series-graphs/

          Comment


          • #6
            See also https://www.stata-journal.com/articl...article=gr0067

            Comment


            • #7
              Thank you so much! I was able to do it using Nick's article. Thanks so much for all your help!

              Comment


              • #8
                Sorry....one more question that I am somehow not getting quite right. The following code makes the bars go as high as I want, but I will need them to go as low as -2,000. Is there a way to do this? Thanks!

                Code:
                gen upper4=5000
                local barcall4 upper4 activemonth_final if inrange(activemonth_final, `=tm(2018m6)', `=tm(2018m8)') | inrange(activemonth_final, `=tm(2019m6)', `=tm(2019m8)')
                twoway bar `barcall4', legend(label(1 "Months Out of School")) || line pcallspermonth modate if modate>=`=tm(2018m1)' & modate<=`=tm(2020m6)', xline(`=tm(2020m3)') xlabel(`=tm(2018m1)' (5) `=tm(2020m6)')  xtitle("Date") ytitle("Number of Allegations") legend(label(2 "# Services Over Time")) || scatter pcallspermonth modate if flag==1, legend(label(3 "April and May")) plotregion(margin(zero))

                Comment


                • #9
                  IIRC there is a base() option to twoway bar to be used to vary from the default of 0.

                  Comment


                  • #10
                    Thank you!!

                    Comment

                    Working...
                    X