Announcement

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

  • Bar chart / Catplot plotting percentages with frequency labels

    Dear Statalisters,
    I have a query that I am fairly certain will result in advice to go back to two-way graph to make a bespoke solution, but just in case I am missing something obvious.

    I am plotting a number of events occurring in different categories. The events have different severities and I have elected to use a stacked bar chart to demonstrate this as the total number of comparative events is important to visualise at the same time as the breakdown (acknowledging the advantages that tabplot can have in these scenarios).

    I am plotting percentages, but would like to label each of the stacked sections of bar chart with the frequencies of occurrence (not percentages), as when comparing this to a separate and much smaller population, it is worth highlighting this, however, as far as I understand it, this is not possible and using blabel will give me only the percentages if plotting percentages, and frequencies if plotting frequencies.

    Of note, I have included allcategories to ensure that columns which have no data are also included, and, as a result I have also had to specify legend order in order to make sure that the base value of severity (=0) is not displayed, as I believe that all categories also forces this.

    Any advice that could be provided would be appreciated
    Many thanks
    Rob

    Code:
    foreach var of local systemic {
        colorpalette red yellow, ipolate(4) nograph
        catplot Overall_`var' if Overall_`var'!=0 & Diary==1, over(Armcode) stack asyvars  bar(2, col("`r(p4)'")) bar(3, col("`r(p3)'")) bar(4, col("`r(p2)'")) bar(5, col("`r(p1)'")) percent graphregion(color(white)) bgcolor(white) allcategories legend(order(2 3 4)) blabel(bar)

  • #2
    You;re right. Any command based on graph bar won't let you plot one thing and have text labels for another thing -- unless you edit the graph.

    You can stack bars using twoway bar and twoway rbar and add whatever text you like. as a marker label.

    EDIT If you're tempted to use tabplot from the SJ after all, here is some technique:

    Code:
    sysuse auto, clear
    
    bysort foreign rep78 : gen freq = _N 
    
    tabplot rep78 foreign , percent(foreign) showval(freq) 
    
    bysort foreign : egen total = total(!missing(rep78))  
    
    gen toshow = strofreal(freq) + " (" + strofreal(100 * freq/total, "%3.1f") + "%)"
    
    tabplot rep78 foreign , percent(foreign) showval(toshow) subtitle(frequency (column %))
    Last edited by Nick Cox; 25 Jan 2023, 09:53.

    Comment


    • #3
      Thank you Nick, much obliged. I will have a play with the pre-calculations for the limits for rbar and see if worthwhile ultimately. Perhaps a descriptive sentence in the text will be just as informative and look less cluttered.

      Comment

      Working...
      X