Skip to content

Commit

Permalink
v1.3 update
Browse files Browse the repository at this point in the history
  • Loading branch information
asjadnaqvi committed May 25, 2022
1 parent e33e194 commit 80d57f4
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 73 deletions.
50 changes: 32 additions & 18 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-bimap) ![license](https://img.shields.io/github/license/asjadnaqvi/stata-bimap) ![Stars](https://img.shields.io/github/stars/asjadnaqvi/stata-bimap) ![version](https://img.shields.io/github/v/release/asjadnaqvi/stata-bimap) ![release](https://img.shields.io/github/release-date/asjadnaqvi/stata-bimap)

# bimap v1.2
# bimap v1.3

This package provides the ability to draw bi-variate maps in Stata. It is based on the [Bi-variate maps Guide](https://medium.com/the-stata-guide/stata-graphs-bi-variate-maps-b1e96dd4c2be) that I released in December 2021.

Expand All @@ -10,12 +10,12 @@ This package provides the ability to draw bi-variate maps in Stata. It is based

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 package can be installed from SSC (**v1.0**):
The package can be installed from SSC (**v1.2**):
```
ssc install bimap, replace
```

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

```
net install bimap, from("https://raw.githubusercontent.com/asjadnaqvi/stata-bimap/main/installation/") replace
Expand Down Expand Up @@ -53,12 +53,12 @@ This command is a wrapper for `spmap` and assumes that you have shapefiles in St

## Syntax

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

```
bimap vary varx [if] [in], cut(option) palette(option)
[ count values ocolor(str) osize(str) ndocolor(str) ndsize(str) ndocolor(str)
[ count percent values ocolor(str) osize(str) ndocolor(str) ndsize(str) ndocolor(str)
polygon(str) line(str) point(str) label(str)
textx(string) texty(str) TEXTLABSize(num) TEXTSize(num) BOXsize(num) xscale(num) yscale(num)
title(str) subtitle(str) note(str) name(srt) scheme(str) ]
Expand Down Expand Up @@ -151,20 +151,32 @@ bimap share_hisp share_afam using usa_county_shp_clean, cut(equal) palette(pinkg
<img src="/figures/bimap2_2.png" height="600">


### Palettes

```
bimap share_hisp share_afam using usa_county_shp_clean, cut(pctile) palette(purpleyellow) ///
title("My first bivariate map") subtitle("Made with Stata") note("Data from US Census")
local i = 1
foreach x in pinkgreen bluered greenblue purpleyellow yellowblue orangeblue brew1 brew2 brew3 census {
bimap share_hisp share_afam using usa_county_shp_clean, cut(pctile) palette(`x') percent title("Scheme: `x'")
graph export bimap3_`i'.png, replace wid(2000)
local i = `i' + 1
}
```

<img src="/figures/bimap3.png" height="600">
<img src="/figures/bimap3_1.png" height="200"><img src="/figures/bimap3_2.png" height="200"><img src="/figures/bimap3_3.png" height="200">
<img src="/figures/bimap3_4.png" height="200"><img src="/figures/bimap3_5.png" height="200"><img src="/figures/bimap3_6.png" height="200">
<img src="/figures/bimap3_7.png" height="200"><img src="/figures/bimap3_8.png" height="200"><img src="/figures/bimap3_9.png" height="200">
<img src="/figures/bimap3_10.png" height="200">


### Advanced examples

```
bimap share_asian share_afam using usa_county_shp_clean, cut(pctile) palette(bluered) ///
title("{fontface Arial Bold:My first bivariate map}") subtitle("Made with Stata") note("Data from the US Census Bureau.") ///
textx("Share of African Americans") texty("Share of Asians") texts(3.5) textlabs(3) values count ///
ocolor() osize(none) ///
polygon(data("usa_state_shp_clean") ocolor(white) osize(0.3))
textx("Share of African Americans") texty("Share of Asians") texts(3.5) textlabs(3) values count ///
ocolor() osize(none) ///
polygon(data("usa_state_shp_clean") ocolor(white) osize(0.3))
```

<img src="/figures/bimap4.png" height="600">
Expand Down Expand Up @@ -193,18 +205,16 @@ bimap share_asian share_hisp using usa_county_shp_clean, cut(pctile) palette(or



### Adding other layers to the bimap

Since `bimap` is a wrapper of `spmap`, we can pass information for other layers as well including dots. Below we use the file we saved in the first step to plot the population of counties:

```
bimap share_hisp share_afam using usa_county_shp_clean, cut(pctile) palette(pinkgreen) ///
bimap share_hisp share_afam using usa_county_shp_clean, cut(pctile) palette(pinkgreen) percent ///
title("{fontface Arial Bold:My first bivariate map}") subtitle("Made with Stata") ///
note("Data from the US Census Bureau. Counties with population > 100k plotted as proportional dots.", size(1.8)) ///
textx("Share of African Americans") texty("Share of Hispanics") texts(3.5) textlabs(3) values count ///
ocolor() osize(none) ///
textx("Share of African Americans") texty("Share of Hispanics") texts(3.5) textlabs(3) values ///
osize(none) ///
polygon(data("usa_state_shp_clean") ocolor(white) osize(0.3)) ///
point(data("usa_county2") x(_CX) y(_CY) select(keep if tot_pop>100000) proportional(tot_pop) psize(absolute) fcolor(lime%85) ocolor(black) osize(0.12) size(0.9) )
point(data("usa_county2") x(_CX) y(_CY) select(keep if tot_pop>100000) proportional(tot_pop) psize(absolute) fcolor(lime%85) ocolor(black) osize(0.12) size(0.9))
```

<img src="/figures/bimap8.png" height="600">
Expand All @@ -216,6 +226,11 @@ Please open an [issue](https://github.com/asjadnaqvi/stata-bimap/issues) to repo

## Versions

**v1.3 (26 May 2022)**
- Percent option added to legend to show box share (thanks to Kit Baum).
- Legend corner label made lighter for visibility.
- Four special use palettes added: `brew1`, `brew2`, `brew3`, `brew4`, `census`.

**v1.2 (29 Apr 2022)**
- Fixed a bug in cut-off groupings (thanks to Ruth Watkinson).
- Error in how cut-off values are collected is fixed.
Expand All @@ -228,7 +243,6 @@ Please open an [issue](https://github.com/asjadnaqvi/stata-bimap/issues) to repo
- Errors in ado file corrected.
- Help file was missing a couple of options.


**v1.0 (08 Apr 2022)**
- Public release.

Expand Down
Binary file added figures/bimap2_1_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/bimap3_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/bimap3_10.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/bimap3_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 added figures/bimap3_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 added figures/bimap3_4.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/bimap3_5.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/bimap3_6.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/bimap3_7.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/bimap3_8.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/bimap3_9.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/bimap5.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/bimap8.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 removed figures/bimap_banner.png
Binary file not shown.
Binary file removed figures/bimap_banner2.png
Binary file not shown.
128 changes: 84 additions & 44 deletions installation/bimap.ado
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
*! Bimap Naqvi v1.2 05 May 2022. category cut-offs, counts, error checks, bug fixes, new palettes
* v1.1 14 Apr 2022. Stable release
*! bimap v1.3
*! Asjad Naqvi ([email protected])
*! 26 May 2022: added percent option. Color range fixes. New schemes. label fixes

* v1.2: 05 May 2022. Category cut-offs, counts, error checks, bug fixes, new palettes
* v1.1: 14 Apr 2022. Stable release

**********************************
* Step-by-step guide on Medium *
Expand All @@ -20,7 +24,7 @@ version 15

syntax varlist(min=2 max=2 numeric) [if] [in] using/ , ///
cut(string) palette(string) ///
[ count BOXsize(real 8) textx(string) texty(string) xscale(real 30) yscale(real 100) TEXTLABSize(real 2) TEXTSize(real 2.5) values ] ///
[ count percent BOXsize(real 8) textx(string) texty(string) xscale(real 30) yscale(real 100) TEXTLABSize(real 2) TEXTSize(real 2.5) values ] ///
[ polygon(passthru) line(passthru) point(passthru) label(passthru) ] ///
[ ocolor(string) osize(string) ] ///
[ ndocolor(string) ndsize(string) ndfcolor(string) ] ///
Expand Down Expand Up @@ -101,15 +105,10 @@ qui {
}


*fillin `cat_`var1'' `cat_`var2'' // rectangularize for correct assignment with group

sort `cat_`var1'' `cat_`var2''

tempvar grp_cut
*egen `grp_cut' = group(`cat_`var1'' `cat_`var2'')

*drop if _fillin==1 // drop the groups we don't actually have
*drop _fillin

gen `grp_cut' = .

Expand Down Expand Up @@ -154,6 +153,12 @@ qui {

// grp order: 1 = 1 1, 2 = 1 2, 3 = 1 3, 4 = 2 1, 5 = 2 2, 6 = 2 3, 7 = 3 1, 8 = 3 2, 9 = 3 3

if "`count'" != "" & "`percent'" != "" {
di as error "Please specify either {it:count} or {it:percent} option."
exit
}


if "`count'" != "" {

forval i = 1/3 {
Expand All @@ -164,12 +169,27 @@ qui {
}
}



if "`percent'" != "" {

count if `cat_`var1''!=. & `cat_`var2''!=.
local grsum = `r(N)'

forval i = 1/3 {
forval j = 1/3 {
count if `cat_`var1''==`j' & `cat_`var2''==`i'
local grsize`i'`j' = (`r(N)' / `grsum') * 100
local grsize`i'`j' : di %3.1f `grsize`i'`j''
}
}


}

// from spmap

if "`palette'" != "" {
local LIST "pinkgreen bluered greenblue purpleyellow yellowblue orangeblue"
local LIST "pinkgreen bluered greenblue purpleyellow yellowblue orangeblue brew1 brew2 brew3 census"
local LEN = length("`palette'")
local check = 0
foreach z of local LIST {
Expand All @@ -179,7 +199,7 @@ qui {
}

if !`check' {
di in yellow "Wrong palette specified. The supported palettes are {ul:pinkgreen}, {ul:bluered}, {ul:greenblue}, {ul:purpleyellow}, {ul:yellowblue}, {ul:orangeblue}."
di in yellow "Wrong palette specified. The supported palettes are {ul:pinkgreen}, {ul:bluered}, {ul:greenblue}, {ul:purpleyellow}, {ul:yellowblue}, {ul:orangeblue}, {ul:brew1}, {ul:brew2}, {ul:brew3}, {ul:census}."
exit 198
}
}
Expand All @@ -206,7 +226,24 @@ qui {

if "`palette'" == "orangeblue" { // from ArcGIS
local color #fef1e4 #97d0e7 #18aee5 #fab186 #b0988c #407b8f #f3742d #ab5f37 #5c473d
}
}

if "`palette'" == "brew1" {
local color #f37300 #fe9aa6 #f0047f #cce88b #e6e6e6 #cd9acc #008837 #9ac9d5 #5a4da4
}

if "`palette'" == "brew2" {
local color #c3b3d8 #7b67ab #240d5e #e6e6e6 #bfbfbf #7f7f7f #ffcc80 #f35926 #b30000
}

if "`palette'" == "brew3" {
local color #cce8d7 #80c39b #008837 #cedced #85a8d0 #0a50a1 #fbb4d9 #f668b3 #d60066
}

if "`palette'" == "census" {
local color #fffdef #e6f1df #d2e4f6 #fef3a9 #bedebc #a1c8ea #efd100 #4eb87b #007fc4
}


if "`polygon'" == "" {
local polyadd
Expand Down Expand Up @@ -248,7 +285,7 @@ qui {
egen x = seq(), t(3)


if "`count'" != "" {
if "`count'" != "" | "`percent'" != "" {
gen mycount = .

local x = 1
Expand All @@ -258,20 +295,13 @@ qui {
local x = `x' + 1
}
}


*forval i = 1/9 {
* replace mycount = `grsize`i'' in `i'
*}


local marksym mycount

}


cap drop spike*


if "`textx'" == "" {
local labx = "`var1'"
}
Expand All @@ -288,27 +318,27 @@ qui {


// arrows
gen spike1_x1 = 0.35 in 1
gen spike1_x2 = 3.6 in 1
gen spike1_y1 = 0.35 in 1
gen spike1_y2 = 0.35 in 1
gen spike1_m = "`labx'" in 1
gen spike1_x1 = 0.35 in 1
gen spike1_x2 = 3.6 in 1
gen spike1_y1 = 0.35 in 1
gen spike1_y2 = 0.35 in 1
gen spike1_m = "`labx'" in 1

gen spike2_y1 = 0.35 in 1
gen spike2_y2 = 3.6 in 1
gen spike2_x1 = 0.35 in 1
gen spike2_x2 = 0.35 in 1
gen spike2_m = "`laby'" in 1
gen spike2_y1 = 0.35 in 1
gen spike2_y2 = 3.6 in 1
gen spike2_x1 = 0.35 in 1
gen spike2_x2 = 0.35 in 1
gen spike2_m = "`laby'" in 1

// ticks
gen xvalx = .
gen xvaly = .
gen xvaln = .

replace xvaly = 0.36 in 1/3
replace xvalx = 0.8 in 1
replace xvalx = 1.8 in 2
replace xvalx = 2.8 in 3
replace xvaly = 0.36 in 1/3
replace xvalx = 0.8 in 1
replace xvalx = 1.8 in 2
replace xvalx = 2.8 in 3

replace xvaln = `var11' in 1
replace xvaln = `var12' in 2
Expand All @@ -319,10 +349,10 @@ qui {
gen yvaly = .
gen yvaln = .

replace yvalx = 0.33 in 1/3
replace yvaly = 1.1 in 1
replace yvaly = 2.1 in 2
replace yvaly = 3.1 in 3
replace yvalx = 0.33 in 1/3
replace yvaly = 1.1 in 1
replace yvaly = 2.1 in 2
replace yvaly = 3.1 in 3

replace yvaln = `var21' in 1
replace yvaln = `var22' in 2
Expand All @@ -335,12 +365,12 @@ qui {
gen laby = .
gen labn = ""

replace labx = 2 in 1
replace laby = 0 in 1
replace labx = 2 in 1
replace laby = 0 in 1
replace labn = "`labx'" in 1

replace labx = 0 in 2
replace laby = 3 in 2
replace labx = 0 in 2
replace laby = 3 in 2
replace labn = "`laby'" in 2


Expand Down Expand Up @@ -369,8 +399,18 @@ qui {

foreach x of local xlvl {
foreach y of local ylvl {
local boxes `boxes' (scatter y x if x==`x' & y==`y', mlab("`marksym'") mlabpos(0) msymbol(square) msize(`boxsize') mc("`color`x'`y''")) ///

if (`x'==3 & `y'==3) { // | (`x'==3 & `y'==2) | (`x'==2 & `y'==3)
local boxes `boxes' (scatter y x if x==`x' & y==`y', mlab("`marksym'") mlabpos(0) mlabc(gs13) msymbol(square) msize(`boxsize') mc("`color`x'`y''")) ///

}

else {
local boxes `boxes' (scatter y x if x==`x' & y==`y', mlab("`marksym'") mlabpos(0) mlabc(black) msymbol(square) msize(`boxsize') mc("`color`x'`y''")) ///

}



}
}
Expand Down
4 changes: 2 additions & 2 deletions installation/bimap.pkg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v 1.2
v 1.3
d {bf:BIMAP}: Bi-variate maps
d
d
Expand All @@ -9,7 +9,7 @@ d KW: graphs
d KW: map
d KW: bivariate map
d
d This version: 05 May 2022
d This version: 26 May 2022
d First version: 08 Apr 2022
d License: MIT
d
Expand Down
Loading

0 comments on commit 80d57f4

Please sign in to comment.