You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stata中spmap绘制地图时如何自定义缺失数据国家的颜色?

问题

我正在Stata中使用spmap(SSC)绘制世界分级统计图,以total_worker变量为依据为各国着色。使用的代码如下:

colorpalette viridis, n(12) nograph reverse
local colors `r(p)'
drop if country=="Antarctica"
spmap total_worker using world_coord, id(id) clbreaks(0 5001 20001 50001 150001 1000000 50000000) clmethod(custom) ///
legend(pos(7) size(2.8) region(fcolor(gs15)) label(2 "0 to 5000") label(3 "5001 to 20000")  label(4 "20001 to 50000" )  label(5 "50001 to 150000" ) label(6 "150001 to 1000000" ) label(7 "1000000+")) ///
fcolor("`colors'")  ocolor(white ..) osize(0.05 ..) title("2017", size(5))

其中world_coord是通过以下命令生成的坐标文件:

shp2dta using "world_countries_2020", database(world_db) coordinates(world_coord) genid(id) replace

world_countries_2020文件包含当前目录下的.dbf和.shp扩展名文件。当前代码生成的地图中,无数据的国家显示为白色,我希望将这些国家设置为灰色,且边框颜色与其他国家一致为白色,请问该如何实现?

解决方案

只需在spmap命令中添加missing()参数,并调整边框设置,即可实现需求:

修改后的完整代码

colorpalette viridis, n(12) nograph reverse
local colors `r(p)'
drop if country=="Antarctica"
spmap total_worker using world_coord, id(id) clbreaks(0 5001 20001 50001 150001 1000000 50000000) clmethod(custom) ///
legend(pos(7) size(2.8) region(fcolor(gs15)) label(2 "0 to 5000") label(3 "5001 to 20000")  label(4 "20001 to 50000" )  label(5 "50001 to 150000" ) label(6 "150001 to 1000000" ) label(7 "1000000+")) ///
fcolor("`colors'")  ocolor(white) osize(0.05) title("2017", size(5)) ///
missing(fcolor(gs12))

关键修改说明

  1. missing(fcolor(gs12)):指定无数据(total_worker为缺失值)的国家填充色为灰色。gs12是Stata内置灰度色值,可根据视觉需求调整为gs10(更深灰)或gs14(更浅灰)。
  2. 移除ocolor和osize后的..:原代码中..表示仅对有数据区域应用边框设置,移除后ocolor(white)和osize(0.05)会作用于所有国家,确保无数据国家的边框也为白色,宽度与其他国家一致。

内容的提问来源于stack exchange,提问作者Hosea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 10:17:42