*! version 1.0 April 2022

Title

ridgeline_plot -- Module create ridgeline/stack/stream plots across different groups.

Syntax

ridgeline_plot yvar xvar [if] [in] [iweight], [over(varname)] [ridgeline_plot_options color_options legend_options twoway_options]

options

yvar xvar You need to specify a two variables. xvar is usually a time variable, and yvar is the variable you want to plot. Similar to line.

When there is more than one observation per xvar and over(), the command will first estimate the mean within xvar, before preparing the plot.

sum When there is more than one observation per xvar and over(), one can request obtaining the weighted sum by xvar, instead of mean.

over(varname) Indicates a variable that defines the groups to be used for the joy_plot. If you omit this, you probably would prefer using line directly. See Descriptions for details.

ridgeline_plot options

This options are used to modify some aspects specific to the looks of the ridgeline_plot

radj(#) When used, the Range used for the plots will be extended beyong the max or min values of the varname. Defeult 0.

range(#1 #2) When used, It sets the range for the plot to be between #1 and #2. The default uses the sample min and max.

dadj(#) When used, the hight of the lines to be adjusted. This allows for plots to overlap each other. Default is 1.

bwadj(#) Should be between 0 and 1. It is used to determine the bandwidth for each subplot. When bwadj=0, all plots use the simple Bandwidth average. When bwadj=1, all plots will use the bandwidth determined by lpoly. One can choose something inbetween.

bwadj2(#) Any possitive number. It is use to change the Bandwith across all plots. One can, for example, modify all
bandwidths to be half (bwdj2=0.5) of the one originally estimated. Default is 0.2.

bwadj3(#) Exogenously provides a bandwidth for all lines.

kernel(kfun) This can be used to select a particular type of kernel function for the plots. Default is triangular.

degree(#) Determines the degree of the local regression for smoothing. Default is 0.

nobs(#) This is used to define how many points to be used for the plot. Larger number creates a smoother figure, but uses more memory. Default is 200.

normalize This is used to normalize the height of the lines for all groups to be equal to 1. The default is for lines height to be relative to the populations height. See Example for details.

ridgeline_plot options:Stack and Stream

stack Request to produce a stack line plot.

stack100 Request to produce a stack line plot, normalized to 100 (Shares).

stream([#]) Request to produce a stream like plot. Default is to produce a graph centered at 0. When a number # is provided, then the height of that group becomes the pivot point for stacking. can be combined with stack & stack100.

half If stream () is used, half request the "middle" of the #th group to be the pivot point for stacking.

Other options

These options can be used to modify the look of the legend, or text describing the groups.

strict Unless specified, the value labels, or values of over(variable) will be used to label groups. using "strict"
will not no text, if a value label is undefined.

notext WHen used, no text will be added on the vertical axis.

textopt(opts) When producing joy_plots, one can use this option to change some aspects of the group indentifiers. See added_text_options or details.

right When used, it will put the text identifying groups on the right of the plot. The default is to add this text on the left.

offset(#) When used, It request to offset (move) the text # points to the right (+) or left (-).

gap0 When used, all joy_plots will be drawn starting at 0. This will be similar to producing various kdensity plots, but using areas. Cannot be combined with violin

alegend When used, It will add a legend with all values defined in over(varname) to the graph. The default is not to show any legends. Can be combined with legend_options

color options

color(colorlist) Can be used to specify colors for each group defined by over(varname), using
a list of colors. If you add only fewer colors than groups in over(varname), subsequent groups will use last specified color.
For example if you type color(red blue), but over(var) has 3 groups, the last group will also be assigned the "blue" color.

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.

Other color options

One can use other rarea specific options including fcolor, fintensity, lcolor,
lwidth, lpattern, lalign, lstyle. Be aware that the same option will be applied to each sub rarea defined by over(varname).

twoway options

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

Description

This module aims to provide an easy way to create ridgeline plots, as well as stream plots and stackline plots. using smoothed versions of the data via lpoly(). Using smooth lines helps dealing with large datasets.

This program should also facilitate using different colors to each sub group. For example, one can simply
provide the list of colors using color(colorlist). You coud also use the option
colorpalette() to select colors for each group defined by over(varname).

See examples for details

Examples

For this example, lets start by loading the ancillary data covid_small. This contains data for South america and Oceania only

`"use covid_small.dta"'

Now, say you want to do a basic ridgeline_plot, looking at new dealths across time by country in South America

`"ridgeline_plot new_deaths date , over(country)"'

This may give you graph with alot of data for brazil, but almost no information for other countries (relatively small)

So what I can do is place the label somwhere else, as well as Normalize figures.

`"ridgeline_plot new_deaths date , over(country) normalize textopt(size(small) placement(e))"'

That should look better. But could also change from text to legend, and adjust overlap

`"ridgeline_plot new_deaths date , over(country) normalize notext alegend dadj(3)"'

What if what you are interested is in stackbars

`"ridgeline_plot new_deaths date , over(country) notext alegend stack"'

or if what you are interested is in stackbars at 100

`"ridgeline_plot new_deaths date , over(country) notext alegend stack100"'

Streamlines are also easy to build

`"ridgeline_plot new_deaths date , over(country) notext alegend stream"'

Or if you want to shift the stream up a bit

`"ridgeline_plot new_deaths date , over(country) notext alegend stream(3)"'

Aknowledgement

This command came up to existance because I do this kind of graphs often enough to visualize multiple groups at the same time. Also, Asjad started a small "revolution" of color and dataviz in Stata that I wanted to contribute.

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, line