Mar 25, 2013

Random graphs (10): Complex bar graph

import excel data.xls, sheet("Table1") firstrow clear

// Create country variable
ren A country
kountry country, from(other) stuck
ren _ISO3N_ cntry
kountry cntry, from(iso3n) to(iso2c)
drop cntry
replace _ISO2C_ = "OECD 30" if country == "OECD-30"
replace _ISO2C_ = "EU-27" if country == "EU-27"
drop country
ren _ISO2C_ country
  
  // Drop some countries
  // -inlist()- would have been more elegant
#delim ;
drop if country == "AU" 
      | country == "CA" 
      | country == "CL" 
      | country == "IL" 
      | country == "JP"    
      | country == "KR"
      | country == "MX"
      | country == "NZ"
      | country == "OECD 30"
      | country == "US" ;
#delim cr

   // Turning of the axis labels can also be achieved 
   // in a more focused fashion
label var country ""
encode country, gen(cntry)

  // Recreate country variable based on NetCost
  // There might have been a more elegant 
  // solution to this
gsort -NetCost
list cntry NetCost
fre cntry
recode cntry ( 1 =  4 "AT") ///
       ( 2 = 23 "BE") ///
       ( 3 =  1 "CH") ///
       ( 4 = 11 "CZ") ///
       ( 5 =  7 "DE") ///
       ( 6 = 10 "DK") ///
       ( 7 = 19 "EE") ///
       ( 8 = 13 "ES") ///
       ( 9 =  8 "FI") ///
       (10 =  9 "FR") ///
       (11 =  3 "UK") ///
       (12 = 22 "EL") ///
       (13 = 20 "HU") ///
       (14 =  2 "IE") ///
       (15 = 14 "IS") ///
       (16 = 18 "LU") ///
       (17 = 12 "NL") ///
       (18 = 5  "NO") ///
       (19 = 17 "PL") ///
       (20 = 21 "PT") ///
       (21 = 16 "SE") ///
       (22 =  6 "SI") ///
       (23 = 15 "SK") ///
      , gen(cntry2) label(cntry2)

// Generate some variables and label them
generate taxreductions2 = Childcarebenefits + Taxreductions
generate otherbenefits2 = taxreductions2 + Otherbenefits
label var taxreductions2 "Tax reductions"
label var otherbenefits2 "Other benefits"
label var NetCost        "Net cost" 

// Try out twice
twoway (bar Childcarefee cntry, xlabel(1/23, valuelabels angle(90)))    ///
       (bar Childcarebenefits cntry, xvarlab(cntry)) ///
    ,name(s1, replace)
twoway (bar Childcarefee cntry, xlabel(1/23, valuelabels angle(90)))    ///
    (bar taxreductions2 cntry, xvarlab(cntry)) ///
       (bar Childcarebenefits cntry, xvarlab(cntry)) ///
       ,name(s2, replace)    

// Create full graph
twoway (bar Childcarefee cntry2, xlabel(1/23, valuelabels angle(90)))    ///
    (bar otherbenefits2 cntry2) ///
    (bar taxreductions2 cntry2) ///
       (bar Childcarebenefits cntry2) ///
       (scatter NetCost cntry2, msymbol(o)) ///
    (scatter ofnetfamilyincome cntry2, msymbol(dh)) ///
    ,name(s3, replace) xtitle("") ytitle("Childcare-related costs and benefits,"/*
    */ "% of AW") ///
    legend(row(2) pos(6) bexpand) ///
    note("Source: OECD (2011), {it:Doing Better for Families}, /*
           */ Figure 4.A2.1 B{char 151}doi: 10.1787/888932393426", span)