Announcement

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

  • Combining several variables as bars on a bar chart

    Hi

    The following code creates variables similar to those that I have:
    Code:
    clear
    set seed 1066
    set obs 100
    gen q1=cond(uniform()<0.5,0,1)
    gen q2=cond(uniform()<0.6,0,1)
    gen q3=cond(uniform()<0.4,0,1)
    gen q4=cond(uniform()<0.3,0,1)
    gen q5=cond(uniform()<0.5,0,1)
    gen q6=cond(uniform()<0.7,0,1)
    label define yesno01 0 "No" 1 "Yes"
    label values q1-q6 yesno01
    forvalues x = 1/6 {
      label variable q`x' "Question `x'"
            }
    forvalues x = 1/6 {
      tab q`x'
            }
    The numbers in the "Yes" categories are 41, 60, 35, 33, 48 and 70.

    How can I plot a single bar chart with six bars, of heights 41, 60, 35, 33, 48 and 70, with the bars labelled on the x-axis with the labels of the variables (here "Question 1", "Question 2", etc)?

    It would be the icing on the cake if I could then add the number (41, 60, etc) to the top of the bar.

    Thankyou

    Karin

  • #2
    With q1-q6 coded 0/1, the sum of each is the number of 1s, and the command is:

    Code:
    collapse (sum) q1-q6
    graph bar (asis) q1-q6 , blabel(bar)

    Comment


    • #3
      I get your results for the numbers of No values.

      With your data, another way to approach it with catplot (SSC) and tabm from tab_chi (SSC):

      Code:
      tabm q1-q6, replace
      catplot _stack [fw=_values]

      Comment


      • #4
        Thanks Svend and Nick for your very helpful replies. I will explore both approaches.

        Comment

        Working...
        X