Showing posts with label wbopendata. Show all posts
Showing posts with label wbopendata. Show all posts

Apr 29, 2016

Random graphs (78): Line graphs using the -by- options

// Download data
wbopendata, clear long year(1990:2014) indicator(sh.xpd.publ.zs)
rename sh_xpd_publ_zs spending
rename iso2code country
label var spending "Health expenditure, public (% of GDP)"

// Group countries
gen lac = (inlist(country, "AR", "BS", "BB", "BZ", "BO", "BR", "CL") ///
        |  inlist(country, "CO", "CR", "CU", "DO", "EC", "SV", "GT") ///
        |  inlist(country, "GY", "HT", "HN", "JM", "MX", "NI", "PA") ///
        |  inlist(country, "PY", "PE", "SR", "TT", "UY", "VE"))
gen oecd = (inlist(country, "AT", "AU", "BE", "CA", "CZ", "DK", "EE") ///
         |  inlist(country, "FI", "FR", "DE", "HU", "IS", "IE", "IT") ///
         |  inlist(country, "JP", "LU", "NL", "NZ", "NO", "PL", "PT") ///
         |  inlist(country, "SK", "SI", "ES", "SE", "CH", "GB", "US"))
generate group = ""
replace  group = "OECD" if oecd
replace  group = "LAC" if lac

// Drop other countries and old data
drop if group == ""
drop if year < 1995

// Drop unnecessary variables
drop countrycode region regioncode
drop lac oecd

// Calculate average spending
sort group year country
*list, sepby(year)
bysort group year: egen avgspending = mean(spending)
*list, sepby(year)

// Calculate coefficient of variation
sort group year country
*list, sepby(year)
bysort group year: egen sdspending = sd(spending)
*list, sepby(year)
gen cvspending = (sdspending/avgspending) * 100
*list, sepby(year) abb(20)

// Calculate delta with respect to the country group
generate delta_group = (spending - avgspending) / sdspending
label var delta_group "Delta country group"

// Calculate delta with respect to the Netherlands
// Dutch spending per year
gen spending_nl = spending if country == "NL"
bysort year (spending_nl): replace spending_nl = spending_nl[_n-1] if missing(spending_nl)
// SD of Dutch spending
egen sdspending_nl = sd(spending) if country == "NL"
replace sdspending_nl = sdspending_nl[_n-1] if missing(sdspending_nl)
// Delta
generate delta_nl = (spending - spending_nl) / sdspending_nl

// Figure 5
capture drop figure5 
generate figure5 = .
replace  figure5 = 1 if country == "DK"
replace  figure5 = 2 if country == "NL"
replace  figure5 = 3 if country == "ES"

label define figure5 1 "Denmark" 2 "The Netherlands" 3 "Spain"
label value figure5 figure5 

twoway line delta_nl year, by(figure5, col(1) note("") ixaxes iytitle l1title("") ///
       title("Selected countries" "compared to The Netherlands")) ///
       yscale(range(-3 3)) ylabel(-3 (1) 3, grid) xlabel(1995 2000 2005 2010 2014, tick) ///
    l1title("") xsize(3) ysize(8) xtitle("") xmtick(1995/2014) name(figure5A, replace) ///
    yline(0) nodraw 
twoway line delta_group year, by(figure5, col(1) note("") ixaxes iytitle ///
       title("Selected countries" "compared to OECD average") l1title("")) ///
       yscale(range(-3 3)) ylabel(-3 (1) 3, grid) xlabel(1995 2000 2005 2010 2014, tick) ///
    l1title("") xsize(3) ysize(8) xtitle("") xmtick(1995/2014) name(figure5B, replace) ///
    yline(0) nodraw 
graph combine figure5A figure5B, col(2) name(figure5, replace)

Apr 28, 2016

Random graphs (77): Line plots

// Download data
wbopendata, clear long year(1990:2014) indicator(sh.xpd.publ.zs)
rename sh_xpd_publ_zs spending
rename iso2code country
label var spending "Health expenditure, public (% of GDP)"

// Group countries
gen lac = (inlist(country, "AR", "BS", "BB", "BZ", "BO", "BR", "CL") ///
        |  inlist(country, "CO", "CR", "CU", "DO", "EC", "SV", "GT") ///
        |  inlist(country, "GY", "HT", "HN", "JM", "MX", "NI", "PA") ///
        |  inlist(country, "PY", "PE", "SR", "TT", "UY", "VE"))
gen oecd = (inlist(country, "AT", "AU", "BE", "CA", "CZ", "DK", "EE") ///
         |  inlist(country, "FI", "FR", "DE", "HU", "IS", "IE", "IT") ///
         |  inlist(country, "JP", "LU", "NL", "NZ", "NO", "PL", "PT") ///
         |  inlist(country, "SK", "SI", "ES", "SE", "CH", "GB", "US"))
