Announcement

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

  • Question about catplot different colors for different bars

    Hello All,

    I thank you very much for taking time to read my question.
    I am trying to get different colors for different groups in catplot
    My example data set is as follows

    Code:
    clear
    input str106 CCSPRINCIPALDIAGNOSIS int NO float(BEF_AFTER n)
    "Abdominal pain" 275 0 1 
    "Abdominal pain" 262 1 1 
    "Complication of device; implant or graft" 24 1 1 
    "Complications of surgical procedures or medical care" 21 1 1 
    "Diabetes mellitus with complications" 323 0 1 
    "Diabetes mellitus with complications" 305 1 1 
    "Diabetes mellitus without complication" 44 0 1 
    "Diabetes mellitus without complication" 30 1 1 
    "Epilepsy; convulsions" 26 1 1 
    "Fluid and electrolyte disorders" 57 0 1 
    "Fluid and electrolyte disorders" 41 1 1 
    "Gastritis and duodenitis" 22 0 1 
    end
    
    expand NO 
    
    catplot BEF_AFTER CCSPRINCIPALDIAGNOSIS  , ylabel(, nogrid) var1opts(sort(1) descending) bar(1, color(black))
    QUESTION : Is it possible to change color of all bars to black when BEF_AFT==0 and to red when BEF_AFTE==1


    Thank you again for your time

    Sincerely ,
    Anwar Dudekula

  • #2
    catplot is from SSC, as you are asked to explain. Here it is just a wrapper for graph hbar and so supports asyvars and so forth.

    Starting from your helpful example, I give a variant on your graph and two others. Sorting on alphabetical order doesn't seem essential or even helpful. I don't expand as frequency weights do the same job.

    Note that as from 9 October 2014, official Stata (in version 13 updated or version 14) has enhanced the underlying commands, so that in many cases it has caught up with catplot (first published 2003). That's the point of the third example.

    -------- update 09oct2014 --------------------------------------------------------

    2. graph bar and graph dot now handle categorical data better. Graphs of
    frequencies and percentages of observations within over() groups can be
    obtained using statistics (count) and (percent). For example,

    graph bar (percent), over(grpvar)

    creates bars measuring the percentage of observations in each level of
    grpvar.

    graph bar, over(grpvar)

    is an abbreviated syntax that does the same.
    Code:
    catplot BEF_AFTER CCSPRINCIPALDIAGNOSIS [fw=NO], ///
    ylabel(, nogrid) var2opts(sort(1) descending)    ///
    asyvars bar(1, color(black)) bar(2, color(red)) name(g1, replace)
    
    graph dot (mean) BEF_AFTER [fw=NO], ///
    over(CCSPRINCIPALDIAGNOSIS, sort(1) descending) ///
    linetype(line) lines(lcolor(gs12) lw(vvthin)) ///
    ysc(r(-0.1 1.25) titlegap(*5)) yla(none) blabel(total, format(%04.2f)) name(g2, replace) ytitle(fraction whatever)  
    
    graph hbar (count) [fw=NO], over(BEF_AFTER) over(CCS, sort(1) descending) ///
    ylabel(, nogrid) asyvars bar(1, color(black)) bar(2, color(red)) name(g3, replace)

    Comment


    • #3
      Got it. Thank you very much for your time and help.
      Sincerely,
      Anwar

      Comment


      • #4
        Originally posted by Nick Cox View Post
        catplot is from SSC, as you are asked to explain. Here it is just a wrapper for graph hbar and so supports asyvars and so forth.

        Starting from your helpful example, I give a variant on your graph and two others. Sorting on alphabetical order doesn't seem essential or even helpful. I don't expand as frequency weights do the same job.

        Note that as from 9 October 2014, official Stata (in version 13 updated or version 14) has enhanced the underlying commands, so that in many cases it has caught up with catplot (first published 2003). That's the point of the third example.



        Code:
        catplot BEF_AFTER CCSPRINCIPALDIAGNOSIS [fw=NO], ///
        ylabel(, nogrid) var2opts(sort(1) descending) ///
        asyvars bar(1, color(black)) bar(2, color(red)) name(g1, replace)
        
        graph dot (mean) BEF_AFTER [fw=NO], ///
        over(CCSPRINCIPALDIAGNOSIS, sort(1) descending) ///
        linetype(line) lines(lcolor(gs12) lw(vvthin)) ///
        ysc(r(-0.1 1.25) titlegap(*5)) yla(none) blabel(total, format(%04.2f)) name(g2, replace) ytitle(fraction whatever)
        
        graph hbar (count) [fw=NO], over(BEF_AFTER) over(CCS, sort(1) descending) ///
        ylabel(, nogrid) asyvars bar(1, color(black)) bar(2, color(red)) name(g3, replace)
        Hi Nick, is there any way to change the outline color as well, as is done for regular barplots, using the lc() option? I tried it but it shows - option lc() not allowed.

        Comment


        • #5
          #4 I can't see the whole of your syntax, but consider putting lc() in the usual place:

          Code:
          sysuse auto, clear
          
          catplot foreign rep78, asyvars bar(1, fc(red*0.1) lc(red)) bar(2, fc(blue*0.1) lc(blue))

          Comment

          Working...
          X