*! version 1.1 21sep2022

Title

plotmeans -- Plot conditional means (a visual analog of mean y, over(x))

Syntax

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

where varname is the outcome and groupvar is the conditioning variable

Basic options
over(groupvar) specify the conditioning variable

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

rgraph(rgraph_type) specify the twoway rgraph_type for confidence intervals:  rarea(default)/rbar/rcap/rspike/etc.

ci(#,ci_options) specify the confidence level for confidence intervals:  # = 0...100, 95 = default, off = suppress CIs

  ci_options denote rgraph_type customization options (e.g., color and transparency)

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

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

Description

plotmeans is a command that visualizes conditional sample means of varname over groupvar (i.e., the output of mean varname, over(groupvar)).
By default, it also visualizes the corresponding confidence intervals .

plotmeans avoids the time-consuming memory operations performed by native graphing commands.
By leveraging the data frame environment, it proves extremely fast in very large datasets.
Furthermore, because plotmeans employs a factorized OLS model, it also outperforms the original mean command.
Note that the routine is least memory-intensive when groupvar is integer (non-integer groupvar variables are allowed, but the routine has to create an auxiliary integer tempvar for factorization).

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

Customization: the conditional-mean plots can be customized by selecting the preferred twoway graph_type, and adjusting it further using the twoway_options and other options specific to the given graph_type.
The confidence-interval plots can be customized by selecting the preferred twoway rgraph_type, and adjusting it further using the ci_options suboption of ci(#,ci_options). See examples below.

Examples

Basic use:

. clear all

. sysuse nlsw88

. plotmeans ttl_exp, over(age)

* produce a whisker plot instead:

. plotmeans ttl_exp, over(age) graph(scatter) rgraph(rcap) clear

Compare conditional means for two groups:

. clear all

. sysuse nlsw88

. plotmeans ttl_exp if union==0, over(age) graph(connect) plname(Non-union)

. plotmeans ttl_exp if union==1, over(age) graph(connect) plname(Union)

Combine multiple graphs from the plot suite:

. clear all

. sysuse nlsw88

. plottabs, over(age) graph(bar) plname(Frequencies) color(green%15)

. plotmeans ttl_exp, over(age) graph(connect) plname(Yrs of Exp) yaxis(2)

Customize line color, CI color & CI transparency:

. clear all

. sysuse nlsw88

. plotmeans ttl_exp, over(age) color(green) ci(, color(gold%35))

Plot data management:

See plottabs / Plot data management

Frames

See plottabs / Frames

Contact

Jan Kabátek, The University of Melbourne

j.kabatek@unimelb.edu.au