Announcement

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

  • Stacked bar chart

    Dear all,

    I would like to create the stacked bar chart to show the proportion of prescriptions of antithrombotic medications in 2006-2010.



    What I would like to make looks as below.
    Figure.png


    But I've got looks:

    Graph.png



    Dataset looks as below and by using loops I got the proportion of prescriptions for each medication.

    clear
    input byte(id aspirin2006 clopidogrel2006 statin2006 fibrate2006 aspirin2007 clopidogrel2007 statin2007 fibrate2007 aspirin2008 clopidogrel2008 statin2008 fibrate2008 aspirin2009 clopidogrel2009 statin2009 fibrate2009 aspirin2010 clopidogrel2010 statin2010 fibrate2010 sex) int str12 first_date last_date
    1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 1 2007 2010
    2 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 2 2006 2010
    3 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 2007 2009
    4 0 0 1 0 0 1 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 2006 2010
    5 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 2 2007 2009
    6 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 2 2006 2010
    7 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 1 2006 2009
    8 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 2007 2010
    9 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 2 2006 2010

    end

    destring first_date last_date, replace
    reshape long aspirin clopidogrel statin fibrate, i(id) j(year)
    keep if inrange(year, first_date, last_date)

    // CALCULATE RATES BY SEX BY YEAR FOR EACH DRUG
    collapse (sum) aspirin-fibrate (count) denom = id, by(sex year)
    foreach v of varlist aspirin-fibrate{
    gen `v'_prop = `v'/denom * 100
    }

    I would really appreciate If you may find time to help me solve the problem.

    Thank you in advance.



  • #2
    Here are some issues and notes with regards to your question:

    1) If you display data in a bar graph, the idea is that differences between the heights of the bars are meaningful. Towards the end of your code, you are calculating proportions by sex and year to sum to 100% for each year, so between year comparisons are not meaningful as such. If you have to insist on a stacked bar graph, then you need to specify the -by()- option which will result in one graph containing one bar for each year.

    2) Official Stata graphing commands do not support stripes, dots, etc. You can however vary shades.

    Here is what a replication of the graph that you attach will look like using your data where the height of a bar represents the frequency of a drug.

    Code:
    clear
    input byte(id aspirin2006 clopidogrel2006 statin2006 fibrate2006 aspirin2007 clopidogrel2007 statin2007 fibrate2007 aspirin2008 clopidogrel2008 statin2008 fibrate2008 aspirin2009 clopidogrel2009 statin2009 fibrate2009 aspirin2010 clopidogrel2010 statin2010 fibrate2010 sex) int str12 first_date last_date
    1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 1 2007 2010
    2 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 2 2006 2010
    3 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 2007 2009
    4 0 0 1 0 0 1 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 2006 2010
    5 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 2 2007 2009
    6 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 2 2006 2010
    7 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 1 2006 2009
    8 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 2007 2010
    9 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 2 2006 2010
    
    end
    
    destring first_date last_date, replace
    reshape long aspirin clopidogrel statin fibrate, i(id) j(year)
    keep if inrange(year, first_date, last_date)
    
    rename ( aspirin clopidogrel statin fibrate ) (drug#), addnumber(1)
    gen n=_n
    reshape long drug, i(n)
    drop n
    label define drug  1 "Aspirin" 2 "Clopidogrel" 3 "Statin" 4 "Fibrate"
    label values _j drug
    
    graph bar (count) drug if drug==1, over(_j) over(year) asyvars stack///
    bar(1, bcolor(black*1.75)) bar(2, bcolor(gray*0.5)) bar(3, bcolor(gray*1.75))///
    bar(4, bcolor(black*0.5)) graphregion(color(white))/// 
    legend(rows(4) position(3) region(lwidth(none)))
    Click image for larger version

Name:	prescriptions.png
Views:	1
Size:	17.4 KB
ID:	1431226

    Comment


    • #3
      Now that Andrew has done all the hard work, I will just stand on his shoulders and throw in a different idea.

      Stacked bar charts are a popular design, but why? Here are some ways in which they can be poor.

      1. It's hard to track any category but that shown on the bottom, as in general all other baselines vary.

      2. It's hard to see very small quantities, especially zeros.

      3. It's hard to add quantitative information or text annotations on the graph without making the design too crowded.

      4. The design usually comes with a legend or key, obliging readers to go back and forth between legend and graph.
      (This can sometimes be avoided.)

      5. Different colourings are often arbitrary or distracting. or both.

      Here's another way to do it. Naturally people who don't like the design may want to mention its disadvantages. (It's to be hoped that the full data show interesting patterns.)

      https://www.statalist.org/forums/for...updated-on-ssc is perhaps the best way in for anyone curious about, but new to, this command.

      Code:
      clear
      input id aspirin2006 clopidogrel2006 statin2006 fibrate2006 aspirin2007 clopidogrel2007 statin2007 fibrate2007 aspirin2008 clopidogrel2008 statin2008 fibrate2008 aspirin2009 clopidogrel2009 statin2009 fibrate2009 aspirin2010 clopidogrel2010 statin2010 fibrate2010 sex first_date last_date
      1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 1 2007 2010
      2 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 2 2006 2010
      3 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 2007 2009
      4 0 0 1 0 0 1 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 2006 2010
      5 0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 1 1 1 1 2 2007 2009
      6 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 2 2006 2010
      7 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 1 2006 2009
      8 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 2007 2010
      9 0 0 0 0 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 2 2006 2010
      
      end
      
      reshape long aspirin clopidogrel statin fibrate, i(id) j(year)
      keep if inrange(year, first_date, last_date)
      
      rename ( aspirin clopidogrel statin fibrate ) (drug#), addnumber(1)
      gen n=_n
      reshape long drug, i(n)
      drop n
      label define drug  1 "Aspirin" 2 "Clopidogrel" 3 "Statin" 4 "Fibrate"
      label values _j drug
      
      * search tabplot for latest download location 
      tabplot _j year if drug==1, bfcolor(none) showval xtitle("")
      Click image for larger version

Name:	drugs.png
Views:	1
Size:	16.3 KB
ID:	1431234

      Comment


      • #4
        Dear Mr. Cox and Musai,

        Thank you so much for your help.


        Sincerely,
        Oyun

        Comment

        Working...
        X