Announcement

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

  • Plotting a horizontal bar chart with an if condition over two dummy variables equal to 1

    Hello Everyone,

    I am really struggling to create this graph so would appreciate any help. (Running Stata MP/17)

    So I have two dummy variables, healthy21 and healthy19 which equal to 1 if you are healthy in your respective year (2019 or 2021).

    I have over 100,000 observations of individuals, which each have either a 1 or 0 assigned to them for healthy19 and healthy21.

    Now, I am trying to plot a horizontal bar graph similar to the one below in nature.

    Where on the vertical axis I have the town (20 towns, each represented by a number between 1 and 20)

    and then on the horizontal axis i have the percentage share of being a healthy person. i.e. two bars per industry one for healthy19==1 and one for healthy21==1

    so effectively the graph shows how differences between the share of healthy people between 2019 and 2021 and then also between towns.

    Code:
    graph hbar (percent) if healthy19==1, over(townid)
    this code above works but only the 2019 data. I want the 2021 data to show in the same graph literally side by side each bar (like image below).

    any help is really appreciated












  • #2
    You don't show the graph(s) you got but the statistics you want are proportions, namely means of (0, 1) indicator variables.

    (percent) does something different and indeed by selecting healthy2019 == 1 you are thereby ignoring the complement and making the right calculation impossible.

    Here is a self-contained script to show some technique. As in https://www.statalist.org/forums/for...th-percentages you would probably be better off with a dot chart. A bar chart will spend most of its space showing that the proportions are not zero, whereas presumably it's the comparisons between towns, not the comparison with zero, that is of most interest.

    Showing percents instead is cosmetic, and can be achieved by fixing the axis labels or multiplying your indicators by 100.

    Code:
    clear
    set obs 100 
    gen town = ceil(_n/20)
    set seed 2803 
    gen healthy2019 = runiform() < 0.8 
    gen healthy2021 = runiform() < 0.7 
    
    graph hbar (mean) healthy????, over(town) legend(order(1 "2019" 2 "2021")) l1title(town)

    Comment

    Working...
    X