Announcement

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

  • catplot with "empty" categories

    I'd like to plot the response percentages to a question using catplot or tabplot. However some of the response categories are empty (i.e., have a value of 0), yet I cannot figure out how to include these response categories in the plot.

    Here is a minimal working example:

    Code:
    use "https://www.dropbox.com/s/obmd1nqc4vvdd0l/example.dta?dl=1", clear
    
    label define placementl 10 "Bottom 10%" 9 "2nd decile" 8 "3rd decile" 7 "4th decile" 6 "6th decile" 5 "5th decile" 4 "4th decile" 3 "3rd decile" 2 "2nd decile" 1 "Top 10%"
    
    label val placement placementl
    
    catplot placement, ///
        title("Decision Making Ability, Relative to Others", color(black)) ///
        percent ///
        blabel(bar, position(outside) size(medsmall) format(%9.1f))  ///
        bar(1, bcolor(orange) blcolor(none)) ///
        graphregion(color(white)) ///
        ylabel(0(5)30, nogrid) ///
        ytitle("")
    Click image for larger version

Name:	image_31157.jpg
Views:	1
Size:	70.8 KB
ID:	1714336


    The graph is missing the 7th, 8th, 9th, and 10th decile because there is no data to plot for these categories, but I would still like to have them as part of the graph.

    Alternatively, I can get close with tabplot but haven't been able to figure out how to get the value labels to display outside of each bar (as in the graph above using catplot). Here is some code and the graph it produces:

    Code:
    use "https://www.dropbox.com/s/obmd1nqc4vvdd0l/example.dta?dl=1", clear
    
    gen placement2 = 11 - placement
    
    tabplot placement2, ///
         horizontal ///
         yasis ///
         percent ///
         bcolor(orange) blcolor(none) ///
         graphregion(color(white)) ///
         ytitle("") ///
         title("Decision making ability", color(black)) ///
         note("") ///
         subtitle("") ///
         xsize(4) ysize(6) ///
         ylabel(1 "Bottom 10%" 2 "2nd decile" 3 "3rd decile" 4 "4th decile" 5 "6th decile" 6 "5th decile" 7 "4th decile" 8 "3rd decile" 9 "2nd decile" 10 "Top 10%")
    Click image for larger version

Name:	image_31158.jpg
Views:	1
Size:	132.0 KB
ID:	1714337


    The graph gets me there, other than I'd like to add values just outside of each bar (my understanding is that with tabplot I can only post them "underneath" each bar, rather than "on top" of each bar).

    Would be grateful for any suggestions or tips, using either catplot or tabplot (or even hbar).
    Last edited by David Tannenbaum; 20 May 2023, 15:37.

  • #2
    Dear David,

    Your example data link is no longer valid, so, I use the example provided by Nick Cox here but with the revised catplot syntax:
    Code:
    ssc install catplot, replace
    sysuse auto, clear
    catplot, over(rep78) over(foreign) percent(foreign) bar(1, bfcolor(none)) blabel(bar, pos(top) format(%3.2f))
    which results in the values or percentages positioned above the top of the bars, including the empty categories but without any value added:
    Click image for larger version

Name:	catplot_empty_categories_top.png
Views:	1
Size:	19.5 KB
ID:	1766476

    or, using:
    Code:
    catplot, over(rep78) over(foreign) percent(foreign) bar(1, bfcolor(none)) blabel(bar, pos(base) format(%3.2f))
    which results in the values or percentages positioned at the base of the bars:
    Click image for larger version

Name:	catplot_empty_categories_base.png
Views:	1
Size:	19.6 KB
ID:	1766477


    http://publicationslist.org/eric.melse

    Comment


    • #3
      I missed this question somehow. Thanks very much to ericmelse for his reply.

      The question of plotting whatever is absent receives some attention in a Tip likely to appear in Stata Journal 25(1).

      Comment


      • #4
        This looks great, thanks ericmelse. And looking forward to seeing the upcoming Stata Tip, Nick.

        Comment


        • #5
          Dear Nick,

          Yes, a Stata Tip about 'plotting whatever is absent' would be interesting indeed.

          One though about the current functionality of catplot is to have also an option that would toggle 'not to display 0 or 0%' for those categories that have no values (like #2), or 'to display 0 or 0%' for those categories that have no values.
          My logic for the latter option is that there is 'information' to be displayed about the data, i.e. the fact that Foreign auto's (brands) have no cases as such with a record for 1 or 2 repairs, hence the 0 or 0% located at the base of the 'absent' bar.
          I suppose it is a matter of taste, or opinion, whether or not to display 0 or 0% at the base of these two categories without any cases.
          But, as always, I very much favour the option to decide what to do.
          http://publicationslist.org/eric.melse

          Comment


          • #6
            ericmelse One of the key points of the Tip in press is that graph bar, graph hbar and graph dot are limited in their scope for showing categories that don't exist. And catplot is just a wrapper for one of those commands. Full flexibility as you desire comes from recssting any problem as one for twoway.

            Comment

            Working...
            X