Announcement

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

  • Stacked Histogram or bar graph with overlying line graph

    I am trying to create a stacked histogram or bar graph with an overlying line graph and am having trouble.

    My dataset is patients who have undergone heart transplant using either "expanded" or "not_expanded" donor hearts where the varname is "expanded" with 0= no and 1= yes. I also have the varname "not_expanded" where 0=no and 1=yes, such that "expanded" and "not_expanded" are inversions of each other. The varname "yot" is the year the transplant occurred.

    To get a stacked bar graph, I use the following code:

    graph bar (sum) expanded not_expanded, over (yot), stack

    and it looks great.

    To create a histogram of all transplants (not stacking expanded and not_expanded) with a line graph of the median sequence number for that year (varname med_seq), I use the following code:

    egen med_seq = median(seq_num), where "seq_num" is the sequence number for each patient

    histogram yot, discrete frequency addplot((line med_seq yot, sort))

    and it also looks great.

    The question is, how do I get the line graph over the stacked bar graph or histogram so I can represent bot the median sequence numbers (as a line) as well as the frequency of both expanded and not expanded transplants?

    Thanks!

  • #2
    Dear Stanford Seth,

    It might be helpful if you could share part of your data using the dataex command in Stata (-help dataex-). I think that it would make resolving this problem easier.

    Comment


    • #3
      Salvatore Viola gives excellent advice. I don't fully understand the set-up either without a data example.

      It is easy enough to stack histograms in this case. You plot the total as backdrop and then one subset on top. The visible part of the underlying histogram is the complementary subset. But be sure you show frequencies. Frequencies are additive.

      Code:
      sysuse auto, clear 
      set scheme stcolor 
      
      local opts freq start(10) width(1) discrete
      
      twoway histogram mpg, `opts' lcolor(red) fcolor(red*0.2) || histogram mpg if foreign, `opts' lcolor(blue) fcolor(blue*0.2) legend(order(1 "Domestic" 2 "Foreign") pos(1) ring(0) col(1))
      Click image for larger version

Name:	histogram_subset.png
Views:	1
Size:	38.9 KB
ID:	1724027


      Now you can add a line using an extra twoway call.

      Code:
      || line whatevery whateverx

      Comment


      • #4
        Actually, with the code you provided, it worked! Thanks so much for your expertise!

        Comment

        Working...
        X