Skip to content

Commit

Permalink
v1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
asjadnaqvi committed Apr 25, 2024
1 parent dfeec83 commit d77805a
Show file tree
Hide file tree
Showing 25 changed files with 148 additions and 91 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ authors:
given-names: "Asjad"
title: "Stata package ``streamplot''"
version: 1.7
date-released: 2024-04-01
date-released: 2024-04-25
url: "https://github.com/asjadnaqvi/stata-streamplot"
59 changes: 47 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@

![streamplot](https://github.com/asjadnaqvi/stata-streamplot/assets/38498046/4b7bbaab-8667-4f47-a119-ec93dcd607a3)

![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)

---

[Installation](#Installation) | [Syntax](#Syntax) | [Examples](#Examples) | [Feedback](#Feedback) | [Change log](#Change-log)

---

# streamplot v1.7
(01 Apr 2024)
![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.8
(25 Apr 2024)

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

Expand All @@ -24,40 +25,34 @@ SSC (**v1.61**):
ssc install streamplot, replace
```

GitHub (**v1.7**):
GitHub (**v1.8**):

```
net install streamplot, from("https://raw.githubusercontent.com/asjadnaqvi/stata-streamplot/main/installation/") replace
```



The `palettes` package is required to run this command:

```
ssc install palettes, replace
ssc install colrspace, replace
```

Even if you have these packages installed, please check for updates: `ado update, update`.

If you want to make a clean figure, then it is advisable to load a clean scheme. These are several available and I personally use the following:

```
ssc install schemepack, replace
set scheme white_tableau
```

You can also push the scheme directly into the graph using the `scheme(schemename)` option. See the help file for details or the example below.

I also prefer narrow fonts in figures with long labels. You can change this as follows:

```
graph set window fontface "Arial Narrow"
```



## Syntax

The syntax for the latest version is as follows:
Expand Down Expand Up @@ -177,7 +172,7 @@ streamplot new_cases date if date > 22400, by(region) smooth(6) palette(CET D02)

or a custom graph scheme:

```
```stata
streamplot new_cases date if date > 22600, by(region) smooth(6) palette(CET CBD1) ///
title("My Stata stream plot", size(6)) subtitle("with colorblind-friendly colors", size(4)) ///
labcond(20000) ylabs(2) lc(black) lw(0.03) offset(25) xtitle("") ///
Expand Down Expand Up @@ -296,13 +291,53 @@ streamplot value_real year if countrycode=="TSA", by(category) cat(splitvar) smo
<img src="/figures/streamplot_tline7.png" width="100%">


## v1.8 updates

```stata
streamplot value_real year if countrycode=="TSA", by(category) cat(splitvar) smooth(2) palette(tab Green-Orange-Teal) ///
yline(0) xsize(2) ysize(1) tline tlc(black) tlw(0.5) tlp(dash) xtitle("") xline(2020) ///
xlabel(1990(2)2022, angle(90)) labsize(2.2) offset(8) labprop ///
title("{fontface Arial Bold:GDP Expenditures in South Asia (Constant 2015 USD billions)}") ///
note("World Bank Open Data.", size(2))
```

<img src="/figures/streamplot_labprop1.png" width="100%">

```
streamplot value_real year if countrycode=="TSA", by(category) smooth(2) palette(tab Green-Orange-Teal) ///
xsize(2) ysize(1) xtitle("") ///
xlabel(, angle(90)) labsize(2.2) offset(8) recenter(bottom) labprop ///
title("{fontface Arial Bold:GDP Expenditures in South Asia (Constant 2015 USD billions)}") ///
note("World Bank Open Data.", size(2))
```

<img src="/figures/streamplot_labprop2.png" width="100%">

```
streamplot value_real year if countrycode=="TSA", by(category) smooth(2) palette(tab Green-Orange-Teal) ///
xsize(2) ysize(1) xtitle("") ///
xlabel(, angle(90)) labsize(2.2) offset(8) recenter(bottom) labprop labcolor(palette) ///
title("{fontface Arial Bold:GDP Expenditures in South Asia (Constant 2015 USD billions)}") ///
note("World Bank Open Data.", size(2))
```

<img src="/figures/streamplot_labprop3.png" width="100%">


## Feedback

Please open an [issue](https://github.com/asjadnaqvi/stata-streamplot/issues) to report errors, feature enhancements, and/or other requests.


## Change log

**v1.8 (25 Apr 2024)**
- Added `labprop` and `labscale()` options to allow easy label scaling.
- Added `share` and `percent` as substitutes.
- Major code rework to optimize the speed of the graph generation.
- Generic twoway options added.
-

**v1.7 (01 Apr 2024)**
- Added trendline options: `tline`, `tlcolor()`, `tlpattern()`, `tlwidth()`.
- Added additional checks for plotting data.
Expand Down
Binary file modified data/streamdata.dta
Binary file not shown.
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 modified 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 modified 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/streamplot6.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/streamplot7.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/streamplot8.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/streamplot_labprop1.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/streamplot_labprop2.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/streamplot_labprop3.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/streamplot_tline1.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/streamplot_tline2.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/streamplot_tline3.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/streamplot_tline4.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/streamplot_tline7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
144 changes: 86 additions & 58 deletions installation/streamplot.ado
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*! streamplot v1.7 (01 Apr 2024)
*! streamplot v1.8 (25 Apr 2024)
*! Asjad Naqvi ([email protected])

* v1.8 (25 Apr 2024): added labscale option. Added percent/share as substitutes. more flexible for generic options.
* v1.7 (01 Apr 2024): trendline, yline, drop if by() is missing
* v1.61 (15 Jan 2024): fixed wrong locals. changed ylab to just lab.
* v1.6 (15 Oct 2023): cat() option added. yrev, labcond() fixed. major code cleanup.
Expand All @@ -21,14 +22,12 @@ program streamplot, sortpreserve
version 15

syntax varlist(min=2 max=2 numeric) [if] [in], by(varname) ///
[ palette(string) alpha(real 100) smooth(real 3) ] ///
[ LColor(string) LWidth(string) labcond(real 0) ] ///
[ LABSize(string) LABColor(string) offset(real 15) ] ///
[ xlabel(passthru) xtitle(passthru) title(passthru) subtitle(passthru) note(passthru) scheme(passthru) name(passthru) xsize(passthru) ysize(passthru) ] ///
[ PERCENT FORMAT(string) RECenter(string) ] ///
[ aspect(passthru) saving(passthru) NOLABel nodraw xscale(passthru) graphregion(passthru) ] /// v1.5x
[ palette(string) alpha(real 100) smooth(real 3) LColor(string) LWidth(string) labcond(real 0) ] ///
[ LABSize(string) LABColor(string) offset(real 15) format(string) RECenter(string) ] ///
[ NOLABel nodraw ] /// v1.5x
[ cat(varname) YREVerse ] /// // v1.6
[ tline TLColor(string) TLWidth(string) TLPattern(string) droplow yline(passthru) ] // v1.7
[ tline TLColor(string) TLWidth(string) TLPattern(string) droplow ] /// // v1.7
[ * labprop labscale(real 0.3333) percent share ] // 1.8 options


// check dependencies
Expand All @@ -54,16 +53,13 @@ version 15
}
}

if `"`format'"' == "" local format "%12.0fc"



qui {
preserve

keep if `touse'


cap confirm numeric var `by'
if !_rc {
drop if `by'==.
Expand Down Expand Up @@ -285,63 +281,66 @@ preserve

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

gen double _ylab = .
gen double _yval = .
gen double _yshare = .
gen _yname = ""

ds _stack*_norm
local items : word count `r(varlist)'
local items = `items' - 2


local counter = 1

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

gen double _ylab`i1' = (_stack`i0'_norm + _stack`i1'_norm) / 2 if last==1
local t : var lab _ma`i1'

replace _ylab = (_stack`i0'_norm[_N] + _stack`i1'_norm[_N]) / 2 in `counter'

replace _yval = _yvar`i1'[_N] in `counter'

replace _yname = "`t'" in `counter'

local ++counter
}

egen double _lastsum = rowtotal(_ma*) if last==1


foreach x of varlist _ma* {
gen double `x'_share = (`x' / _lastsum) * 100
}

drop _lastsum

sum _yval, meanonly
replace _yshare = (_yval / `r(sum)') * 100

summ `xvar', meanonly
gen _xlab = r(max) if _yval!=.

**** automate this part


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

foreach x of numlist 1/`items' {

if `"`percent'"' != "" {
local ylabvalues `"string(_ma`x'_share, `"`format'"') + "%""'

local labvar _ma`x'_share

}
else {
local ylabvalues `"string(_yvar`x', `"`format'"')"'

local labvar _yvar`x'
}



local t : var lab _ma`x'
gen _label`x' = "`t'" + " (" + `ylabvalues' + ")" if last==1 & `labvar' >= `labcond'

}


if "`format'" == "" {
if "`percent'"=="" & "`share'"=="" {
local format %15.0fc
}
else {
local format %6.1f
}
}


if "`percent'" != "" | "`share'"!="" {
gen _label = _yname + " (" + string(_yshare, "`format'") + "%)" if _yval!=.
}
else {
gen _label = _yname + " (" + string(_yval, "`format'") + ")" if _yval!=. //if last==1 & `labvar' >= `labcond'
}


if "`labsize'" == "" local labsize 1.6
if "`lcolor'" == "" local lcolor white
Expand All @@ -358,7 +357,7 @@ preserve



summ _stack0_norm, meanonly
summ _stack0_norm, meanonly

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

Expand Down Expand Up @@ -397,51 +396,74 @@ summ _stack0_norm, meanonly
local trendline (line _linevar `xvar', lc(`tlcolor') lw(`tlwidth') lp(`tlpattern'))
}



if "`nolabel'"=="" & "`labprop'" == "" & "`labcolor'" =="" {
local labels `labels' (scatter _ylab _xlab if _yval >= `labcond' , mlabel(_label) mcolor(none) mlabsize(`labsize') mlabcolor("`labcolor'"))
}


summ _yval, meanonly
local height = r(max)


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


local counter = 1

forval x = 0/`items' {

local numcolor = `items' + 1
local numcolor = `items' + 1

colorpalette `palette', n(`numcolor') nograph `poptions'
colorpalette `palette', n(`numcolor') nograph `poptions'

local x0 = `x'
local x1 = `x' + 1

local areagraph `areagraph' rarea _stack`x0'_norm _stack`x1'_norm `xvar', fcolor("`r(p`x1')'") fi(100) lcolor(`lcolor') lwidth(`lwidth') ||


if "`labcolor'" == "palette" {
local ycolor "`r(p`x1')'"
}


