Announcement

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

  • new package twby available on SSC: create a cross-tabulation of graphs

    Thanks to Kit Baum, a new package, twby, is now available on SSC. It is a prefix command for twoway graph, that creates a cross-tabulation of graphs. By aligning the graphs appropriately, one does not need to label each individual graph, so instead twby labels the columns at the top and the rows on the side. It automates the process described in (Buis & Weiss 2009) To install it, type in Stata ssc install twby

    In its simplest form you just specify the two categorical variables that represent the rows and the columns of the table of graphs, followed by a colon, followed by the twoway graph that should appear in each cell. For example:

    Code:
    webuse auto2, clear
    set scheme s1color
    
    twby foreign rep78 : scatter price weight
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	75.1 KB
ID:	1517492



    Most of the sub-options you would normally use in the by option can be specified in the twby prefix. So if we want to make this graph prettier, we could do something like so:

    Code:
    // use more sensible units
    replace weight = 0.00045359237*weight
    label variable weight "Weight (tonnes)"
    replace price = price / 1000
    label variable price "Price (1000s {c S|})"
    
    //create the graph
    twby foreign rep78, compact :    ///
        scatter price weight,        ///
        ylab(,angle(0)) xlab(1(.5)2)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	73.7 KB
ID:	1517493



    We could use twby to visualize a three way cross-tabulation in a way similar to (Cox 2016).

    Code:
    // open example data    
    sysuse nlsw88, clear
    
    // create the necessary categorical variables
    gen byte urban = c_city + smsa if !missing(c_city,smsa)
    label define urban 2 "central city" ///
                       1 "suburban"     ///
                       0 "rural"
    label value urban urban
    label variable urban "urbanicity"
    
    gen byte marst = !never_married + married if !missing(never_married,married)
    label define marst 0 "never married" ///
                       1 "widowed/divorced" ///
                       2 "married"
    label value marst marst
    label var marst "marital status"
                      
    gen byte edcat = cond(grade <  12, 1,     ///
                     cond(grade == 12, 2,     ///
                     cond(grade <  16, 3,4))) ///
                     if !missing(grade)
    label variable edcat "education"
    label define edcat 1 "< highschool"    ///
                       2 "highschool"      ///
                       3 "some college"    ///
                       4 "college"            
    label value edcat edcat                  
    
    // the three way table we want to visualize
    bys edcat: tab urban marst, row nofreq
    
    // recreate that table as variables
    contract edcat marst urban, zero nomiss
    egen tot = total(_freq), by(urban edcat)
    gen perc = _freq / tot *100
    
    // variables that helps display the numbers in the graph
    gen lab = strofreal(perc, "%5.0f")
    gen y = -5
    
    // the graph
    twby urban marst ,                                             ///
            compact left xoffset(0.5) legend(off)                  ///
            title("Percentage in each marital status"              ///
                  "given education and urbanicity") :              ///
        twoway bar perc edcat ,                                    ///
            xlab(1/4, val alt) yscale(range(0 75))                 ///
            ylab(none) ytitle("") barw(.5)                      || ///
        scatter y edcat ,                                          ///
            msymbol(none) mlab(lab) mlabpos(0) mlabcolor(black)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	90.1 KB
ID:	1517494



    Buis, Maarten L. and Weiss, Martin (2009) "Stata Tip 81: A Table of Graphs", The Stata Journal, 9(4):643-647. https://doi.org/10.1177/1536867X0900900410

    Cox, Nicholas J. (2016) "Speaking Stata: Multiple bar charts in tableform", The Stata Journal, 16(2): 491-510. https://doi.org/10.1177/1536867X1601600214
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

  • #2
    Hi Maarten,

    I just came across this post and find your command very useful, so thanks. One question though: how would you change the display of the row and column titles? Specifically, I would like to unbox the row titles (unboxing the column titles can be done with the subtitle(, nobox) option of the twoway command) and modify the offset of the column titles, like the xoffset() option does with the row titles. As far as the box situation goes, I tried to play a graph recording where I manually unboxed the titles but to my surprise that did not work. Any hints?

    Comment


    • #3
      Unboxing right now is not that easy, but the graph editor should work. Maybe your graphs have different number of rows, which might cause trouble for the graph recorder.
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks, Maarten. The graph editor does work; only the graph recording route does not (which I was hoping to use because of multiple graphs, or rather tables of graphs, needing editing). The table of graphs I am trying to edit has 12 graphs in total split in three rows and four columns.

        Comment


        • #5
          Maarten, I found out why the graph recording does not pass through as intended: twby takes it as an option of the twoway graph itself before it edits the style of the row headings. Below is a workaround if you're interested in incorporating it in a future update:

          After adding an optional play(passthru) option to twby's syntax, I substituted
          Code:
          if `"`play'"' == "" graph display `gr'
          else {
             tempfile _`gr'
             quietly graph save `gr' `_`gr''
             graph use `_`gr'', `play'
          }
          For
          Code:
          graph display `gr'
          Last edited by Maxime Bercholz; 04 Nov 2022, 15:06.

          Comment

          Working...
          X