Announcement

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

  • Creating a stacked histogram

    Hello! I am trying to create two histograms (epidemic curve) based on the frequency of cases per day of the Onset_of_first_symptom variable, one stratified by Compound_name (medium or maximum) and the other stratified by Outcome (alive or DIED). In excel, this would be a stacked bar chart. I'm not quite sure how to do it in Stata since most examples use twoway bar charts, not histograms.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte Respondent_Type str7 Compound_name double Onset_of_first_symptom str5 Outcome
    1 "medium" 22586 "alive"
    1 "maximum" 22586 "alive"
    1 "medium" 22590 "DIED"
    1 "medium" 22586 "alive"
    1 "maximum" 22590 "alive"
    1 "medium" 22586 "alive"
    1 "maximum" 22588 "DIED"
    1 "medium" 22586 "alive"
    1 "medium" 22586 "alive"
    1 "medium" 22586 "alive"
    end
    format %td Onset_of_first_symptom
    Thank you!

  • #2
    For once there is a resemblance with MS Excel. You could do this as a stacked bar chart in Stata, but I don't recommend trying that as I imagine your full dataset includes many more dates, so many that graph bar's inclination to label all categories would be a nuisance. That last point is discussed in detail in https://journals.sagepub.com/doi/pdf...6867X211000032

    What I do recommend is

    1. Laying down a frequency histogram for all cases

    2. Laying down a frequency histogram for one category on top.

    Then the visible difference represents the other category and you make sure that the legend matches.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte Respondent_Type str7 Compound_name double Onset_of_first_symptom str5 Outcome
    1 "medium" 22586 "alive"
    1 "maximum" 22586 "alive"
    1 "medium" 22590 "DIED"
    1 "medium" 22586 "alive"
    1 "maximum" 22590 "alive"
    1 "medium" 22586 "alive"
    1 "maximum" 22588 "DIED"
    1 "medium" 22586 "alive"
    1 "medium" 22586 "alive"
    1 "medium" 22586 "alive"
    end
    format %td Onset_of_first_symptom
    
    set scheme s1color 
    
    twoway histogram Onset, freq discrete barw(1) lcolor(black) fcolor(none) || histogram Onset if Outcome == "alive", freq discrete barw(1) legend(order(1 "Died" 2 "Alive")) color(blue)

    But the same graph could be created with twoway bar: you just need to calculate frequencies first.

    Comment


    • #3
      Thank you, Nick! Excellent solution as always. Quick follow-up, how do I bring up the twoway histogram dialog box if I wanted to make tweaks to the graph? In the dropdown menu, there's a twoway option and a histogram option, but no twoway histogram option.

      Comment


      • #4
        Sorry, no ideas from me on how to use dialogs for this.

        Comment

        Working...
        X