Announcement

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

  • Stacked bar graph for a binary and a categorical variable

    Hi. I have a binary variable, OC, and a categorical variable Department. Department has 16 categories. I want to create one graph that has different bars for each Department, depicting the percentage of 1s in OC. I'm new to visualizations in Stata and so any help would be great.

    Code:
    input float OC str21 Department
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "PED"
    0 "RSX"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "PED"
    0 "COR"
    0 "COR"
    0 "OCU"
    0 "RSX"
    0 "COR"
    0 "COR"
    0 "OCU"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "GLA"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "GLA"
    0 "GLA"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "COR"
    0 "CL" 
    0 "CL" 
    0 "CL" 
    0 "CL" 
    0 "CL"

  • #2
    The proportion of 1s is the mean of a binary (0, 1) variable, so
    Code:
    graph hbar (mean) OC, over(Department, sort(1))
    is likely to be a step towards what you seek.

    Stacking is not a good idea here for several different reasons.
    Last edited by Nick Cox; 09 Apr 2023, 08:26.

    Comment


    • #3
      Thanks, Nick. That's just what I was looking for!

      Comment

      Working...
      X