From 673c7515309008b69a0892c426f5cc368221243a Mon Sep 17 00:00:00 2001 From: zoniusalexandr Date: Mon, 3 Feb 2014 01:57:01 -0500 Subject: [PATCH 1/2] Adds Variable Labels to Axes and Legend Previous version used only variable names to label the axes and legend entries. This commit changes the program to use variable labels if they exist, and variable names otherwise. The one exception is the label for the y-axis if there are multiple y-variables. I'll add in a for-loop sometime in the future to fix this. --- binscatter.ado | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/binscatter.ado b/binscatter.ado index 85d2947..0608f13 100644 --- a/binscatter.ado +++ b/binscatter.ado @@ -521,12 +521,17 @@ program define binscatter, eclass sortpreserve local xind=`counter_by'*2-1 local yind=`counter_by'*2 + + if ("`by'"!="") local byvarlabel : var label `byvarname' + if ("`byvarlabel'"=="") local byvarlabel `byvarname' * LOOP over y-vars local counter_depvar=0 foreach depvar of varlist `y_vars' { local ++counter_depvar local ++c + local ylabel : var label `depvar' + if ("`ylabel'"=="") local ylabel `depvar' * LOOP over rows (each row contains a coordinate pair) local row=1 @@ -563,7 +568,7 @@ program define binscatter, eclass sortpreserve * Add legend if "`by'"=="" { if (`ynum'==1) local legend_labels off - else local legend_labels `legend_labels' lab(`counter_series' `depvar') + else local legend_labels `legend_labels' lab(`counter_series' "`ylabel'") } else { if ("`bylabel'"=="") local byvalname=`byval' @@ -571,8 +576,8 @@ program define binscatter, eclass sortpreserve local byvalname `: label `bylabel' `byval'' } - if (`ynum'==1) local legend_labels `legend_labels' lab(`counter_series' `byvarname'=`byvalname') - else local legend_labels `legend_labels' lab(`counter_series' `depvar': `byvarname'=`byvalname') + if (`ynum'==1) local legend_labels `legend_labels' lab(`counter_series' "`byvarlabel'=`byvalname'") + else local legend_labels `legend_labels' lab(`counter_series' "`ylabel': `byvarlabel'=`byvalname'") } if ("`by'"!="" | `ynum'>1) local order `order' `counter_series' @@ -650,13 +655,17 @@ program define binscatter, eclass sortpreserve } * Prepare y-axis title - if (`ynum'==1) local ytitle `y_vars' + if (`ynum'==1) local ytitle : var label `y_vars' else if (`ynum'==2) local ytitle : subinstr local y_vars " " " and " else local ytitle : subinstr local y_vars " " "; ", all + + * Prepare x-axis title + local xtitle : var label `x_var' + if ("`xtitle'"="") local xtitle `x_var' * Display graph - local graphcmd twoway `scatters' `fits', graphregion(fcolor(white)) `xlines' xtitle(`x_var') ytitle(`ytitle') legend(`legend_labels' order(`order')) `options' - if ("`savedata'"!="") local savedata_graphcmd twoway `savedata_scatters' `fits', graphregion(fcolor(white)) `xlines' xtitle(`x_var') ytitle(`ytitle') legend(`legend_labels' order(`order')) `options' + local graphcmd twoway `scatters' `fits', graphregion(fcolor(white)) `xlines' xtitle("`xtitle'") ytitle("`ytitle'") legend(`legend_labels' order(`order')) `options' + if ("`savedata'"!="") local savedata_graphcmd twoway `savedata_scatters' `fits', graphregion(fcolor(white)) `xlines' xtitle("`xtitle'") ytitle("`ytitle'") legend(`legend_labels' order(`order')) `options' `graphcmd' ****** Save results ****** From 50f1048990c7fc128929cf3ef66b44f61365d156 Mon Sep 17 00:00:00 2001 From: zoniusalexandr Date: Mon, 3 Feb 2014 10:52:44 -0500 Subject: [PATCH 2/2] Finished adding variable labels to axes Added a for loop to cover the case with multiple y-variables. Now each variable is described using the variable label, or the variable name if there is no label, using the appropriate separator. --- binscatter.ado | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/binscatter.ado b/binscatter.ado index 0608f13..a1eb114 100644 --- a/binscatter.ado +++ b/binscatter.ado @@ -656,8 +656,26 @@ program define binscatter, eclass sortpreserve * Prepare y-axis title if (`ynum'==1) local ytitle : var label `y_vars' - else if (`ynum'==2) local ytitle : subinstr local y_vars " " " and " - else local ytitle : subinstr local y_vars " " "; ", all + else if (`ynum'==2) { + local firstloop "yes" + foreach y_var of varlist `y_vars' { + local y_varlabel : var label `y_var' + if ("`y_varlabel'" == "") local y_varlabel `y_var' + if ("`firstloop'" == "yes") local ytitle `y_varlabel' + else local ytitle `ytitle' and `y_varlabel' + local firstloop "no" + } + } + else { + local firstloop "yes" + foreach y_var of varlist `y_vars' { + local y_varlabel : var label `y_var' + if ("`y_varlabel'" == "") local y_varlabel `y_var' + if ("`firstloop'" == "yes") local ytitle `y_varlabel' + else local ytitle `ytitle', `y_varlabel' + local firstloop "no" + } + } * Prepare x-axis title local xtitle : var label `x_var'