*! version 1.1 21sep2022

Title

plottabs -- Plot conditional frequencies or shares (a visual analog of tabulate oneway)

Syntax

plottabs varname [if] [in]
[weight]
[, options]

where varname is the conditioning variable.

Basic options
over(varname) an alternative way to specify the conditioning variable

output(output_type) specify the output_type to be plotted:  frequency(default)/share/cumulative

graph(graph_type) specify the twoway graph_type:  line(default)/bar/connected/scatter/etc.

Memory/data management
frame(frame_name) specify the name of the frame that stores the plot data (frame_pt is the default)

clear clear all plot data stored in frame_name

replace(#) replace data and/or options for plot # in frame_name

plotonly display the plots already stored in the frame_name

Plot customization
command print out the twoway command used to display the chart (useful for finer customization)

global apply the same customization options to all plots in frame_name

plname(plot_name) name the current plot (used in legends when displaying multiple plots at once)

yzero a shorthand option for displaying zero on the y-axis (and scaling ylabels accordingly)

twoway_options change titles, legends, axes, aspect ratio, etc.

connect_options change look of lines or connecting method

marker_options change look of
markers (color, size, etc.)

Other options
nodraw do not display the plotted values (useful when looping/overlaying many intermediate plots)

times(real) multiply the plotted values by a constant (useful for normalizations)

varname needs to be specified (one way or the other) to produce a new plot. It does not need to be specified when displaying plots that are already stored in the memory (using the option plotonly)..

fweights, aweights, and iweights are allowed;
see weight.

Description

plottabs is a command that visualizes conditional frequencies and shares (i.e., the output of tabulate oneway commands).

By default, plottabs produces frequencies of observations conditional on the unique values of varname.
Setting the output_type to share produces relative shares, and cumulative produces cumulative shares instead.

plottabs avoids time-consuming memory operations performed by native graphing commands. By leveraging the data frame environment, it proves extremely fast in large datasets (up to 300-times faster than native commands).

plottabs can be called sequentially. The plotted data is stored in a dedicated data frame (see Memory/data management options), which allows users to create complex visualizations that combine multiple conditional plots.
plottabs can be also combined with other commands from the plot suite (plotmeans, plotbetas, and plotshares).

Customization: you can select your preferred twoway graph_type, graph scheme, and adjust it further using the twoway_options and other options specific to the given graph_type.

Examples

Basic use:

. clear all

. sysuse auto

. plottabs mpg, graph(bar)

Compare cumulative shares of two groups:

. clear all

. sysuse auto

. plottabs mpg if foreign == 0, output(cumulative) connect(stairstep) plname(Domestic)

. plottabs mpg if foreign == 1, output(cumulative) connect(stairstep) plname(Foreign)

Combine two output_types in a graph with a custom scheme:

. ssc install schemepack

. clear all

. sysuse auto

. plottabs mpg, graph(bar)

. plottabs mpg, output(cumulative) connect(stairstep) legend(off) scheme(gg_tableau) yaxis(2)

Combine multiple graphs from the plot suite:

See plotmeans / Combine multiple graphs from the plot suite

Plot data management:

* generate two plots but suppress their display:

. clear all

. sysuse auto

. plottabs mpg if foreign == 0, nodraw plname(Domestic)

. plottabs mpg if foreign == 1, nodraw plname(Foreign)

* display the two plots:

. plottabs, plotonly

* replace the second plot by another one:

. plottabs mpg if headroom>3, replace(2)

* adjust the customization options of the second plot:

. plottabs, plotonly replace(2) color(pink) plname("Headroom > 3")

* do the last two steps with one command:

. plottabs mpg if headroom>3, rep(2) col(pink) pln("Headroom > 3")

* clear the plot data from memory and create a new plot:

. plottabs mpg, clear graph(bar)

* create a separate plot (stored in a separate frame):

. plottabs turn, graph(bar) frame(frame_hr)

* name and display each of the two plots (stored in separate frames):

. plottabs, plotonly name(fig1)

. plottabs, plotonly name(fig2) frame(frame_hr)

Frames

Plotted data are stored in a dedicated frame. The default name of this frame is frame_pt, but other names can be specified using the option frame(frame_name).
To access the plotted data, switch to the respective frame:

. clear all

. sysuse auto

. plottabs mpg if foreign==0, out(cum) connect(stairstep) pln(Domestic)

. plottabs mpg if foreign==1, out(cum) connect(stairstep) pln(Foreign) scheme(economist)

. frame change frame_pt

. browse

The customization options are stored in a separate frame (frame_name_cust):

. frame change frame_pt_cust

. describe

. browse

To switch back to the default frame and plot more data:

. frame change default

Note that, once specified, the custom frame_name needs to be repeated every time you want to add data to the frame_name.
If no frame_name is specified, plottabs will add the data to the default frame (frame_pt) instead.

. plottabs mpg if rep78 == 3, graph(bar) pln(rep78=3) frame(fr_2)

. plottabs mpg if rep78 == 4, graph(bar) pln(rep78=4)

. plottabs mpg if rep78 == 5, graph(bar) pln(rep78=5) frame(fr_2)

Contact

Jan Kabátek, The University of Melbourne

j.kabatek@unimelb.edu.au