Announcement

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

  • Binary Variable Graph

    Hi All;

    I need help about binaries. I reviewed the topics but i couldn't find solution.
    I have five binary variable and i want to draw bar plot that contains only 1 value of binaries. And whole variables must be side by side in the same bar graph. Bars must be show, e.g How many 1 values does var1 contains.

    I'm so sorry if there is any topic same with this in forum, I have time shortage. Thank you all!

    Click image for larger version

Name:	WhatsApp Image 2020-01-14 at 22.11.22.jpeg
Views:	1
Size:	55.2 KB
ID:	1532030

  • #2
    If your variables are coded 0 and 1, then perhaps a simple bar graph displaying the sums of said variables would suffice. Try:

    Code:
    clear
    sysuse nlsw88.dta
    graph bar (sum) never_married (sum) south, bargap(5)
    To tweak the graph to make it look as you want, try:

    Code:
    help graph bar
    or explore possibilities going to the GUI for graph bar: Graphics -> Bar

    Comment


    • #3
      Code:
      clear*
      
      //  CREATE DEMONSTRATION DATA SET
      
      local p1 0.5
      local p2 0.8
      local p3 0.1
      local p4 0.3
      local p5 0.6
      
      set seed 1234
      set obs 1000
      forvalues i = 1/5 {
          gen var`i' = (runiform() < `p`i'')
      }
      
      //  CREATE DESIRED GRAPH
      collapse (sum) var*
      gen long obs_no = _n
      reshape long var, i(obs_no) j(var_num)
      label define var_num    1   "var1"  ///
                              2   "var2"  ///
                              3   "var3"  ///
                              4   "var4"  ///
                              5   "var5"
      label values var_num var_num
      label var var "Number Obs with Variable = 1"
      graph twoway bar var var_num, xlabel(, valuelabel)
      You can add other options to the -graph twoway bar- command to customize the appearance of the bars and other aspects of the graph to your liking.

      Added: Crossed with #2 which offers a simpler approach.

      Comment


      • #4
        Honorable friends Igor and Clyde thanks for your urgent answers. Both answers were quite helpful. I used Igor's suggestion and i acquired that i want.

        Result:
        Click image for larger version

Name:	Result.png
Views:	1
Size:	37.0 KB
ID:	1532043


        And Clyde you are my idol in terms of stata knowledge.

        Thank you again.

        Sincerely yours

        Mustafa

        Comment


        • #5
          statplot (SSC) may also be useful. Here's indicative code:


          Code:
          clear
          sysuse nlsw88.dta
          
          * ssc install statplot 
          statplot never_married south, stat(sum) asyvars recast(bar) bargap(5)

          Comment


          • #6
            Originally posted by Nick Cox View Post
            statplot (SSC) may also be useful. Here's indicative code:


            Code:
            clear
            sysuse nlsw88.dta
            
            * ssc install statplot
            statplot never_married south, stat(sum) asyvars recast(bar) bargap(5)
            Thank you Nick Cox it is looking useful.

            Comment

            Working...
            X