Announcement

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

  • Percentage bars

    I want a bar chart to show the percentage of observations with each of nine different values (0 to 8) in four groups of patients.
    This code shows the number of patients (i e, frequency) with each of these values in each of the four groups (see attached fig):

    Code:
     graph bar (count), over(value) over(Pt_group) bargap(10)
    This code shows the percentage (see attached fig):

    Code:
     graph bar, over(value) over(Pt_group)
    But it is obvious that the percentage is calculated with the entire patient cohort (i e, all four groups) as the denominator.

    How do I get the percentage within each patient group?

    I have tried the graphics menu and selected the option "Graph of percent of frequencies within categories", but that is apparently not what I get?

    Thanks for any advice
    Attached Files

  • #2
    You can consider for more flexibility the user community contributed package catplot that provides this with many more relevant options:
    Code:
    ssc install catplot, replace
    which catplot
    h catplot
    
    http://publicationslist.org/eric.melse

    Comment


    • #3
      This may help. Most of the code here consists of setting up a data example, but you have the data

      Code:
      clear 
      set obs 100 
      gen Pt_group = ceil(_n/25)
      set seed 31459 
      gen value = runiformint(0, 8)
      capture set scheme stcolor 
      tab value Pt_group, column 
      
      graph bar (percent), over(value) by(Pt_group) blabel(bar)

      Comment


      • #4
        Thank you very much, Nick, but that resulted in four separate graphs (see enclosure). I would very much like a graph like the examples shown (one panel). I can also add a percent option after the over options in the original code and get the correct percentages, but then the axis labels (0-8) disappear. I would very much like to keep the axis labels.
        Attached Files

        Comment


        • #5
          Noting now #2, here is extra code producing the same graph:

          Code:
          clear 
          set obs 100 
          gen Pt_group = ceil(_n/25)
          set seed 31459 
          gen value = runiformint(0, 8)
          capture set scheme stcolor 
          graph bar (percent), over(value) by(Pt_group) blabel(bar) name(G1, replace)
          
          tab value Pt_group, column 
          
          catplot, recast(bar) over(value) by(Pt_group) percent(Pt_group) blabel(bar) name(G2, replace)
          For real data if you wanted the bar labels at all, extra formatting would be a good idea.

          Comment


          • #6
            #4 Well, as Eric Melse commented, you could look at the help to find other possibilities. Here is one:

            Code:
             catplot, recast(bar) over(value, gap(*0.5)) over(Pt_group) percent(Pt_group) name(G3, replace)
            I have omitted the bar labels. 36 bars in a row is less likely to make that a good choice.

            Comment


            • #7
              And, depending a little on what it is graded 0 to 8, a histogram may also be competitive:

              Code:
              histogram value, by(Pt_group, row(1)) discrete horizontal percent barw(0.8) bfcolor(stc1*0.4) yla(0/8) blcolor(stc1)

              Comment


              • #8
                Thanks a lot, Nick and Eric

                This produced the desired result (se enclosure):

                Code:
                 catplot, recast(bar) over(value) over(Pt_group) percent(Pt_group) name(G2, replace)
                Have a nice evening
                Hans
                Attached Files

                Comment

                Working...
                X