. * clean up the boundaries . use usa_state_shp, clear . merge m:1 _ID using usa_state // to get the identifiers . destring STATEFP, replace . drop if inlist(STATEFP,2,15,60,66,69,72,78) . twoway (scatter _Y _X)
. colorpalette #e8e8e8 #dfb0d6 #be64ac #ace4e4 #a5add3 #8c62aa #5ac8c8 #5698b9 #3b4994, nograph . return list . local color11 `r(p1)' . local color12 `r(p2)' . local color13 `r(p3)' . local color21 `r(p4)' . local color22 `r(p5)' . local color23 `r(p6)' . local color31 `r(p7)' . local color32 `r(p8)' . local color33 `r(p9)' . twoway (scatter y x if x==1 & y==1, msymbol(square) msize(18) mc('`color11'')) /// > (scatter y x if x==2 & y==2, msymbol(square) msize(18) mc('`color22'')) /// > (scatter y x if x==3 & y==3, msymbol(square) msize(18) mc('`color33'')), /// > xlabel(0 4) ylabel(0 4) aspect(1) legend(off)
然后我们用循环的方法来快速填充其余的方格:
. * 填充颜色 . levelsof x, local(xlvl) . levelsof y, local(ylvl) . local boxes . foreach x of local xlvl { 2. foreach y of local ylvl { 3. local boxes `boxes' (scatter y x if x==`x' & y==`y', msymbol(square) /// > msize(19) mc('`color`x'`y''')) 4. } 5. } . cap drop spike* . gen spike1_x1 = 0.2 in 1 . gen spike1_x2 = 3.6 in 1 . gen spike1_y1 = 0.2 in 1 . gen spike1_y2 = 0.2 in 1 . gen spike1_m = 'More African Americans' . gen spike2_y1 = 0.2 in 1 . gen spike2_y2 = 3.2 in 1 . gen spike2_x1 = 0.2 in 1 . gen spike2_x2 = 0.2 in 1 . gen spike2_m = 'More Hispanics' . twoway `boxes' (pcarrow spike1_y1 spike1_x1 spike1_y2 spike1_x2, lcolor(gs12) lw(thin) /// > mcolor(gs12) mlabel(spike1_m) mlabpos(7) headlabel) (pcarrow spike2_y1 spike2_x1 /// > spike2_y2 spike2_x2, lcolor(gs12) lw(thin) mcolor(gs12) mlabel(spike2_m) mlabpos(10) /// > headlabel mlabangle(90) mlabgap(1.8)), xlabel(0 4) ylabel(0 4) aspect(1) legend(off)
接下来将这张图的尺寸缩小以匹配我们之前绘制的地图,同时删掉原来的坐标轴和格点:
. colorpalette #e8e8e8 #dfb0d6 #be64ac #ace4e4 #a5add3 #8c62aa #5ac8c8 #5698b9 #3b4994, nograph . return list . local color11 `r(p1)' . local color12 `r(p2)' . local color13 `r(p3)' . local color21 `r(p4)' . local color22 `r(p5)' . local color23 `r(p6)' . local color31 `r(p7)' . local color32 `r(p8)' . local color33 `r(p9)' . levelsof x, local(xlvl) . levelsof y, local(ylvl) . local boxes . foreach x of local xlvl { 2. foreach y of local ylvl { 3. local boxes `boxes' (scatter y x if x==`x' & y==`y', msymbol(square) msize(5) /// > mc('`color`x'`y''')) 4. } 5. } . twoway `boxes' (pcarrow spike1_y1 spike1_x1 spike1_y2 spike1_x2, lw(thin) lcolor(gs12) /// > mcolor(gs12) mlabel(spike1_m) mlabpos(7 ) msize(0.8) headlabel mlabsize(2)) /// > (pcarrow spike2_y1 spike2_x1 spike2_y2 spike2_x2, lw(thin) lcolor(gs12) mcolor(gs12) /// > mlabel(spike2_m) mlabpos(10) msize(0.8) headlabel mlabangle(90) mlabgap(1.8) /// > mlabsize(2)), xlabel(0 4, nogrid) ylabel(0 4, nogrid) aspectratio(1) xsize(1) ysize(1) /// > fxsize(20) fysize(100) legend(off) ytitle('') xtitle('') xscale(off) yscale(off) /// > name(bivar_legend, replace)
最后我们将地图和这张小图例合在一起:
. * 合并两张图 . graph combine bivar_map bivar_legend, imargin(zero) /// > title('{fontface Arial Bold:Share of African Americans and Hispanics in the USA}', size(4.5)) /// > note('Source of data, and county and state layers: United States Census Bureau.', size(2))
6. 中国地图
set scheme white_tableau // 设置绘图模板 cap spshape2dta 省级数据 /* 这条命令可以将 dbf 和 shp 格式的地图文件转为两个 dta 格式的文件,分别以省级数据.dta 和省级数据_shp.dta命名,其中后者省级数据_shp.dta为坐标数据,前者省级数据.dta为标签数据, 在绘制地图时,我们主要是通过标签数据中的 ID 变量连接 (合并) 地图数据和作图数据 */
// 使用总的数字金融普惠指数 collapse (mean) index_aggregate, by(prov_name) drop in 1 save 普惠金融指数.dta,replace ren prov_name name merge 1:1 name using '省级数据.dta' // drop in 32 // 由于北大普惠指数没有考虑香港、澳门、台湾,因此这些地区的数据缺失 drop _merge spmap index_aggregate using 省级数据_shp, id(_ID) fcolor(Blues) mocolor(black) mosize(0.1)
* 二维地图 import excel 北京大学数字普惠金融指数.xlsx, firstrow sheet(Provinces) clear collapse (mean) coverage_breadth usage_depth, by(prov_name) drop in 1 save 数字金融广度和深度.dta,replace ren prov_name name merge 1:1 name using '省级数据.dta'
// drop in 32 drop _merge xtile xtile_coverage = coverage_breadth, n(3) xtile xtile_usage = usage_depth, n(3) sort xtile_coverage xtile_usage cap drop xtile2 egen xtile2 = group(xtile_coverage xtile_usage)
// map with new color scheme colorpalette #c8b35a #b0d5df #64acbe #e4acac #ad9ea5 #627f8c #c85a5a #985356 #574249, nograph local colors `r(p)' spmap xtile2 using 省级数据_shp, id(_ID) clm(unique) fcolor('`colors'') /// ocolor(black ..) osize(0.02 ..) /// ndfcolor(gs14) ndocolor(gs6 ..) ndsize(0.03 ..) ndlabel('No data') /// polygon(data('省级数据_shp') ocolor(black) osize(0.12) ) /// legend(off) name(bivar_map2, replace) mosize(vvvthick)
// legend with new color scheme clear set obs 9 egen y = seq(), b(3) egen x = seq(), t(3) cap drop spike* gen spike1_x1 = 0.2 in 1 gen spike1_x2 = 3.6 in 1 gen spike1_y1 = 0.2 in 1 gen spike1_y2 = 0.2 in 1 gen spike1_m = '数字普惠广度更强' gen spike2_y1 = 0.2 in 1 gen spike2_y2 = 3.2 in 1 gen spike2_x1 = 0.2 in 1 gen spike2_x2 = 0.2 in 1 gen spike2_m = '数字普惠深度更强' colorpalette #c8b35a #b0d5df #64acbe #e4acac #ad9ea5 #627f8c #c85a5a #985356 #574249, nograph
local color11 `r(p1)' local color12 `r(p2)' local color13 `r(p3)' local color21 `r(p4)' local color22 `r(p5)' local color23 `r(p6)' local color31 `r(p7)' local color32 `r(p8)' local color33 `r(p9)'
levelsof x, local(xlvl) levelsof y, local(ylvl) local boxes foreach x of local xlvl { foreach y of local ylvl { local boxes `boxes' (scatter y x if x==`x' & y==`y', msymbol(square) /// msize(5) mc('`color`x'`y''')) } }