*! version 1.1 21sep2022

Title

plotshares -- Plot conditional shares (a visual analog of tabulate twoway)

Syntax

plotshares varname [if] [in]
[weight]
, over(groupvar) [options]

where varname is the (categorical) outcome variable and groupvar is the conditioning variable

Basic options
over(groupvar) specify the conditioning variable

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

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

invert invert the order of stacked categories on the y-axis frame_name

nostack do not use stacked output presentation (i.e., each share/frequency category is measured from zero, and the default graph_type is set to line)

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

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 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.

area_options change look of area plots

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

varname and over(groupvar) need to be specified to produce a new plot. They do 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

plotshares is a command that visualizes conditional shares and frequencies of categorical variables (i.e., the output of tabulate twoway commands).

By default, plotshares produces relative shares of varname categories, conditional on the unique values of groupvar (with the shares summing up to 1).
Setting output_type to frequencies produces frequencies of varname instead. By default, plotshares uses stacked presentation of the output categories. Option nostack produces non-stacked shares/frequencies, instead.

plotshares avoids time-consuming memory operations performed by native graphing commands.
By leveraging the data frame environment, it proves extremely fast in very large datasets.

plotshares 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.
plotshares can be also combined with other commands from the plot suite (plottabs, plotbetas, and plotmeans).

Customization: conditional plots can be customized by selecting your preferred twoway graph_type, and adjusting it further using the twoway_options and other options specific to the given graph_type.

NOTE: Some options (such as area/bar color adjustments) can only be adjusted outside the plotshares environment.
To change the area/bar colors, print out the twoway command that displays the chart (use the option command),
add the custom colors to the respective sub-graph options, and parse the adjusted twoway command directly. Direct twoway parsing is as fast as plotshares, plotonly.

Examples

Basic use:

. clear all

. sysuse nlsw88

* stacked shares:

. plotshares race, over(age)

* non-stacked shares (in a new graph):

. plotshares race, over(age) nostack clear

Plot stacked frequencies instead:

. clear all

. sysuse nlsw88

. plotshares race, over(age) output(frequency) graph(bar)

Flip the ordering, use a custom scheme, and combine with a plotmeans graph:

. ssc install schemepack

. clear all

. sysuse nlsw88

. plots race, over(age) invert

. plotm ttl_, over(age) graph(connect) plname(Yrs of Exp) scheme(white_ptol) color(red) yaxis(2) yzero

Frames

See plottabs / Frames

Contact

Jan Kabátek, The University of Melbourne

j.kabatek@unimelb.edu.au