使用 Stata 分析世界各国人为碳排放量数据( 1990~2020 年)

Climate Watch 网站上提供了一份 1990~2020 年世界各国人为 CO2 排放量面板数据:

该数据表明,碳排放排名前十的国家总碳排放超过全球的 60%,而碳排放最少的 100 个国家总碳排放不足全球的 3%,今天我们一起来用 Stata 分析下这份数据。

首先我们可以从网站上下载到 “ghg-emissions.csv” 数据,在 Stata 中读入:

import delimited using "ghg-emissions.csv", clear
*- ssc install nrow
*- 把第一行作为变量名
nrow 1
destring _1990, replace force
*- 删除没有数据的最后两行
drop in 194/195

*- 更改下部分国家的名字,方便与地图数据匹配
ren v1 country
replace country = "The Bahamas" if country == "Bahamas"
replace country = "Ivory Coast" if country == "Côte d'Ivoire"
replace country = "Swaziland" if country == "Eswatini"
replace country = "Guinea Bissau" if country == "Guinea-Bissau"
replace country = "Federated States of Micronesia" if country == "Micronesia"
replace country = "Republic of Serbia" if country == "Serbia"
replace country = "United Republic of Tanzania" if country == "Tanzania"
replace country = "East Timor" if country == "Timor-Leste"
replace country = "Turkey" if country == "Türkiye"
replace country = "United States of America" if country == "United States"

在之前的课程

×

中我们讲解过如何使用 Stata 绘制世界地图。下面我们先绘制下世界各国人为碳排放的分布地图:

把上述处理结果和地图数据合并:

merge 1:1 country using worldmap_db.dta

*- 由于 图瓦卢 在这份地图数据里面并没有,所以只能忽略了
drop if _m == 1

然后就可以绘图了:

*- 把缺失的替换成 -1000
replace _2020 = -1000 if missing(_2020)

sum _2020 if _2020 != -1000
_pctile _2020, n(8)
ret list

spmap _2020 using worldmap_coord, id(ID) ///
osize(vvthin ...) ocolor(white ...) ///
clmethod(custom) clbreaks(-1000 -0.9 0 0.33 7.86 24.25 ///
47.12 91.19 270.31 13000) ///
fcolor(gray "255 243 224" "255 224 178" "255 204 128" ///
"255 183 77" "255 167 38" "255 152 0" "251 140 0" "245 124 0") ///
graphr(margin(medium)) ///
polygon(data(worldmap_polygon) fcolor(black) ///
osize(vvthin)) ///
label(data(worldmap_label2) x(X) y(Y) ///
label(country_cn) length(60) size(*0.6)) ///
line(data(worldmap_line_coord.dta) size(vthin ...) ///
pattern(solid ...) ///
color(black ...)) ///
leg(order(2 "无数据" 3 "<0.33" 4 "0.33~7.86" ///
5 "7.86~24.25" 6 "24.25~47.12" ///
7 "47.12~91.19" 8 "91.19~270.31" 9 ">270.31") ///
ring(1) pos(6) row(1)) ///
ti("2020 年世界各国人为碳排放量") ///
subti("绘制:微信公众号 RStata") xsize(10) ysize(6)

gr export "pic2020.png", width(4800) replace

如果想绘制历年的,可以使用下面的循环:

*- 循环绘制每一年的
forval y = 1990/2019 {
*- 把缺失的替换成 -1000
replace _`y' = -1000 if missing(_`y')

spmap _`y' using worldmap_coord, id(ID) ///
osize(vvthin ...) ocolor(white ...) ///
clmethod(custom) clbreaks(-1000 -0.9 0 0.33 7.86 24.25 ///
47.12 91.19 270.31 13000) ///
fcolor(gray "255 243 224" "255 224 178" "255 204 128" ///
"255 183 77" "255 167 38" "255 152 0" "251 140 0" "245 124 0") ///
graphr(margin(medium)) ///
polygon(data(worldmap_polygon) fcolor(black) ///
osize(vvthin)) ///
label(data(worldmap_label2) x(X) y(Y) ///
label(country_cn) length(60) size(*0.6)) ///
line(data(worldmap_line_coord.dta) size(vthin ...) ///
pattern(solid ...) ///
color(black ...)) ///
leg(order(2 "无数据" 3 "<0.33" 4 "0.33~7.86" ///
5 "7.86~24.25" 6 "24.25~47.12" ///
7 "47.12~91.19" 8 "91.19~270.31" 9 ">270.31") ///
ring(1) pos(6) row(1)) ///
ti("`y' 年世界各国人为碳排放量") ///
subti("绘制:微信公众号 RStata") xsize(10) ysize(6)

gr export "pic`y'.png", width(4800) replace
}

