Monday, March 28, 2011

Some -statplot- examples, Part 2 (wrapping long labels)

...continued from Part 1...
Part 1 of this post covered some advanced examples of -statplot-, focusing on the use of combinations of over() and by() options.
In Part 2, I examine some strategies to use -statplot- with really long variable and/or value labels.  Recently, I was using -statplot- to create some tables in a paper where some of the labels in the tables needed to be the (longish) question and answer choice text, I discovered how long labels can really be a pain for graphs.  This is a problem for any graph in Stata, regardless of whether your labels are in the legend or at the axis; however, my preference is that long labels (up to a limit) look better at the axis.
So, the examples below show how to use -statplot- options to create wrapped labels.  I hope to create an option to help make this a part of -statplot- at some point in the future, but for now, the code below is a good template for helping you to automate wrapping labels.  This can be extended to other plotting packages/commands.
Continuing from the last post, we're using the in-built "nlsw88" dataset.  Let's first look at plots with long variable labels first, and then we'll look at long value labels (which are a bit more complicated).
Note: Please make sure you update your -statplot- to the latest version since an earlier version of the program bites when you have double quotes in suboptions, as I do in the examples below.


1. Wrapping Long Variable Labels
Figure 9 (below) shows what happens when we have really long variable labels for grade, tenure, and wage.
*********************************begin
sysuse nlsw88, clear
**varlabels**
lab var grade "Really Long Variable Label for the Variable GRADE that will cutoff at 80 chars"
lab var tenure "Another Really Long Var Label for the Variable TENURE that will cutoff at 80 chars"
lab var wage "Long Variable Label, this time for the Variable WAGE that will cutoff at 80 chars"
d grade tenure wage
****************!beginFig9
statplot grade tenure wage,  ///
    tit("Long Variable Labels", size(small))
****************!endFig9
*********************************end






Sunday, March 20, 2011

Some -statplot- examples

-statplot- (co-authored by Nick Cox and myself) was released earlier this month.  You can get it at the SSC [1] [2].
In this posting, I show you some more advanced examples of using -statplot- using the Stata nlsw88 dataset (-sysuse nlsw88.dta-).  [Note: Click on any of the graphs below to see a larger example in a new tab/window.]
First, a basic example of -statplot- might look like:
***********************!begin
sysuse nlsw88.dta, clear 
statplot grade tenure wage, blabel(bar) subtit({it:-statplot-} example)
graph export "fig1.png", as(png) replace
***********************!end

Fig. 1
The main advantage of -statplot- is creating plots of summary stats with the labels moved from the legend (the usual placement when using -gr bar|hbar|dot-) to the axis.  So, I could create a graph of the same data above with something like:
graph hbar (mean) grade tenure wage
however, it would look like the graph on the left in Fig. 2 below, where we still have a legend and an array of colors indicating each bar.  I often need to produce these types of graphs but with the labels on the axis (instead of the legend).  To get this type of graph using -graph bar|hbar|dot-, I might run something like:
******!
collapse (mean) grade tenure wage
xpose, clear varn
graph hbar v1, over(_varname)
******!
which does produce something like the -statplot- graph on the right in Figure 2, but in a less-straightfoward way, and in a way that is difficult to extend to other configurations (multiple vars in the varlist, multiple over()or by() categories, etc).  
Figure 2 compares the syntax and output of -graph hbar- and -statplot-:
***********************!beginFig2
graph hbar (mean) grade tenure wage,  ///
    name(g1, replace) tit({it:{bf:-graph bar-}})
statplot grade tenure wage,  ///
    name(g2, replace) tit({it:{bf:-statplot-}})
*After running the commands above, compare the graphs with graph combine: 
gr combine g1 g2 
***********************!endFig2