Announcement

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

  • How can I use Stata to plot a graph like this?

    Dear all,
    Can I use Stata to plot a graph to visualize the treatment statuses of each observation in a panel dataset.For example,in the picture below,State "ME" "MN" "WI" are treated in 1976.So,before 1976,the color is blue and after 1976 the color is dark blue.the control states' color is light blue.
    Does Stata have a command to plot this kind of graph?

    best regards
    Raymond
    Attached Files
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

  • #2
    You could look at heatplot by Ben Jann (ssc desc heatplot). You would probably have to create your own State variable that is in the required order.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Here is a way with twoway range bars. I also use labmask from the Stata Journal by Nick Cox.

      Code:
      sysuse census, clear
      gen treated= regexm(state2, "(CT|MT|IA|WY|NH|ID|WI|MN|ME)")
      expand 24
      bys state2: gen year=_n
      gen val= cond(!treated, 3, cond(treated &year<=14, 2, cond(inlist(state2, "ID", "NH", "WY") ///
      &inrange(year, 15,19), 2,cond(inlist(state2, "IA", "MT") &inrange(year, 15,22), 2, ///
      cond(state2=="CT" &inrange(year, 15,23), 2, 1)))))
      
      gen order= cond(state2=="CT", 42, cond(state2=="MT", 43,cond(state2=="IA", 44, ///
      cond(state2=="WY", 45,cond(state2=="NH", 46,cond(state2=="ID", 47, ///
      cond(state2=="WI", 48,cond(state2=="MN", 49,cond(state2=="ME", 50,.)))))))))
      
      gsort treated -state  
      replace order=sum(state!=state[_n-1]) if missing(order)
      labmask order, values(state2)
      
      
      bys state2 (year): gen end=_n*5
      gen start=end-5
      local xlab
      local year 1920
      forval i=5(5)`=24*5'{
          local xlab "`xlab' `=`i'-2.5' `" "`year'" "'"
          local year = `year'+4
      }
      
      tw  (rbar start end order if val==3, horiz barw(0.9) lc(white) fc(blue%22)) ///
      (rbar start end order if val==2, horiz barw(0.9) lc(white) fc(blue%57)) ///
      (rbar start end order if val==1, horiz barw(0.9) lc(white) fc(blue%100)), ///
      scheme(s1color) xlab(`xlab', noticks labsize(tiny)) leg(off) ytitle("") xtitle("") ///
      ylab(1/50, val noticks labsize(tiny) angle(horiz)) plotregion(margin(0))
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	126.0 KB
ID:	1613685

      Last edited by Andrew Musau; 08 Jun 2021, 03:25.

      Comment


      • #4
        Andrew Musau @Maarten Buis
        Thank you for both of your advice.
        Best regards.

        Raymond Zhang
        Stata 17.0,MP

        Comment

        Working...
        X