为了方便后面的分析,我们再把这个数据转换成长数据:

*- 历年碳排放
*- ssc install tidy
gather _*
replace var = subinstr(var, "_", "", .)
drop if var == "merge"
destring var, replace
drop if value == -1000
keep country var val country_cn
ren var year
ren val co2
save "历年各国人为碳排放量", replace

我们可以关注下碳排放量最高的 8 个国家的碳排放变化趋势:

*- 筛选最后一年排放量最高的 8 个国家
use 历年各国人为碳排放量, clear
keep if year == 2020
gsort -co2
keep in 1/8
keep country
merge 1:m country using 历年各国人为碳排放量
keep if _m == 3
drop _m
gsort country year
tw ///
line co2 year if country == "China", lc("254 212 57") lp(solid) || ///
line co2 year if country == "United States of America", lc("112 154 225") lp(solid) || ///
line co2 year if country == "India", lc("138 145 151") lp(solid) || ///
line co2 year if country == "Russia", lc("210 175 129") lp(solid) || ///
line co2 year if country == "Indonesia", lc("253 116 70") lp(solid) || ///
line co2 year if country == "Brazil", lc("213 228 162") lp(solid) || ///
line co2 year if country == "Japan", lc("25 126 192") lp(solid) || ///
line co2 year if country == "Iran", lc("240 92 59") lp(solid) ||, ///
leg(order(1 "中国" 2 "美国" 3 "印度" 4 "俄罗斯" 5 "印度尼西亚" ///
6 "巴西" 7 "日本" 8 "伊朗")) ///
xla(1990(5)2020) xti("") ///
yti("CO2排放量(百万吨)") ///
ti("2020 年部分国家人为 CO2 排放量(百万吨)") ///
subti("数据处理 & 绘图:微信公众号 RStata") ///
caption("数据来源:Greenhouse Gas (GHG) Emissions | Climate Watch")

gr export "2020 年部分国家人为 CO2 排放量(百万吨).png", width(4800) replace

再分析下各个国家的占比:

use 历年各国人为碳排放量, clear
gsort -co2
drop if co2 == -1000
keep if year == 2020
egen total = total(co2)
keep in 1/10
egen total2 = total(co2)
gen other = total - total2
set obs 11
replace country = "Others" in 11
replace country_cn = "其他国家" in 11
replace year = 2020 in 11
replace co2 = other[1] in 11

*- 前 10 个国家的人为碳排放占了全球的 62.46%
gen ratio = (total2 / total) * 100

*- 绘制饼图
gen id = _n
sencode country_cn, gen(country2) gsort(id)
gen subratio = (co2 / total[1]) * 100
gr pie co2, over(country2) ///
plabel(1 "中国:26.62%") ///
plabel(2 "美国:11.45%") ///
plabel(3 "印度:6.85%") ///
plabel(4 "俄罗斯:3.89%") ///
plabel(5 "印尼:3.19%", gap(8)) ///
plabel(6 "巴西:3.18%", gap(5)) ///
plabel(7 "日本:2.30%", gap(-1)) ///
plabel(11 "其他:37.54%") ///
ti("世界各国 2020 年人为碳排放量占比分布图") ///
subti("数据处理 & 绘图:微信公众号 RStata") ///
caption("数据来源:Greenhouse Gas (GHG) Emissions | Climate Watch")

gr export "世界各国 2020 年人为碳排放量占比分布图.png", width(4800) replace

如果大家做全球范围的一些研究,或许会用到这份数据。

点击这里跳转到 RStata 短书平台获取附件:使用 Stata 分析世界各国人为碳排放量数据( 1990~2020 年)

评论