Announcement

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

  • Creating a bar graph from a binary variable

    Hi all, I'm new to stata.

    I have a variable (intnl) and it has 442 values of either 0 or 1 to represented the internationalised nature of a civil war (0 = No, 1 = Yes).

    I have even created a new variable (x1) with the individual frequencies


    . tab intnl, matcell(x)
    Code:
          Intnl |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |        269       60.86       60.86
              1 |        173       39.14      100.00
    ------------+-----------------------------------
          Total |        442      100.00
    
    . matrix list x
    
    x[2,1]
         c1
    r1  269
    r2  173
    
    . svmat x
    
    . list x* in 1/10
    
         +-----+
         |  x1 |
         |-----|
      1. | 269 |
      2. | 173 |
      3. |   . |
      4. |   . |
      5. |   . |
         |-----|
      6. |   . |
      7. |   . |
      8. |   . |
      9. |   . |
     10. |   . |
         +-----+
    My question is, how do I generate a bar graph with 2 bars, one displaying the frequency of "Yes2 and the other the frequency of "No". So far I have only been able to generate a graph with 1 bar which shows the sum.

    Any help is much appreciated, thank you.

  • #2
    How about this (using your original variable)?

    Code:
    histogram intnl, frequency
    From the PDF manual:

    If you want gaps between the bars, do not specify histogram’s width() option—which would change how the histogram is calculated—but specify the bar option barwidth() or the histogram option gap, both of which affect only how the bar is rendered.
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 18.5 (Windows)

    Comment


    • #3
      Originally posted by Bruce Weaver View Post
      How about this (using your original variable)?

      Code:
      histogram intnl, frequency
      From the PDF manual:
      Thanks for the reply Bruce, this is what the command produced: https://gyazo.com/e6ffe7e1819071a3bdf0953e26d5115a
      1) How do I only view values 0 and 1 (not the points in-between).
      2) How do I make values on the Y-axis more specific (i.e. not in increments of 100).
      Last edited by Finn Sloan; 07 Dec 2017, 07:17.

      Comment


      • #4
        How's this for starters?

        Code:
        histogram intnl, frequency discrete barwidth(.8) xlabel(0 1)
        I got there by typing

        Code:
        help histogram
        Then I clicked on discrete_opts > bar_options.
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment


        • #5
          In addition to Bruce's helpful suggestions, here is the start of another road.

          Code:
          clear 
          input intnl freq 
          0 269 
          1 173
          end 
          label var intnl "Intnl" 
          expand freq 
          
          graph bar (count), over(intnl)
          It's the only the last line that needs to be repeated by Finn. Naturally, you need a better variable label!

          Comment


          • #6
            Thank you for the fantastic advice!

            Comment

            Working...
            X