generate group = ""
replace  group = "OECD" if oecd
replace  group = "LAC" if lac

// Drop other countries and old data
drop if group == ""
drop if year < 1995

// Drop unnecessary variables
drop countrycode region regioncode
drop lac oecd

// Calculate average spending
sort group year country
*list, sepby(year)
bysort group year: egen avgspending = mean(spending)
*list, sepby(year)

// Calculate coefficient of variation
sort group year country
*list, sepby(year)
bysort group year: egen sdspending = sd(spending)
*list, sepby(year)
gen cvspending = (sdspending/avgspending) * 100
*list, sepby(year) abb(20)

// Figure 1a
sort group year
twoway (line avgspending year if group == "OECD") ///
       (line avgspending year if group == "LAC") ///
    (line spending year if country == "NL"), ///
    legend(order(1 "OECD regional average" 3 "The Netherlands" 2 "LAC regional average") title("") ring(0)) ///
    yscale(range(0 10)) ylabel(0 (1) 10, grid) ytitle("Public health expenditure (% of GDP)") ///
    xlabel(1995 2000 2005 2010 2014, tick) xmtick(1995/2014) xtitle("") name(figure1a, replace) ysize(4) nodraw

// Figure 1b
sort group year
twoway (line cvspending year if group == "OECD") ///
       (line cvspending year if group == "LAC"), ///
    legend(order(2 "LAC" 1 "OECD") title("Coefficient of Variation (CV)") ring(0)) ///
    yscale(range(0 50)) ylabel(0 (5) 50, grid) ytitle("CV of public health expenditure") ///
    xlabel(1995 2000 2005 2010 2014, tick) xmtick(1995/2014)  xtitle("") name(figure2a, replace) ysize(4) nodraw

// Combined Figures
graph combine figure1a figure2a, col(1) ysize(8) name(figure1, replace)

Jan 5, 2016

Random graphs (55): Box plots

wbopendata, indicator(NY.GDP.PCAP.KD) clear

// Select countries
#delimit ;
keep if inlist(countrycode, "AUS", "AUT", "BEL", "CAN", "DNK") |
        inlist(countrycode, "FIN", "FRA", "DEU", "GRC", "ISL") |
        inlist(countrycode, "IRL", "ITA", "JPN", "NLD") |
        inlist(countrycode, "NZL", "NOR", "PRT", "ESP") |
        inlist(countrycode, "SWE", "CHE", "USA") ;
#delimit cr

// Get rid of unnecessary years
keep countryname countrycode iso2code region regioncode ///
     indicatorname indicatorcode ///
     yr1960 yr1970 yr1980 yr1990 yr2000 yr2010
  
// Reshape into long format
reshape long yr, i(countryname) j(year)

// Prepare outcome variable
rename yr gdppc
label var gdppc "GDP per capita (constant 2005 US$)"

// Plot
graph box gdppc, over(year)

Dec 17, 2014

Random graphs (39): Plotting on a log axis


Rather than looking at a variable in absolute terms, namely GDP per capita in the upper panel of the Figure, it is also sometimes helpful to look at it in terms of percentage increases, as in the lower panel. Equal distances on the x-axis refer to equal percentage increases in GDP per capita. Four equidistant points on the x-axis are labeled, each indicating a fourfold increase in GDP.

use wvs2005_v20090901a.dta, clear

// Country variable
// 1) Turn into string
decode v2, gen(ctry)

// 2) Abbreviate
kountry ctry, from(other) stuck marker
ren _ISO3N_ cntry
kountry cntry, from(iso3n) to(iso2c)
ren _ISO2C_ country

// Generate outcome: % in good health
generate goodhealth = 100 if v11 <= 2
replace  goodhealth =   0 if v11  > 2
replace  goodhealth =   . if v11 == .

// Collapse data set
collapse (mean) goodhealth [pw = v259], by(country)

// Generate year variable
gen year = 2006

// Preserve collapsed data set
preserve

// Get GDP from World Bank data base
wbopendata, language(en - English) country() topics() indicator(NY.GDP.PCAP.PP.CD) clear long

// Fix obtained data set
ren ny_gdp_pcap_pp_cd gdp
ren iso2code country
keep if year == 2006
keep gdp country
drop if country == ""

// Save obtained GDP data
tempfile gdp
save `gdp'

// Restore 
restore

// Merge GDP with collapsed data set
merge m:1 country using `gdp'
keep if _merge == 3
drop _merge

// Create labels with thousand separator
label define gdp 20000 "20,000" ///
                 40000 "40,000" ///
                 60000 "60,000"
label val gdp gdp

// Plot on unlogged axis
twoway (scatter goodhealth gdp, mlabel(country) mlabpos(0) msymbol(none)) ///
       (lfit goodhealth gdp) ///
      , legend(off) xtitle("GDP per capita, 2006, PPP in current international $") ///    
        ytitle("% in good health") ///
        xlabel(0(20000)60000, valuelabels) ///
        name(unlogged, replace)

