Skip to content

Commit

Permalink
v1.3 updated
Browse files Browse the repository at this point in the history
Fixes #2 and #3
  • Loading branch information
asjadnaqvi committed Dec 14, 2022
1 parent 0521de6 commit 093dd7d
Show file tree
Hide file tree
Showing 34 changed files with 108 additions and 47 deletions.
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![StataMin](https://img.shields.io/badge/stata-2015-blue) ![issues](https://img.shields.io/github/issues/asjadnaqvi/stata-treemap) ![license](https://img.shields.io/github/license/asjadnaqvi/stata-treemap) ![Stars](https://img.shields.io/github/stars/asjadnaqvi/stata-treemap) ![version](https://img.shields.io/github/v/release/asjadnaqvi/stata-treemap) ![release](https://img.shields.io/github/release-date/asjadnaqvi/stata-treemap)


# treemap v1.21
# treemap v1.3


This package provides the ability to draw treemaps Stata.
Expand All @@ -13,13 +13,13 @@ It is based on D3's [treemap](https://observablehq.com/@d3/treemap) and Python's

The package can be installed via SSC or GitHub. The GitHub version, *might* be more recent due to bug fixes, feature updates etc, and *may* contain syntax improvements and changes in *default* values. See version numbers below. Eventually the GitHub version is published on SSC.

The SSC version (**v1.1**):
The SSC version (**v1.21**):

```
ssc install treemap, replace
```

Or it can be installed from GitHub (**v1.21**):
Or it can be installed from GitHub (**v1.3**):

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

## Syntax

The syntax for v1.21 is as follows:
The syntax for **v1.3** is as follows:

```
treemap numvar [if] [in], by(variables (min=1, max=3))
[ xsize(num) ysize(num) format(str) labcond(num) pad(list) fi(list)
[ xsize(num) ysize(num) format(str) labcond(num) pad(list) fi(list) percent
labsize(list) linewidth(list) linecolor(list)
addtitles novalues nolabels labsize(num) titlegap(num)
labprop titleprop colorprop labscale(num) title(str) subtitle(str)
addtitles novalues nolabels labsize(num) titlegap(num) labgap(str)
labprop titleprop colorprop labscale(num) title(str) subtitle(str)
note(str) scheme(str) name(str) palette(str) ]
```

Expand Down Expand Up @@ -264,13 +264,34 @@ treemap y_TOT if NUTS0=="DE", by(NUTS3 NUTS2 NUTS1) linew(none 0.1 none) linec(w

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

### v1.3 updates

```
treemap y_TOT if NUTS0=="DK", by(NUTS3 NUTS2) addtitles labsize(2) ///
title("Population of Denmark at NUTS2 and NUTS3 level") subtitle("% of total") percent
```

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

```
treemap y_TOT if NUTS0=="DK", by(NUTS3 NUTS2) addtitles labsize(2) title("Population of Denmark at NUTS2 and NUTS3 level") subtitle("% of total") ///
percent format(%3.1f) palette(CET C7) labgap(1)
```

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

## Feedback

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


## Versions

**v1.3 (14 Dec 2022)**
- Fixed issue with the defaults in value formatting.
- Add `percent` option that shows percentage share of the total value rather than actual values.
- Added a `labgap()` option that allows the distance of the labels and the values in boxes to be adjusted.

**v1.21 (22 Nov 2022)**
- Removed error where duplicate values were causing categories to be dropped.
- Stablized the sort order to prevent random trees across different runs.
Expand Down
Binary file added figures/treemap-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/treemap-1_old.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/treemap1.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/treemap10.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/treemap11.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/treemap12.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/treemap13.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/treemap14.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/treemap15.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/treemap16.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/treemap17.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/treemap18.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/treemap19.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/treemap2.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/treemap20.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/treemap21.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/treemap22.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/treemap23.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/treemap24.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/treemap25.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/treemap26.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/treemap27.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/treemap3.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/treemap4.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/treemap5.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/treemap6.png
Binary file modified figures/treemap7.png
Binary file modified figures/treemap8.png
Binary file modified figures/treemap9.png
8 changes: 4 additions & 4 deletions installation/stata.toc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
v 1.21
d 'TREEMAP': A Stata package for treemaps
v 1.3
d 'TREEMAP': A Stata package for tree maps
d
d Distribution-Date: 20221122
d Distribution-Date: 20221214
d
d Asjad Naqvi,
d Asjad Naqvi
d [email protected]
d
d https://medium.com/the-stata-guide
Expand Down
76 changes: 52 additions & 24 deletions installation/treemap.ado
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
*! Treemap v1.21 (22 Nov 2022)
*! Treemap v1.3 (14 Dec 2022)
*! Asjad Naqvi ([email protected])

* v1.3 (14 Dec 2022): fixed formatting of labels. Added percent option. Add labgap option.
* v1.21 (22 Nov 2022): fixed a bug where duplicate values were causing categories to be dropped.
* v1.2 (22 Sep 2022): Negative values check. Control over fill intensity.
* v1.1 (13 Sep 2022): Label, color, title scaling. More options for controls. More checks. Better defaults.
* v1.0 (08 Sep 2022): First release.

cap prog drop treemap

prog def treemap, sortpreserve
prog def treemap, // sortpreserve

version 15

syntax varlist(numeric max=1) [if] [in], by(varlist min=1 max=3) ///
[ XSize(real 5) YSize(real 3) format(str) palette(string) ADDTitles NOVALues NOLABels cond(string) ] ///
[ title(passthru) subtitle(passthru) note(passthru) scheme(passthru) name(passthru) ] ///
[ pad(numlist max=3) labprop labscale(real 0.3333) labcond(real 0) colorprop titlegap(real 0.1) titleprop LINEWidth(string) LINEColor(string) LABSize(string) ] /// // v1.1 options. labscale is undocumented labprop scaling
[ fi(numlist max=3) ] // v1.2 options.
[ fi(numlist max=3) ] /// // v1.2 options
[ percent LABGap(string) ] // v1.3 options

marksample touse, strok

Expand All @@ -27,9 +29,7 @@ prog def treemap, sortpreserve
}





qui {
preserve
keep if `touse'
Expand Down Expand Up @@ -280,21 +280,38 @@ preserve

mata: xmin = 0; xmax = `xsize'; ymin = 0; ymax = `ysize'; dy = ymax - ymin; dx = xmax - xmin; myratio = `ratio'

*** calculate the percentage variable

*summ var0_v, meanonly
*gen double _percent = (var0_v / r(sum)) * 100

*** define format options
if "`format'" == "" {
if "`percent'" == "" {
local format %9.0fc
}
else {
local format %5.2f
}
}



**************
** layer0 **
**************


mata: data = select(st_data(., ("var0_v")), st_data(., "var0_t=1"))
mata: datasum = sum(data)
mata: padb = `pad0'; padt = `pad0'; padl = `pad0'; padr = `pad0'

mata: normlist = normdata(data, dx, dy); b0 = squarify(normlist, xmin, ymin, dx, dy, myratio), normlist; c0 = getcoords2(data, b0, padb, padt, padl, padr)
mata: normlist = normdata(data, dx, dy); b0 = squarify(normlist, xmin, ymin, dx, dy, myratio), normlist; c0 = getcoords2(data, b0, padb, padt, padl, padr, datasum)
mata: st_matrix("c0", c0)

local varlist

mat colnames c0 = "_l0_x" "_l0_y" "_l0_id" "_l0_val" "_l0_xmid" "_l0_ymid" "_l0_xmax" "_l0_ymax" "_l0_wgt"
mat colnames c0 = "_l0_x" "_l0_y" "_l0_id" "_l0_val" "_l0_xmid" "_l0_ymid" "_l0_xmax" "_l0_ymax" "_l0_wgt" "_l0_pct"

mat li c0
svmat c0, n(col)
Expand All @@ -308,9 +325,11 @@ preserve
summ id if var0_o==`i' & var0_t==1, meanonly
replace _l0_lab1 = `var0'[r(mean)] in `i'
}

