Announcement

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

  • don't display missing dates in twoway line graph

    I am creating a line graph with the variable ratio. I would like my graph to show no line when data for that month is missing.

    twoway (line ratio date if treated == 1) (line ratio date if treated == 0), title("Test")
    graph export test.emf, replace

    date is a Stata date with a month format and 1 for each day:

    date
    1/1/2012 - Jan12
    2/1/2012 - Feb12
    3/1/2012 - Mar12

    Click image for larger version

Name:	graph_test.JPG
Views:	1
Size:	34.9 KB
ID:	1360876

    My data-set has missing values for certain months, but from the graph it looks like continuous data. Is it possible to show missing months as blank (no line)?

    Thank you for your assistance.


  • #2
    Just add the cmissing(n) option to your graph to skip those points.
    Code:
    twoway (line ratio date if treated == 1, cmis(n)) (line ratio date if treated == 0, cmis(n)), title("Test")
    Last edited by Oded Mcdossi; 19 Oct 2016, 16:25.

    Comment


    • #3
      Ah nice. I didn't know about cmissing(). For posterity, here's some data and an example:

      Code:
      clear
      set obs 60
      
      //Generating date variable
      local i = 0
      local cmd generate
      forvalues month = 1/12{
          forvalues year = 2011(1)2015{
              `cmd' datestr = "1/`month'/`year'" in `=`++i''
              local cmd replace
          }
      }
      generate date = date(datestr, "DMY")
      format date %d
      
      //Sorting on date
      sort date
      
      //Generating 'ratio' variable
      gen Ratio = rbeta(3,0.5)
      
      //Making some observations missing
      forvalues i = 1(9)60{
          replace date = . in `i'
          replace Ratio = . in `i'
      }
      
      //Generating a treatment indicator
      gen treated = mod(_n,2)
      
      //Plotting
      twoway (line Ratio date if treated==1, cmissing(n)) (line Ratio date if treated==0, cmissing(n)), ///
              ysc(r(0 1)) ylab(0(0.2)1, format(%9.1f)) xtitle("") xlab(, format(%tddd/nn/YY)) ///
              legend(pos(6) ring(1) col(2) lab(1 "Treatment") lab(2 "Control") size(3) region(c(none)))
      Last edited by Chris Larkin; 19 Oct 2016, 17:18.

      Comment


      • #4
        Thank you both! I very much appreciate it.

        Comment

        Working...
        X