Skip to content

Commit

Permalink
v1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
asjadnaqvi committed Nov 20, 2022
1 parent 6610f5a commit a047ecf
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 72 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![StataMin](https://img.shields.io/badge/stata-2015-blue) ![issues](https://img.shields.io/github/issues/asjadnaqvi/stata-streamplot) ![license](https://img.shields.io/github/license/asjadnaqvi/stata-streamplot) ![Stars](https://img.shields.io/github/stars/asjadnaqvi/stata-streamplot) ![version](https://img.shields.io/github/v/release/asjadnaqvi/stata-streamplot) ![release](https://img.shields.io/github/release-date/asjadnaqvi/stata-streamplot)

# streamplot v1.4
# streamplot v1.5

This package provides the ability to generate stream plots in Stata. It is based on the [Streamplot Guide](https://medium.com/the-stata-guide/covid-19-visualizations-with-stata-part-10-stream-graphs-9d55db12318a) that I released in December 2020.
This package provides the ability to generate stream plots in Stata. It is based on the [Streamplot Guide](https://medium.com/the-stata-guide/covid-19-visualizations-with-stata-part-10-stream-graphs-9d55db12318a) (December 2020).


## Installation
Expand All @@ -14,7 +14,7 @@ SSC (**v1.4**):
ssc install streamplot, replace
```

GitHub (**v1.4**):
GitHub (**v1.5**):

```
net install streamplot, from("https://raw.githubusercontent.com/asjadnaqvi/stata-streamplot/main/installation/") replace
Expand Down Expand Up @@ -50,11 +50,11 @@ graph set window fontface "Arial Narrow"

## Syntax

The syntax for **v1.4** is as follows:
The syntax for **v1.5** is as follows:

```
streamplot y x [if] [in], by(varname) [ palette(str) smooth(num) alpha(num) labcond(str) offset(num) droplow precent format
lcolor(str) lwidth(str) xlabel(str) ylabel(str) ylabsize(num) ylabcolor(str)
lcolor(str) lwidth(str) xlabel(str) ylabel(str) ylabsize(num) ylabcolor(str) recenter(top|mid|bot)
title(str) subtitle(str) note(str) xsize(num) ysize(num) scheme(str) name(str) ]
```

Expand Down Expand Up @@ -97,6 +97,21 @@ streamplot new_cases date if date > 22400, by(region) smooth(6)

<img src="/figures/streamplot2.png" height="600">

Recenter the graphs to top or bottom:

```
streamplot new_cases date if date > 22400, by(region) smooth(6) recenter(bot)
```

<img src="/figures/streamplot2_1.png" height="600">

```
streamplot new_cases date if date > 22400, by(region) smooth(6) recenter(top)
```

<img src="/figures/streamplot2_2.png" height="600">


```
streamplot new_cases date if date > 22400, by(region) smooth(6) ///
title("My Stata stream plot") /// note("test the note") ///
Expand Down Expand Up @@ -171,6 +186,10 @@ Please open an [issue](https://github.com/asjadnaqvi/stata-streamplot/issues) to

## Versions

**v1.5 (20 Nov 2022)**
- Option to recenter the graphs added.
- Improve the precision of the calculations.

**v1.4 (08 Nov 2022)**
- Major code cleanup.
- The command now does error checks on the number of observations.
Expand Down
Binary file modified figures/streamplot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/streamplot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/streamplot2_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/streamplot2_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/streamplot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/streamplot3_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/streamplot3_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/streamplot3_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/streamplot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/streamplot5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion installation/stata.toc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v 1.4
v 1.5
d STREAMPLOT: A package for creating streamplots in Stata
d
d Asjad Naqvi,
Expand Down
119 changes: 70 additions & 49 deletions installation/streamplot.ado
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*! streamplot v1.4 (08 Nov 2022)
*! streamplot v1.5 (20 Nov 2022)
*! Asjad Naqvi ([email protected])

* v1.5 (20 Nov 2022): recenter option added. improved variable precision.
* v1.4 (08 Nov 2022): Major code cleanup and some parts reworked. Observation checks. Palette options. label controls.
* v1.3 (20 Jun 2022). Add marker labels and format options
* v1.2 14 Jun 2022: passthru optimizations. error checks. reduce the default smoothing. labels fix
Expand Down Expand Up @@ -28,7 +29,7 @@ version 15
[ LColor(string) LWidth(string) labcond(string) ] ///
[ YLABSize(string) YLABel(varname) YLABColor(string) offset(real 0.12) droplow ] ///
[ xlabel(passthru) xtitle(passthru) title(passthru) subtitle(passthru) note(passthru) scheme(passthru) name(passthru) xsize(passthru) ysize(passthru) ] ///
[ PERCENT FORMAT(string) ]
[ PERCENT FORMAT(string) RECenter(string) ]



Expand Down Expand Up @@ -128,13 +129,6 @@ preserve
}








// prepare the dataset
collapse (sum) `yvar' if `touse', by(`xvar' `by')

xtset `by' `xvar'
Expand All @@ -148,22 +142,23 @@ preserve
tssmooth ma ``yvar'_ma7' = `yvar' , w(`smooth' 1 0)

// add the range variable on the x-axis
summ `xvar' if ``yvar'_ma7' != .
local xrmin = r(min)
local xrmax = r(max) + ((r(max) - r(min)) * `offset')
summ `xvar' if ``yvar'_ma7' != ., meanonly

local xrmin = r(min)
local xrmax = r(max) + ((r(max) - r(min)) * `offset')

cap drop stack_`yvar'
*cap drop stack_`yvar'

gen stack_`yvar' = .
gen double stack_`yvar' = .

sort `xvar' `by'
qui levelsof `xvar', local(lvls)
levelsof `xvar', local(lvls)

foreach y of local lvls {

qui summ `by'
qui replace stack_`yvar' = ``yvar'_ma7' if `xvar'==`y' & `by'==r(min)
qui replace stack_`yvar' = ``yvar'_ma7' + stack_`yvar'[_n-1] if `xvar'==`y' & `by'!=r(min)
summ `by', meanonly
replace stack_`yvar' = ``yvar'_ma7' if `xvar'==`y' & `by'==r(min)
replace stack_`yvar' = ``yvar'_ma7' + stack_`yvar'[_n-1] if `xvar'==`y' & `by'!=r(min)

}

Expand All @@ -183,56 +178,66 @@ preserve
foreach x of local idlabels {
local idlab_`x' : label `mylab' `x' // store the corresponding value label in a macro

}
}



reshape wide stack_`yvar' `yvar' , i(`xvar') j(`by')
reshape wide stack_`yvar' `yvar' , i(`xvar') j(`by')


foreach x of local idlabels { // here we know how many variables we have
foreach x of local idlabels { // here we know how many variables we have

lab var stack_`yvar'`x' "`idlab_`x''"
lab var `yvar'`x' "`idlab_`x''"
}
lab var stack_`yvar'`x' "`idlab_`x''"
lab var `yvar'`x' "`idlab_`x''"
}



gen stack_`yvar'0 = 0 // we need this for area graphs

ds `yvar'*
local items : word count `r(varlist)'
local items = `items' - 1

gen meanval_`yvar' = stack_`yvar'`items' / 2

ds `yvar'*
local items : word count `r(varlist)'
local items = `items' - 1

if "`recenter'" == "" | "`recenter'"=="middle" | "`recenter'"=="mid" | "`recenter'"=="m" {
gen double meanval_`yvar' = stack_`yvar'`items' / 2
}

if "`recenter'" == "bottom" | "`recenter'"=="bot" | "`recenter'"=="b" {
gen meanval_`yvar' = 0
}

if "`recenter'" == "top" | "`recenter'"=="t" {
local items2 = `items' + 1
gen double meanval_`yvar' = stack_`yvar'`items2'
}


foreach x of varlist stack_`yvar'* {
gen `x'_norm = `x' - meanval_`yvar'
}
foreach x of varlist stack_`yvar'* {
gen double `x'_norm = `x' - meanval_`yvar'
}

drop meanval*
drop meanval*


// this part is for the mid points
// this part is for the mid points

summ `xvar'
gen last = 1 if `xvar'==r(max)
summ `xvar'
gen last = 1 if `xvar'==r(max)


ds stack_`yvar'*norm
local items : word count `r(varlist)'
local items = `items' - 2
ds stack_`yvar'*norm
local items : word count `r(varlist)'
local items = `items' - 2



forval i = 0/`items' {
local i0 = `i'
local i1 = `i' + 1
forval i = 0/`items' {
local i0 = `i'
local i1 = `i' + 1

gen y`yvar'`i1' = (stack_`yvar'`i0'_norm + stack_`yvar'`i1'_norm) / 2 if last==1
}
gen double y`yvar'`i1' = (stack_`yvar'`i0'_norm + stack_`yvar'`i1'_norm) / 2 if last==1
}



Expand Down Expand Up @@ -281,7 +286,7 @@ drop lastsum*
if "`lwidth'" == "" local lwidth 0.05
if "`ylabcolor'" != "palette" local ycolor `ylabcolor'
if "`palette'" == "" {
local palette CET C1
local palette tableau
}
else {
tokenize "`palette'", p(",")
Expand All @@ -291,11 +296,27 @@ drop lastsum*



summ stack_`yvar'0_norm
local ymin = -1 * abs(r(min)) * 1.05
local ymax = abs(r(min)) * 1.05
summ stack_`yvar'0_norm, meanonly

if "`recenter'" == "" | "`recenter'" == "middle" | "`recenter'" == "mid" | "`recenter'" == "m" {

local ymin = -1 * abs(r(min)) * 1.05
local ymax = abs(r(min)) * 1.05
}

if "`recenter'" == "bottom" | "`recenter'" == "bot" | "`recenter'" == "b" {

local ymin = 0
local ymax = abs(r(min)) * 1.05
}

if "`recenter'" == "top" | "`recenter'" == "t" {

local ymin = -1 * abs(r(min)) * 1.05
local ymax = 0
}


ds stack_`yvar'*norm
local items : word count `r(varlist)'
local items = `items' - 2
Expand Down
6 changes: 3 additions & 3 deletions installation/streamplot.pkg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v 1.4
v 1.5
d {bf:STREAMPLOT}: A package for creating streamplots in Stata
d
d Requires: Stata version 15 or higher.
Expand All @@ -7,9 +7,9 @@ d KW: Stata
d KW: graphs
d KW: streamplot
d
d Distribution-Date: 20221108
d Distribution-Date: 20221120
d
d This version: 08 Nov 2022
d This version: 20 Nov 2022
d First version: 05 Dec 2021
d License: MIT
d
Expand Down
27 changes: 13 additions & 14 deletions installation/streamplot.sthlp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{smcl}
{* 08Nov2022}{...}
{* 20Nov2022}{...}
{hi:help streamplot}{...}
{right:{browse "https://github.com/asjadnaqvi/stata-streamplot":streamplot v1.4 (GitHub)}}
{right:{browse "https://github.com/asjadnaqvi/stata-streamplot":streamplot v1.5 (GitHub)}}

{hline}

Expand All @@ -16,13 +16,10 @@ The command is based on the following guide on Medium: {browse "https://medium.c

{cmd:streamplot} {it:y x} {ifin}, {cmd:by}(varname) {cmd:[} {cmd:palette}({it:str}) {cmd:smooth}({it:num}) {cmd:labcond}({it:str}) {cmd:offset}({it:num}) {cmd:alpha}({it:num}) {cmd:droplow}
{cmdab:lc:olor}({it:str}) {cmdab:lw:idth}({it:str}) {cmdab:ylabs:ize}({it:num}) {cmdab:ylabc:olor}({it:color}|{it:palette}) {cmd:percent} {cmd:format}({it:str})
{cmd:xlabel}({it:str}) {cmd:xtitle}({it:str}) {cmd:ytitle}({it:str}) {cmd:title}({it:str}) {cmd:subtitle}({it:str}) {cmd:note}({it:str})
{cmd:xlabel}({it:str}) {cmd:xtitle}({it:str}) {cmd:ytitle}({it:str}) {cmd:title}({it:str}) {cmd:subtitle}({it:str}) {cmd:note}({it:str}) {cmdab:rec:enter}({it:mid|top|bot})
{cmd:ysize}({it:num}) {cmd:xsize}({it:num}) {cmd:scheme}({it:str}) {cmd:name}({it:str}) {cmd:]}





{p 4 4 2}
The options are described as follows:

Expand All @@ -37,14 +34,17 @@ The options are described as follows:
{p2coldent : {opt droplow}}If there are fewer than 10 observations per {bf:by()} variable, the command will give an error. If only some groups have this issue, then use the {opt droplow} option to drop these observations.
If all groups have few observations, then {cmd:streamplot} cannot be used.{p_end}

{p2coldent : {opt palette(str)}}Color name is any named scheme defined in the {stata help colorpalette:colorpalette} package.
Default is {stata colorpalette CET C1:{it:CET C1}}.{p_end}

{p2coldent : {opt smooth(num)}}The smoothing parameter defined in terms of last observations to use. The default value is {it:3}.
A value of 0 implies no smoothing.{p_end}

{p2coldent : {opt palette(str)}}Color name is any named scheme defined in the {stata help colorpalette:colorpalette} package.
Default is {stata colorpalette tableau:{it:tableau}}.{p_end}

{p2coldent : {opt alpha(num)}}The transparency of area fills. The default value is {it:100}.{p_end}

{p2coldent : {opt rec:enter(options)}}This option changes where the graph is recentered. The default option is {opt rec:enter(middle)}. Additional options are {opt rec:enter(top)} or {opt rec:enter(bottom)}.
For brevity, the following can be specified: {it:middle} = {it:mid} = {it:m}, {it:top} = {it:t}, {it:bottom} = {it:bot} = {it:b}.{p_end}

{p2coldent : {opt offset(num)}}Extends the x-axis range to accommodate labels. The default value is {it:0.12} or 12% of {it:xmax-xmin}.{p_end}

{p2coldent : {opt ylabc:olor(str)}}Either takes on a named Stata color, e.g. {it:ylabc(red)} for red labels.
Expand Down Expand Up @@ -87,7 +87,7 @@ Even if you have these installed, it is highly recommended to check for updates:

{title:Examples}

See {browse "https://github.com/asjadnaqvi/streamplot":GitHub}.
See {browse "https://github.com/asjadnaqvi/stata-streamplot":GitHub}.

{hline}

Expand All @@ -98,10 +98,10 @@ Marc Kaulisch has suggested several useful features across various versions.

{title:Package details}

Version : {bf:streamplot} v1.4
This release : 08 Nov 2022
Version : {bf:streamplot} v1.6
This release : 20 Nov 2022
First release: 06 Aug 2021
Repository : {browse "https://github.com/asjadnaqvi/streamplot":GitHub}
Repository : {browse "https://github.com/asjadnaqvi/stata-streamplot":GitHub}
Keywords : Stata, graph, stream plot
License : {browse "https://opensource.org/licenses/MIT":MIT}

Expand All @@ -110,7 +110,6 @@ E-mail : [email protected]
Twitter : {browse "https://twitter.com/AsjadNaqvi":@AsjadNaqvi}



{title:References}

{p 4 8 2}Jann, B. (2018). {browse "https://www.stata-journal.com/article.html?article=gr0075":Color palettes for Stata graphics}. The Stata Journal 18(4): 765-785.
Expand Down

0 comments on commit a047ecf

Please sign in to comment.