Announcement

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

  • Command likes -fbar- that simultaneously show frequencies or percents of categorical variables

    Dear Stata users,
    The bar plot is frequently used in daily practice. Suppose that I have several categorical variables in my dataset, and I want to show frequencies or percents of these variables using bar plot. The -graph bar- will fail in this case. And the user-written command -fbar- (by Nick Cox in 2001.06.19) can do some work, i.e. it can show frequencies of one categorical variable one at a time. Is there some command can do this work and simultaneously show frequencies or percents of several categorical variables? Thank you.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(q1 q2 q3 q4 q5)
     3 3 2 2 2
     1 1 1 1 1
     2 3 2 1 1
     1 1 1 1 1
     2 2 2 2 2
     2 2 1 1 1
    .a 2 2 2 2
     1 1 1 1 1
     2 2 2 2 1
     2 2 2 2 2
     1 1 1 1 1
     2 2 1 1 1
     2 2 2 2 2
     1 1 1 1 1
     1 1 1 1 1
     4 4 2 3 2
     2 2 2 2 2
     1 1 1 1 1
     2 2 2 2 2
     1 1 1 1 1
     2 2 2 2 2
     2 2 1 1 1
     2 2 2 1 2
     1 1 1 1 1
     3 2 1 1 1
     2 2 2 2 2
     1 1 1 1 1
     2 3 1 2 2
     2 2 2 2 1
     2 2 2 2 2
    end
    label values q1 q
    label values q2 q
    label values q3 q
    label values q4 q
    label values q5 q
    label def q 1 "stongly agree", modify
    label def q 2 "slightly agree", modify
    label def q 3 "slightly disagree", modify
    label def q 4 "strongly disagree", modify
    Click image for larger version

Name:	fbar.png
Views:	1
Size:	44.7 KB
ID:	1694861

  • #2
    Dear Chen,

    Possibly the user community controbuted package floatplot, from Nick J. Cox, offers what you are looking for.
    Below my code using your example.
    For frequencies:
    Code:
    * To compare several variables, reshape long and apply floatplot, over():
    * Repeat dataex from #1 or use dta file - frequency
    rename (*) (answer=)
    gen id = _n
    reshape long answer, i(id) j(question) string
    egen prneg = mean(answer <= 2), by(question)
    myaxis question2=question, sort(mean prneg)
    
    * Create the floatplot - frequencies
    floatplot ans, over(question2) highneg(2) fcolors(blue blue*0.5 red*0.5 red) freq legend(span s(*.8) reg(lc(white))) ytit("all values (frequency)", m(l-2 r+2)) xtit("") graphreg(fc(white))
    graph export "Case_Chen_Samulsion_Freq.png", height(500) replace // Change path as required
    which results in:
    Click image for larger version

Name:	Case_Chen_Samulsion_Freq.png
Views:	1
Size:	20.5 KB
ID:	1694867

    For proportions:
    Code:
    * Create the floatplot - proportions
    floatplot ans, over(question2) highneg(2) fcolors(blue blue*0.5 red*0.5 red) prop legend(span s(*.8) reg(lc(white))) ytit("all values (proportion)", m(l-2 r+2)) xtit("") graphreg(fc(white))
    graph export "Case_Chen_Samulsion_Prop.png", height(500) replace // Change path as required
    which results in:
    Click image for larger version

Name:	Case_Chen_Samulsion_Prop.png
Views:	1
Size:	21.9 KB
ID:	1694868
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Dear ericmelse, thank you very much for your attention and suggestion. The -floatplot- seems nice, however, what I want is a plot like below:
      Click image for larger version

Name:	fbar2.png
Views:	1
Size:	66.8 KB
ID:	1694876

      Last edited by Chen Samulsion; 25 Dec 2022, 02:41.

      Comment


      • #4
        designplot from the Stata Journal can do this.


        Code:
        SJ-19-3 gr0061_3  . . . . . . . . . . . . . . . Software update for designplot
                (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
                Q3/19   SJ 19(3):748--751
                any attempt to use the missing option of graph dot,
                graph hbar, or graph bar is now ignored and advice on
                what to do instead is shown
        
        SJ-17-3 gr0061_2  . . . . . . . . . . . . . . . Software update for designplot
                (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
                Q3/17   SJ 17(3):779
                help file updated
        
        SJ-15-2 gr0061_1  . . . . . . . . . . . . . . . Software update for designplot
                (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
                Q2/15   SJ 15(2):605--606
                bug fixed for Stata 14
        
        SJ-14-4 gr0061  Design plots for graphical summary of a response given factors
                (help designplot if installed)  . . . . . . . . . . . . . .  N. J. Cox
                Q4/14   SJ 14(4):975--990
                produces a graphical summary of a numeric response variable
                given one or more factors

        Comment


        • #5
          Here's an example with the Titanic data. You don't need the first variable named to be an outcome. It can be a vacuous constant.


          Code:
          clear all
          
          infix class 1-9 adult 10-18 male 19-27 survived 28-36 using http://jse.amstat.org/datasets/titanic.dat.txt 
          label define class 0 crew 1 first 2 second 3 third
          label define adult 1 adult 0 child
          label define male 1 male 0 female
          label define survived 1 survived 0 died 
          
          foreach v in class adult male survived {
              label values `v' `v'
          }
          
          set scheme s1color 
          gen one = 1 
          label var one "Titanic data"
          designplot one survived class adult male, min(1) max(1) ysize(7) ysc(r(0 2300) alt) ///
          stat(count) recast(hbar) bar(1, bfcolor(green*0.2) blcolor(green)) blabel(bar)
          Click image for larger version

Name:	titanic.png
Views:	1
Size:	13.2 KB
ID:	1694879


          I find it hard to believe that anyone strongly prefers a design like #3 with its awkward sloping axis labels. Whenever the variables are all on similar scales, I would prefer a twoway bar chart or floating/sliding design such as Eric Melse shows clearly.

          Comment


          • #6
            Dear Nick Cox, Thank you so much. That is exactly what I want. I have installed yours -designplot- command but never explore it. Now it reveals its power to me.
            P.S.The plot in #3 is undoubtedly ugly which I just used to exemplify.

            Comment

            Working...
            X