gen _l0_lab0 = "{it:" + _l0_lab1 + " (" + string(_l0_val, "`format'") + ")}" in 1/`item0'
gen _l0_lab2 = string(_l0_val, "`format'") in 1/`item0'

local mylab cond("`percent'"=="", _l0_val, _l0_pct)

gen _l0_lab0 = "{it:" + _l0_lab1 + " (" + string(`mylab', "`format'") + ")}" in 1/`item0'
gen _l0_lab2 = string(`mylab', "`format'") in 1/`item0'



Expand All @@ -335,10 +354,10 @@ preserve
mata: padb = `pad1'; padt = `pad1'; padl = `pad1'; padr = `pad1'

mata: b1_`z' = processchildren(`z', mydata[.,1], b0 , padb, padt, padl, padr, myratio)
mata: c1_`z' = getcoords2( mydata[.,1], b1_`z', padb, padt, padl, padr)
mata: c1_`z' = getcoords2( mydata[.,1], b1_`z', padb, padt, padl, padr, datasum)
mata: st_matrix("c1_`z'", c1_`z')

mat colnames c1_`z' = "_l1_`z'_x" "_l1_`z'_y" "_l1_`z'_id" "_l1_`z'_val" "_l1_`z'_xmid" "_l1_`z'_ymid" "_l1_`z'_xmax" "_l1_`z'_ymax" "_l1_`z'_wgt"
mat colnames c1_`z' = "_l1_`z'_x" "_l1_`z'_y" "_l1_`z'_id" "_l1_`z'_val" "_l1_`z'_xmid" "_l1_`z'_ymid" "_l1_`z'_xmax" "_l1_`z'_ymax" "_l1_`z'_wgt" "_l1_`z'_pct"
svmat c1_`z', n(col)

gen _l1_`z'_lab1 = ""
Expand All @@ -350,8 +369,10 @@ preserve
replace _l1_`z'_lab1 = `var1'[r(mean)] in `i'
}

gen _l1_`z'_lab0 = "{it:" + _l1_`z'_lab1 + " (" + string(_l1_`z'_val, "`format'") + ")}" in 1/`item1'
gen _l1_`z'_lab2 = string(_l1_`z'_val, "`format'") in 1/`item1'
local mylab cond("`percent'"=="", _l1_`z'_val, _l1_`z'_pct)

gen _l1_`z'_lab0 = "{it:" + _l1_`z'_lab1 + " (" + string(`mylab', "`format'") + ")}" in 1/`item1'
gen _l1_`z'_lab2 = string(`mylab', "`format'") in 1/`item1'

}

