Heat (and hexagon) plots in Stata
Ben Jann
University of Bern, ben.jann@soz.unibe.ch
2019 German Stata Users Group meeting Munich, May 24, 2019
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 1
Heat (and hexagon) plots in Stata Ben Jann University of Bern, - - PowerPoint PPT Presentation
Heat (and hexagon) plots in Stata Ben Jann University of Bern, ben.jann@soz.unibe.ch 2019 German Stata Users Group meeting Munich, May 24, 2019 Ben Jann (University of Bern) heatplot Munich, 24.05.2019 1 Outline Introduction 1 Syntax of
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 1
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 2
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 3
. quietly drawnorm y x, n(10000) corr(1 .5 1) cstorage(lower) clear . heatplot y x, backfill colors(plasma)
.84893 .78679 .72464 .6625 .60036 .53821 .47607 .41393 .35179 .28964 .2275 .16536 .10321 .04107
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 4
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 5
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 6
1Lewin-Koh, N. (2018). Hexagon Binning: an Overview. Available from
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 7
. hexplot y x, backfill colors(plasma)
.8875 .8225 .7575 .6925 .6275 .5625 .4975 .4325 .3675 .3025 .2375 .1725 .1075 .0425
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 8
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 16
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 17
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 18
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 19
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 20
. webuse nhanes2, clear . heatplot weight height 50 100 150 200 weight (kg) 140 160 180 200 height (cm)
.86884 .80958 .75033 .69108 .63182 .57257 .51332 .45406 .39481 .33556 .2763 .21705 .15779 .09854 .03929
percent
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 21
. heatplot weight height, xbins(20) ybwidth(10 30) 50 100 150 200 weight (kg) 140 160 180 200 height (cm)
4.2682 3.9745 3.6808 3.3871 3.0934 2.7997 2.506 2.2123 1.9187 1.625 1.3313 1.0376 .74389 .4502 .15651
percent
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 22
. heatplot weight height, statistic(count) color(plasma, reverse) /// > cut(1(5)@max) keylabels(, range(1)) 50 100 150 200 weight (kg) 140 160 180 200 height (cm)
91-93 86-90 81-85 76-80 71-75 66-70 61-65 56-60 51-55 46-50 41-45 36-40 31-35 26-30 21-25 16-20 11-15 6-10 1-5
count
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 23
. hexplot weight height, statistic(count) color(plasma, reverse) /// > cut(1(5)@max) keylabels(, range(1)) 50 100 150 200 weight (kg) 140 160 180 200 height (cm)
96-98 91-95 86-90 81-85 76-80 71-75 66-70 61-65 56-60 51-55 46-50 41-45 36-40 31-35 26-30 21-25 16-20 11-15 6-10 1-5
count
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 24
. hexplot weight height, statistic(count) color(plasma) /// > cut(1(5)@max) keylabels(, range(1)) size 50 100 150 200 weight (kg) 140 160 180 200 height (cm)
96-98 91-95 86-90 81-85 76-80 71-75 66-70 61-65 56-60 51-55 46-50 41-45 36-40 31-35 26-30 21-25 16-20 11-15 6-10 1-5
count
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 25
. heatplot weight height, statistic(count) color(plasma) /// > cut(1(5)@max) keylabels(, range(1)) size 50 100 150 200 weight (kg) 140 160 180 200 height (cm)
91-93 86-90 81-85 76-80 71-75 66-70 61-65 56-60 51-55 46-50 41-45 36-40 31-35 26-30 21-25 16-20 11-15 6-10 1-5
count
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 26
. hexplot weight height, statistic(count) color(plasma) /// > cut(1(5)@max) keylabels(, range(1)) size /// > addplot(lpolyci weight height, degree(1) psty(p2) lw(*1.5) ac(%50) alc(%0)) 50 100 150 200 weight (kg) 140 160 180 200 height (cm)
96-98 91-95 86-90 81-85 76-80 71-75 66-70 61-65 56-60 51-55 46-50 41-45 36-40 31-35 26-30 21-25 16-20 11-15 6-10 1-5
count
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 27
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 28
. webuse nhanes2, clear . hexplot female weight height, color(PiYG) ylabel(25(25)175) cuts(0(.05)1) 25 50 75 100 125 150 175 weight (kg) 140 160 180 200 height (cm)
.975 .925 .875 .825 .775 .725 .675 .625 .575 .525 .475 .425 .375 .325 .275 .225 .175 .125 .075 .025
female
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 29
. hexplot female weight height, color(PiYG) ylabel(25(25)175) cuts(0(.05)1) /// > sizeprop recenter p(lcolor(black) lwidth(vthin) lalign(center)) 25 50 75 100 125 150 175 weight (kg) 140 160 180 200 height (cm)
.975 .925 .875 .825 .775 .725 .675 .625 .575 .525 .475 .425 .375 .325 .275 .225 .175 .125 .075 .025
female
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 30
. heatplot highbp bmi i.female, xdiscrete(0.9) yline(18.5 25) cuts(0(.05).75) /// > sizeprop recenter colors(inferno) plotregion(color(gs11)) ylabel(, nogrid) 10 20 30 40 50 60 Body Mass Index (BMI) 1 1=female, 0=male
.725 .675 .625 .575 .525 .475 .425 .375 .325 .275 .225 .175 .125 .075 .025
highbp
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 31
. sysuse surface, clear (NOAA Sea Surface Temperature) . heatplot temperature longitude latitude, discrete(.5) statistic(asis) /// > by(date, legend(off)) ylabel(30(1)38) aspectratio(1)
30 31 32 33 34 35 36 37 38 142 144 146 148 150 142 144 146 148 150
01mar2011 11mar2011 30N to 38.5N 142E to 150E
Graphs by date Ben Jann (University of Bern) heatplot Munich, 24.05.2019 32
. hexplot temperature longitude latitude, discrete(.5) statistic(asis) /// > by(date, legend(off)) ylabel(30(1)38) aspectratio(1)
30 31 32 33 34 35 36 37 38 142 144 146 148 150 142 144 146 148 150
01mar2011 11mar2011 30N to 38.5N 142E to 150E
Graphs by date Ben Jann (University of Bern) heatplot Munich, 24.05.2019 33
. hexplot temperature longitude latitude, discrete(.5) statistic(asis) clip /// > by(date, legend(off)) ylabel(30(1)38) aspectratio(1)
30 31 32 33 34 35 36 37 38 142 144 146 148 150 142 144 146 148 150
01mar2011 11mar2011 30N to 38.5N 142E to 150E
Graphs by date Ben Jann (University of Bern) heatplot Munich, 24.05.2019 34
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 35
. quietly sysuse auto, clear . hexplot price weight mpg, values(format(%9.0f)) legend(off) aspectratio(1) /// > colors(plasma, intensity(.6)) p(lc(black) lalign(center))
5147 4294 4425 4296 5837 3876 3866 4194 5397 7103 4647 5651 11995 4976 4569 4647 12990 4888 9298 8814 11385 15906 12546
1000 2000 3000 4000 5000 Weight (lbs.) 10 20 30 40 Mileage (mpg)
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 36
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 37
. quietly sysuse auto, clear . quietly correlate price mpg trunk weight length turn foreign . matrix C = r(C) . heatplot C, values(format(%9.3f)) color(hcl, diverging intensity(.6)) /// > legend(off) aspectratio(1)
1.000
0.314 0.539 0.432 0.310 0.049
1.000
0.393 0.314
1.000 0.672 0.727 0.601
0.539
0.672 1.000 0.946 0.857
0.432
0.727 0.946 1.000 0.864
0.310
0.601 0.857 0.864 1.000
0.049 0.393
1.000
price mpg trunk weight length turn foreign price mpg trunk weight length turn foreign Ben Jann (University of Bern) heatplot Munich, 24.05.2019 38
. heatplot C, values(format(%9.3f)) color(hcl, diverging intensity(.6)) /// > legend(off) aspectratio(1) lower nodiagonal
0.314
0.539
0.672 0.432
0.727 0.946 0.310
0.601 0.857 0.864 0.049 0.393
mpg trunk weight length turn foreign price mpg trunk weight length turn
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 39
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 40
. copy http://www.stata-press.com/data/r15/homicide1990.dta . . copy http://www.stata-press.com/data/r15/homicide1990_shp.dta .
. use homicide1990 (S.Messner et al.(2000), U.S southern county homicide rates in 1990) . spmatrix create contiguity W . spmatrix matafromsp W id = W . mata mata describe W # bytes type name and extent 15,949,952 real matrix W[1412,1412]
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 41
. heatplot mata(W), drop(0) aspectratio(1) 500 1000 1500 Rows 500 1000 1500 Columns
22.732 21.175 19.617 18.06 16.503 14.945 13.388 11.831 10.273 8.7161 7.1587 5.6014 4.0441 2.4867 .92938
sum
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 42
. heatplot mata(W), drop(0) aspectratio(1) hexagon bins(100) 500 1000 1500 Rows 500 1000 1500 Columns
5.8325 5.4406 5.0488 4.657 4.2651 3.8733 3.4814 3.0896 2.6977 2.3059 1.914 1.5222 1.1303 .73848 .34663
sum
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 43
. heatplot mata(W), drop(0) aspectratio(1) discrete color(black) p(lalign(center)) 500 1000 1500 Rows 500 1000 1500 Columns
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 44
. heatplot mata(W), drop(0) aspectratio(1) discrete color(black) scatter p(ms(p)) 500 1000 1500 Rows 500 1000 1500 Columns
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 45
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 46
Ben Jann (University of Bern) heatplot Munich, 24.05.2019 47