Announcement

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

  • Displaying percentages in bar graph (catplot)

    Hi,

    I am using catplot from SSC to graph the distribution of my sample (panel data), categorized by credit rating category.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long gvkey int sic double year str10 rating
    1408 2085 2002 "A"  
    1408 2085 2003 "A"  
    1408 2085 2004 "A"  
    1408 2085 2005 "A"  
    1408 2085 2006 "BBB"
    1408 2085 2007 "BBB"
    1408 2085 2008 "BBB"
    1408 2085 2009 "BBB"
    1408 2085 2010 "BBB"
    1408 2085 2011 "BBB"
    1408 2085 2012 "BBB"
    1478 2834 2004 "A"  
    1478 2834 2005 "A"  
    1478 2834 2006 "A"  
    1478 2834 2007 "A"  
    1478 2834 2008 "A"  
    1794 2820 2003 "BBB"
    1794 2820 2004 "BBB"
    1794 2820 2005 "BBB"
    1794 2820 2006 "BB" 
    1794 2820 2007 "BB" 
    1794 2820 2008 "BB" 
    1794 2820 2009 "BB" 
    1794 2820 2010 "BB" 
    1794 2820 2011 "BB" 
    1794 2820 2012 "BB" 
    1794 2820 2013 "BB" 
    1794 2820 2014 "BB" 
    1794 2820 2015 "BB" 
    1794 2820 2016 "BB" 
    1913 2670 2002 "A"  
    1913 2670 2003 "A"  
    1913 2670 2004 "A"  
    1913 2670 2005 "A"  
    1913 2670 2006 "A"  
    1913 2670 2007 "A"  
    1913 2670 2008 "BBB"
    1913 2670 2009 "BBB"
    1913 2670 2010 "BBB"
    1913 2670 2011 "BBB"
    1913 2670 2012 "BBB"
    1913 2670 2013 "BBB"
    2044 3841 2002 "BBB"
    2044 3841 2003 "BBB"
    2044 3841 2004 "BBB"
    2044 3841 2005 "A"  
    2044 3841 2006 "A"  
    2044 3841 2007 "A"  
    2044 3841 2008 "A"  
    2044 3841 2009 "A"  
    2044 3841 2010 "A"  
    2044 3841 2011 "A"  
    2044 3841 2012 "A"  
    2044 3841 2013 "A"  
    2044 3841 2014 "A"  
    2044 3841 2015 "A"  
    2044 3841 2016 "A"  
    2085 2834 2002 "BBB"
    2085 2834 2003 "BBB"
    2085 2834 2004 "BBB"
    2085 2834 2005 "BBB"
    2085 2834 2006 "BBB"
    2255 3540 2002 "BBB"
    2255 3540 2003 "BBB"
    2255 3540 2004 "BBB"
    2255 3540 2005 "BBB"
    2255 3540 2006 "BBB"
    2255 3540 2007 "BBB"
    2255 3540 2008 "BBB"
    2255 3540 2009 "BBB"
    2393 3510 2002 "BB" 
    2393 3510 2003 "BB" 
    2393 3510 2004 "BB" 
    2393 3510 2005 "BBB"
    2393 3510 2006 "BBB"
    2393 3510 2007 "BB" 
    2393 3510 2008 "BB" 
    2393 3510 2009 "BB" 
    2393 3510 2010 "BB" 
    2393 3510 2011 "BB" 
    2393 3510 2012 "BB" 
    2393 3510 2013 "BB" 
    2393 3510 2014 "BB" 
    2393 3510 2015 "BB" 
    2393 3510 2016 "BB" 
    2403 2834 2002 "AA" 
    2403 2834 2003 "AA" 
    2403 2834 2004 "A"  
    2403 2834 2005 "A"  
    2403 2834 2006 "A"  
    2403 2834 2007 "A"  
    2403 2834 2008 "A"  
    2403 2834 2009 "A"  
    2403 2834 2010 "A"  
    2403 2834 2011 "A"  
    2403 2834 2012 "A"  
    2403 2834 2013 "A"  
    2403 2834 2014 "A"  
    2403 2834 2015 "A"  
    2403 2834 2016 "A"  
    end
    The command I'm using:

    gen order =.
    local i=1
    foreach K in AAA AA A BBB BB B CCC CC D {
    replace order= `i' if rating=="`K'"
    local ++i
    }
    catplot rating, percent var1opts(sort(order)) recast(bar)

    I get the following graph.

    Click image for larger version

Name:	pnggraph.png
Views:	1
Size:	58.2 KB
ID:	1613785


    I'm wondering if I can display the percentage of each rating in the graph, above every bar like this:

    Click image for larger version

Name:	Schermafbeelding 2021-06-09 om 00.15.22.png
Views:	1
Size:	52.5 KB
ID:	1613786

    Source: Brown et al. (2015)


  • #2
    You can try the following, which doesn't use Nick's catplot, but does make use of his mylabels (SSC).

    Code:
    gen order =.
    local i=1
    foreach K in AAA AA A BBB BB B CCC CC D {
        replace order= `i' if rating=="`K'"
        local ++i
    }
    
    mylabels 0(10)50, local(labels) suffix(%)
    
    preserve 
    contract rating order
    egen pc = pc(_freq)
    
    graph bar pc, over(rating, sort(order)) blabel(bar, format(%5.1fc) position(outside) size(small) color(black)) ///
          ylab(`labels', angle(h)) ytitle("") title("S&P Domestic Long Term Issuer Credit Rating", pos(6) size(med))
    
    local nb=`.Graph.plotregion1.barlabels.arrnels'
    forval i=1/`nb' {
      di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
      .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
    }
    .Graph.drawgraph
    restore

    Comment


    • #3
      Justin Niakamal is correct. To add % at the end of each bar label is a job for the Graph Editor. % of each axis label is a matter of spelling out that you want it one way or another

      I'd be content with % mentioned in an axis title myself.

      Comment


      • #4
        Originally posted by Justin Niakamal View Post
        You can try the following, which doesn't use Nick's catplot, but does make use of his mylabels (SSC).

        Code:
        gen order =.
        local i=1
        foreach K in AAA AA A BBB BB B CCC CC D {
        replace order= `i' if rating=="`K'"
        local ++i
        }
        
        mylabels 0(10)50, local(labels) suffix(%)
        
        preserve
        contract rating order
        egen pc = pc(_freq)
        
        graph bar pc, over(rating, sort(order)) blabel(bar, format(%5.1fc) position(outside) size(small) color(black)) ///
        ylab(`labels', angle(h)) ytitle("") title("S&P Domestic Long Term Issuer Credit Rating", pos(6) size(med))
        
        local nb=`.Graph.plotregion1.barlabels.arrnels'
        forval i=1/`nb' {
        di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
        .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
        }
        .Graph.drawgraph
        restore
        That works, thanks so much!!

        Comment


        • #5
          Here's an example using coefplot from SSC:

          Code:
          gen order =.
          local i=1
          foreach K in AAA AA A BBB BB B CCC CC D {
              replace order = `i' if rating=="`K'"
              lab def order `i' "`K'", add
              local ++i
          }
          lab val order order
          
          proportion order
          coefplot (., recast(bar) barwidth(0.5)) ///
                   (., ms(i) mlabel(string(@b,"%9.2f")+"%") mlabpos(12) pstyle(p1)) ///
                   , nooffset noci vertical rescale(100) legend(off) plotr(margin(b=0))

          Comment

          Working...
          X