*! version 1.1 April 2022

Title

mscatter -- Module to create Scatters-plots across different groups.

Syntax

mscatter yvar xvar [if] [in] [aweight], [over(varname)] [mscatter_options scatter_options twoway_options]

options

yvar xvar You need to specify two numeric variables (y and x) that will be used for the scatter plot.

over(varname) Indicates a variable that defines groups to be used for the scatter plot. See examples and description. If Omitted, it would be better to use scatter.

legend options

alegend When requested, a label will be added to the graph, using the levels of over(varname).
The default is to ommit legends.

legend(Options) Can be combined with alegend. It is used to add legends to the scatter plot.

strict Request to use strict option to determine the value labels. With "strict", if a value has no label, nothing will be used in legend.
The default option uses either the label (when available) or over_variable value.

fit(fit_cmds, opts) Request to produce fitted values and plots them in the scatter. One can use lfit, qfit, fpfit, or lpoly (or the CI alternatives).
It also allows you to specify weights and options depending on each "fit" command.

noscatter Ommits plotting the Scatterplots. Useful if you use "fit()" options.

color options

color(*) Can be used to specify colors for each group defined by over(varname).
It has two options. You can either specify a list of colors, or a variable that define those colors.

colorpalette(*) An alternative approach to specify colors. This uses the command colorpalette
to define colors and use them for the scatter plot.

If neither option is used, colors are assigned based on the current scheme, which uses up to 15 different
colors.

scatter options

One can use other scatter specific options including msymbol, msize, msangle,
mfcolor, mlcolor, mlwidth, mlalign, jitter. Be aware that the same option will be applied to each scatter group
defined by over(varname).

twoway options

It is also possible to use any of the twoway options, including labels, titles, name, notes, etc.

Technically, you can combine weights with by(). However, this tends to create problems if there are any groups not observed across the by() variable

Description

This module aims to provide an easy way to create scatter plots over different groups.

For example, say that one is interested in ploting wages against age, across gender. What you would normally do would be

twoway scatter wage age if sex==1 || scatter wage age if sex==2, legend(order(1 "Men" 2 "Women"))

Instead you could create a similar graph using the following syntax:

mscatter wage age, over(sex) alegend

This command should also facilitate using different colors to each sub group. For example, one can simply
provide the list of colors using color(colorlist). When you have many subgroups, it may be easier
to provide a variable with the desired colors color(varname). Finally, you coud also use the option
colorpalette() to select colors for each group defined by over(varname).

This command should also make it easy to add "fitted" value plots to scatter plots. Some people may find that useful.

One last thing. If you see something weird as an ouput, its probably an easter egg! let me know if you find it

See examples for details

Examples

Lets start by loading some data:

. use http://fmwww.bc.edu/RePEc/bocode/o/oaxaca.dta

Say that you want to see the relationship between wages and experience, but differentiating
by marital status:

gen mstatus=single+2*married+3*divorced
mscatter lnwage age, over(mstatus)

But now you want to add a label to differentiate all groups:

label define mstatus 1 Single 2 Married 3 Divorced

label values mstatus mstatus

mscatter lnwage age, over(mstatus) alegend

But what if you want to try using colors other than the default. You have three options

Provide a variable with a list of colors

`"gen mycolor="forest_green" if mstatus==1 "'

`"replace mycolor="gold" if mstatus==2"'

`"replace mycolor="lavender" if mstatus==3"'

mscatter lnwage age, over(mstatus) alegend color(mycolor)

Specify the list of colors manually

mscatter lnwage age, over(mstatus) alegend color(forest_green gold lavender)

Or use colorpalette:

mscatter lnwage age, over(mstatus) alegend colorpalette(viridis)

Other than that, you can use standard scatter options, and two way options

`"mscatter lnwage age, over(mstatus) alegend colorpalette(magma) msize(4) mfcolor(%40) title("Wages vs age") subtitle("by Marital Status") "'

Aknowledgement

This command came up to existance because I do this kind of graphs often just to visualize multiple groups at the same time. However, it was because someone else ask the question (Davis Kedrosky), that I decided to write up command for this.

Also, colorpalette is a very powerful command by Ben Jann. Without it, playing with colors would be far more difficult!

Author

Fernando Rios-Avila

Levy Economics Institute of Bard College

Blithewood-Bard College

Annandale-on-Hudson, NY

friosavi@levy.org

Also see

Help: colorpalette, scatter