if "`nolabel'"=="" {
local labels `labels' (scatter _ylab`x1' `xvar' if last==1, mlabel(_label`x1') mcolor(none) mlabsize(`labsize') mlabcolor("`ycolor'")) ||
if "`labprop'" != "" {
summ _yval in `counter' , meanonly
local labwgt = `labsize' * (r(max) / `height')^`labscale'

local labels `labels' (scatter _ylab _xlab in `counter' if _yval >= `labcond' , mlabel(_label) mcolor(none) mlabsize(`labwgt') mlabcolor("`ycolor'"))
local ++counter
}

}

if "`labprop'"=="" & "`labcolor'"=="palette" {

local labels `labels' (scatter _ylab _xlab in `counter' if _yval >= `labcond', mlabel(_label) mcolor(none) mlabsize(`labsize') mlabcolor("`ycolor'"))
local ++counter
}
}
}



*** final graph

twoway ///
`areagraph' ///
`labels' /// // (linei 0 date, lc(gs10) lp(dash)) ///
`labels' ///
`trendline' ///
, ///
legend(off) ///
yscale(noline) ///
ytitle("") `xtitle' ///
ylabel(`ymin' `ymax', nolabels noticks nogrid) ///
`xlabel' xscale(noline range(`xrmin' `xrmax')) `yline' ///
`title' `subtitle' `note' `scheme' `xsize' `ysize' `name' `aspect' `saving' `nodraw' `xscale' `graphregion'


xscale(noline range(`xrmin' `xrmax')) ///
`options'

*/

restore
Expand All @@ -454,3 +476,9 @@ end
*********************************
******** END OF PROGRAM *********
*********************************






Loading

0 comments on commit d77805a

Please sign in to comment.