Announcement

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

  • Help request with catplot: possible to include "total" bars for catvar1?

    Hello! I've been having some trouble finding a solution to the following: I'm using "catplot" (ssc) to create a bar graph. I am able to have the % of catvar2 broken down by each value of catvar1. However, in addition to the two values of catvar2, I'd like to also show a "total". This would be similar to using the "by(catvar1, total)" option, except that the three values (catvar1==1, catvar1==2, and catvar==1 | 2) would be next to each other instead of in separate panels.

    As an example, suppose I make the following graph:
    Code:
    ssc install catplot, replace
    sysuse auto.dta, clear
    
    catplot foreign headroom, ///
    percent(foreign) ///
    recast(bar) asyvars
    What I'd like is for there to be groups of *three* bars instead of just the two, with the third bar representing the total % of the sample at each value of headroom. Is this possible to do?

    Many thanks in advance for your help!

    ~John

  • #2
    Code:
    sysuse auto.dta, clear
    catplot foreign headroom, percent(foreign) recast(bar) asyvars saving(gr1, replace)
    expand 2, g(new)
    replace foreign=3 if new
    label def origin 3 "Total", modify
    catplot foreign headroom, percent(foreign) recast(bar) asyvars saving(gr2, replace)
    gr combine gr1.gph gr2.gph
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.7 KB
ID:	1739675

    Comment


    • #3
      See also

      SJ-14-1 gr0058 . . . . . Stata tip 119: Expanding datasets for graphical ends
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q1/14 SJ 14(1):230--235 (no commands)
      tip on using the expand command for graphics

      https://www.stata-journal.com/articl...article=gr0058

      Comment


      • #4
        These are fantastic solutions. Thank you, Andrew and Nick, SO MUCH!!!

        Comment

        Working...
        X