Announcement

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

  • Bar graph containing three information with two axis

    Hello,

    I am trying to create a horizontal bar graph (actually, I am not sure if I should call it a bar graph). The y-axis is going to be country and the x-axis should be year. The content of the graph is going to be based on a 'binary' variable. If 'binary' ==1, I want it to appear on the graph and if 'binary' ==0, it is not going to show on the graph.

    To be more elaborate, let's say the sample period spans from 2000 to 2010. The binary for the US is 1 from 2000 to 2006 but 0 from 2007 to 2010. On the graph, for the US (found on y-axis), it will have a straight line/bar from period 2000 - 2006 (found on x-axis) but no line/bar from 2007 - 2010.

    Could someone help me with coding this graph?

    Thank you!

    Best,
    Soyoung

  • #2
    Here's a possible solution. The -tsspell- command by Nick Cox is available from SSC (ssc install tsspell). The tsset command, tsspell command and references to the variable _spell are actually only necessary if you have "gaps" in your binary variable as in the first set of observations (2000-2001=1, 2002=0, 2003-2004=1).

    The graph command is set up to add a little space on either side of the graph (range goes from 0.5 to 3.5), you'll need to adjust the outer value to account for the number of countries you have. Likewise in the xlab option.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str2 country_name float(year binary)
    "ca" 2000 1
    "ca" 2001 1
    "ca" 2002 0
    "ca" 2003 1
    "ca" 2004 1
    "uk" 2000 1
    "uk" 2001 1
    "uk" 2002 0
    "uk" 2003 0
    "uk" 2004 0
    "us" 2000 0
    "us" 2001 0
    "us" 2002 1
    "us" 2003 1
    "us" 2004 1
    end
    
    encode country_name, gen(country_id)
    tsset country_id year
    tsspell binary, cond(binary==1)
    bysort country_id _spell (year): egen max=max(year) if binary==1
    bysort country_id _spell (year): egen min=min(year) if binary==1
    twoway pcspike max country_id min country_id, lwidth(vvvthick) xscale(range(0.5 3.5)) ///
        xlab(1(1)3, valuelabel) ylab(, angle(0)) ytitle("years") xtitle("countries")
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Not very clear to me what you want but this may help:


      Code:
      webuse grunfeld, clear
      egen median = median(mvalue) , by(company)
      histogram year if median > mvalue , ///
      by(company, col(1) note("")) discrete width(1) bfcolor(ltblue) blcolor(blue) ///
      subtitle(, fcolor(none) pos(9) nobox nobexpand) ytitle("") xtitle("") ///
      xla(1935/1954, format(%tyY)) yla(none)

      Click image for larger version

Name:	histobar.png
Views:	1
Size:	19.7 KB
ID:	1456431

      Comment


      • #4
        I just realized that I had the axes switched in #2. Here's the correct arrangement:

        Code:
        encode country_name, gen(country_id)
        tsset country_id year
        tsspell binary, cond(binary==1)
        bysort country_id _spell (year): egen max=max(year) if binary==1
        bysort country_id _spell (year): egen min=min(year) if binary==1
        twoway pcspike country_id max country_id min, lwidth(vvvthick) yscale(range(0.5 3.5)) ///
            ylab(1(1)3, valuelabel) ylab(, angle(0)) xtitle("years") ytitle("countries")
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          I did something similar recently, but I had gaps in my data, so I used dots instead of bars after recasting an -xtline-. Here's my code adapted to the pig data:

          Code:
          /* Fake data */
          set seed 09191944
          webuse pig, clear
          keep id week
          keep if inrange(id,1,10)
          lab define id 1 "A" 2 "B" 3 "C" 4 "D" 5 "E" 6 "F" 7 "G" 8 "H" 9 "I" 10 "J"
          lab val id id
          gen in_or_out = uniform()>=0.2
          
          
          /* Set up data */
          xtset id week
          egen group = group(id), label
          gen      y = in_or_out * group 
          egen t_max = max(week)
          
          /* This changes the rendition of the lines connecting the points for each pig over time */
          distinct id
          
          forvalues i=1(1)`=r(ndistinct)' {
              local connect_options "`connect_options' plot`i'opts(lpattern(dot) lcolor(gray))"
          }
          
          /* This graphs using points and label the pigs on the right y-axis */
          xtline y if y>=1, overlay recast(connected) `connect_options' /// 
          xlab(#10, grid) xtitle("Time") ylabel(none) ytitle("") legend(off) ///
          addplot(scatter group t_max, ms(none) ylabel(#`=r(ndistinct)', value angle(horizontal) axis(2)) ///
          ytitle("Pig", axis(2) orientation(horizontal)) yaxis(2)) ///
          plotregion(fcolor(white)) graphregion(fcolor(white))
          
          drop y t_max group
          This produced:

          Click image for larger version

Name:	Screen Shot 2018-08-02 at 6.45.05 PM.png
Views:	1
Size:	60.2 KB
ID:	1456444

          Comment

          Working...
          X