// Generate logged variable
gen loggdp = log(gdp) * 1000  // Multiply by 1,000 because only integers can be labeled

// Generate numbers for labeling
//  Round them to three decimal digits, then multiply by 1,000 to get integers
local log1 = round(log(1000), .001) * 1000
local log2 = round(log(1000 * 4), .001) * 1000
local log3 = round(log(1000 * 4 * 4), .001) * 1000
local log4 = round(log(1000 * 4 * 4 * 4), .001) *1000

*di `log1' _skip(2) `log2' _skip(2) `log3' _skip(2) `log4' 

// Create labels for numbers to be labeled
label define loggdp `log1' "1,000" ///
                    `log2' "4,000" ///
                    `log3' "16,000" ///                    
                    `log4' "64,000"
label value loggdp loggdp

// Plot on log axis
twoway (scatter goodhealth loggdp, mlabel(country) mlabpos(0) msymbol(none)) ///
       (lfit goodhealth loggdp) ///
      , legend(off) xtitle("GDP per capita, 2006, PPP in current international $") ///   
        ytitle("% in good health") ///
        xlabel(`log1' `log2' `log3' `log4', valuelabels) ///
        name(logged, replace)

// Combine plots
graph combine unlogged logged, col(1) ysize(8)

Jul 2, 2014

Random graphs (24): Small-multiples and overlayed line plots


clear

// SG.GEN.PARL.ZS - Proportion of seats held by women in national parliaments (%)
wbopendata, indicator(SG.GEN.PARL.ZS) clear long

// Keep European countries
keep if regioncode == "ECS" // "Europe & Central Asia (all income levels)"

// Keep EU-28
keep if inlist(iso2code, "AT", "BE", "BG", "CY", "CZ", "DE", "DK", "EE", "ES") | ///
        inlist(iso2code, "FI", "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LI") | ///
  inlist(iso2code, "LT", "LU", "LV", "NL", "PL", "PT", "RO", "SE", "SI") | ///
  inlist(iso2code, "SK") // EU-28

// Identify and rename salient variables
ren sg_gen_parl_zs women
label var women "% of national parliament seats held by women"
ren iso2code cntry
drop countrycode region regioncode

// Reshape to drop empty observations
reshape wide women, i(cntry) j(year)
dropmiss women1960-women2014, force
reshape long

// Calculate EU-28 mean
preserve
collapse (mean) women, by(year)
gen cntry = "{bf:EU-28}"
tempfile eu28
save `eu28', replace
restore
append using `eu28'

// Plot small-multiples figure twoway scatter women year /// , by(cntry /// , note(" ") /// caption("{it:Source:} World Development Indicators, date of extraction: 2015-05-25", span size(small))) /// cmissing(no) /// connect(l) /// msymbol(o) /// xtitle("") xlabel(1990 1997 2000 2005 2010 2014, ang(45)) /// ylabel(0 (10) 50) /// name(small_multiples, replace)
// Plot overlayed figure encode cntry, gen(country) // No strings for -xtline- xtset country year, yearly // Declare panel xtline women if cntry != "{bf:EU-28}" /// , overlay /// xlabel(1990 1997 2000 2005 2010 2014) xtitle("Year") /// plot1opts(lpattern(dash)) plot2opts(lpattern(dash)) /// plot3opts(lpattern(dash)) plot4opts(lpattern(dash)) /// plot5opts(lpattern(dash)) plot6opts(lpattern(dash)) /// plot7opts(lpattern(dash)) plot8opts(lpattern(dash)) /// plot9opts(lpattern(dash)) plot10opts(lpattern(dash)) /// plot11opts(lpattern(dash)) plot12opts(lpattern(dash)) /// plot13opts(lpattern(dash)) plot14opts(lpattern(dash)) /// plot15opts(lpattern(dash)) plot16opts(lpattern(dash)) /// plot17opts(lpattern(dash)) plot18opts(lpattern(dash)) /// plot19opts(lpattern(dash)) plot20opts(lpattern(dash)) /// plot21opts(lpattern(dash)) plot22opts(lpattern(dash)) /// plot23opts(lpattern(dash)) plot24opts(lpattern(dash)) /// plot25opts(lpattern(dash)) plot26opts(lpattern(dash)) /// plot27opts(lpattern(dash)) plot28opts(lpattern(dash)) /// name(overlay, replace) /// addplot(line women year if cntry == "{bf:EU-28}", /// lwidth(thick) lpattern(solid) sort(cntry year)) /// legend(order(29 "EU-28 (unweighted average)") pos(6) ring(0)) /// caption("{it:Source:} World Development Indicators, date of extraction: 2015-05-25", span size(small)) /// ytitle("% of national parliament seats" "held by women, EU-28 countries")