Expand Down Expand Up @@ -384,10 +405,10 @@ preserve
mata: mydata = select(mydata, mydata[.,2] :== `z' :& mydata[.,3] :== `y')
mata: padb = `pad2'; padt = `pad2'; padl = `pad2'; padr = `pad2'
mata: b2_`z'_`y' = processchildren(`y', mydata[.,1], b1_`z', padb, padt, padl, padr, myratio)
mata: c2_`z'_`y' = getcoords2(mydata[.,1], b2_`z'_`y', padb, padt, padl, padr)
mata: c2_`z'_`y' = getcoords2(mydata[.,1], b2_`z'_`y', padb, padt, padl, padr, datasum)
mata: st_matrix("c2_`z'_`y'", c2_`z'_`y')

mat colnames c2_`z'_`y' = "_l2_`z'_`y'_x" "_l2_`z'_`y'_y" "_l2_`z'_`y'_id" "_l2_`z'_`y'_val" "_l2_`z'_`y'_xmid" "_l2_`z'_`y'_ymid" "_l2_`z'_`y'_xmax" "_l2_`z'_`y'_ymax" "_l2_`z'_`y'_wgt"
mat colnames c2_`z'_`y' = "_l2_`z'_`y'_x" "_l2_`z'_`y'_y" "_l2_`z'_`y'_id" "_l2_`z'_`y'_val" "_l2_`z'_`y'_xmid" "_l2_`z'_`y'_ymid" "_l2_`z'_`y'_xmax" "_l2_`z'_`y'_ymax" "_l2_`z'_`y'_wgt" "_l2_`z'_`y'_pct"
svmat c2_`z'_`y', n(col)

// get the labels
Expand All @@ -400,19 +421,24 @@ preserve
replace _l2_`z'_`y'_lab1 = `var2'[r(mean)] in `i'
}

gen _l2_`z'_`y'_lab0 = "{it:" + _l2_`z'_`y'_lab1 + " (" + string(_l2_`z'_`y'_val, "`format'") + ")}" in 1/`item2' if _l2_`z'_`y'_val > `labcond'
gen _l2_`z'_`y'_lab2 = string(_l2_`z'_`y'_val, "`format'") in 1/`item2' if _l2_`z'_`y'_val > `labcond'
local mylab cond("`percent'"=="", _l2_`z'_`y'_val, _l2_`z'_`y'_pct)

gen _l2_`z'_`y'_lab0 = "{it:" + _l2_`z'_`y'_lab1 + " (" + string(`mylab', "`format'") + ")}" in 1/`item2' if _l2_`z'_`y'_val > `labcond'
gen _l2_`z'_`y'_lab2 = string(`mylab', "`format'") in 1/`item2' if _l2_`z'_`y'_val > `labcond'
}
}
}






**************
// draw //
**************

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


if "`palette'" == "" {
local palette tableau
Expand All @@ -423,6 +449,7 @@ preserve
local poptions `3'
}

if "`labgap'" == "" local labgap 0.6

***************
*** layer 0 ***
Expand Down Expand Up @@ -455,7 +482,7 @@ preserve

local lab0 `lab0' (scatter _l0_ymid _l0_xmid in `i' if _l0_val > `labcond', mc(none) mlab(_l0_lab1) mlabpos(0) mlabsize(`labs0') mlabc(black) ) ||

if "`novalues'" == "" local lab0 `lab0' (scatter _l0_ymid _l0_xmid in `i' if _l0_val > `labcond', mc(none) mlab(_l0_lab2) mlabpos(6) mlabsize(`labs0') mlabgap(*0.1) mlabc(black) ) ||
if "`novalues'" == "" local lab0 `lab0' (scatter _l0_ymid _l0_xmid in `i' if _l0_val > `labcond', mc(none) mlab(_l0_lab2) mlabpos(6) mlabsize(`labs0') mlabgap(`labgap') mlabc(black) ) ||

***************
*** layer 1 ***
Expand Down Expand Up @@ -496,7 +523,7 @@ preserve

local lab1 `lab1' (scatter _l1_`i'_ymid _l1_`i'_xmid in `j' if _l1_`i'_val > `labcond', mc(none) mlab(_l1_`i'_lab1) mlabpos(0) mlabsize(`labs1') mlabc(black) ) ||

if "`novalues'" == "" local lab1 `lab1' (scatter _l1_`i'_ymid _l1_`i'_xmid in `j' if _l1_`i'_val > `labcond', mc(none) mlab(_l1_`i'_lab2) mlabpos(6) mlabsize(`labs1') mlabgap(*0.05) mlabc(black) ) ||
if "`novalues'" == "" local lab1 `lab1' (scatter _l1_`i'_ymid _l1_`i'_xmid in `j' if _l1_`i'_val > `labcond', mc(none) mlab(_l1_`i'_lab2) mlabpos(6) mlabsize(`labs1') mlabgap(`labgap') mlabc(black) ) ||


***************
Expand Down Expand Up @@ -529,7 +556,7 @@ preserve

local lab2 `lab2' (scatter _l2_`i'_`j'_ymid _l2_`i'_`j'_xmid in `k' , mc(none) mlab(_l2_`i'_`j'_lab1) mlabpos(0) mlabsize(`labs2') mlabc(black) ) ||

if "`novalues'" == "" local lab2 `lab2' (scatter _l2_`i'_`j'_ymid _l2_`i'_`j'_xmid in `k', mc(none) mlab(_l2_`i'_`j'_lab2) mlabpos(6) mlabsize(`labs2') mlabgap(*0.05) mlabc(black) ) ||
if "`novalues'" == "" local lab2 `lab2' (scatter _l2_`i'_`j'_ymid _l2_`i'_`j'_xmid in `k', mc(none) mlab(_l2_`i'_`j'_lab2) mlabpos(6) mlabsize(`labs2') mlabgap(`labgap') mlabc(black) ) ||

}
}
Expand Down Expand Up @@ -836,9 +863,9 @@ end
cap mata mata drop getcoords2()

mata:
real matrix getcoords2(data, b2, padb, padt, padl, padr) // data, bounds, padding
real matrix getcoords2(data, b2, padb, padt, padl, padr, mysum) // data, bounds, padding
{
coords = J(5 * rows(b2), 9, .) // rows = 4x coordinates + 1 empty, 8 cols = x,y,index,value, xmean, ymean, xmax, ymax, weight
coords = J(5 * rows(b2), 10, .) // rows = 4x coordinates + 1 empty, 8 cols = x,y,index, value, xmean, ymean, xmax, ymax, weight, percentage // add percentage here
for (i=1; i<= rows(b2); i++) {
Expand All @@ -853,6 +880,7 @@ mata:
coords[i, 7] = min(select(coords[.,1], coords[.,3] :== i))
coords[i, 8] = max(select(coords[.,2], coords[.,3] :== i))
coords[i, 9] = b2[i,5]
coords[i,10] = (data[i] / mysum) * 100
}
return (coords)
Expand Down
6 changes: 3 additions & 3 deletions installation/treemap.pkg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v 1.21
v 1.3
d {bf:TREEMAP}: A Stata package for treemaps
d
d Requires: Stata version 15 or higher.
Expand All @@ -8,9 +8,9 @@ d KW: graphs
d KW: treemap
d KW: squarify
d
d Distribution-Date: 20221122
d Distribution-Date: 20221214
d
d This version: 22 Nov 2022
d This version: 14 Dec 2022
d First version: 08 Sep 2022
d License: MIT
d
Expand Down
Loading

0 comments on commit 093dd7d

Please sign in to comment.