Showing posts with label twoway bar. Show all posts
Showing posts with label twoway bar. Show all posts

Feb 13, 2018

Random graphs (125): Bar graphs

clear
sdmxuse data ESTAT, dataset(lfsa_epgar)

keep if time == "2010"
keep if inlist(geo, "AT", "BE", "BG", "CY", "CZ", "DK", "EE", "FI") ///
      | inlist(geo, "FR", "DE", "EL", "HU", "IE", "LT", "NL", "NO") ///
      | inlist(geo, "PL", "PT", "ES", "SE", "CH", "UK") 
keep if age == "Y_GE15"

replace geo = "GR" if geo == "EL"
kountry geo, from(iso2c)
rename NAMES_STD country

drop if sex == "T"
replace sex = "Men" if sex == "M"
replace sex = "Women" if sex == "F"
encode reason, gen(reasonno)
label define reasonno 1 "Looking after children or incapacitated adults" ///
                      2 "Other family or personal responsibilities" ///
                      3 "Own illness or disability" 4 "In education or training" ///
                      5 "Could not find a full-time job" 6 "Other", modify
twoway bar value reasonno, horizontal by(country sex, ///
           cols(4) note("{it:Source:} Eurostat, lfsa_epgar, 2010. Respondents 15 years or older.", size(vsmall)) ///
     title("Reasons for part-time work")) ///
           ylabel(1/6, val)  ///
           ytitle("") xtitle("% of part-time workforce") ysize(12) xsize(8) 

Nov 3, 2017

Random graphs (115): Bar graphs

// Data
use ESS7e02_1.dta, clear

// Drinking variable
recode alcfreq (7 = 0 "Never") (6 = 1 "Less than once a month") ///
               (5 = 2 "Once a month") (4 = 3 "2{c 150}3 times a month") ///
               (3 = 4 "Once a week") (2 = 5 "Several times a week") ///
               (1 = 6 "Every day") (77 88 99 = .), gen(alcohol)
label var alcohol "Alcohol consumption"

// Dichotomize alcohol variable
generate drinking = inlist(alcohol, 5, 6) if !missing(alcohol)      

// Country name variable
kountry cntry, from(iso2c) marker
rename NAMES_STD country
  
// Education
recode eisced (1 2   = 0 "Lower") ///
              (3 4 5 = 1 "Medium") ///
     (6 7   = 2 "High") ///
     (55/99 = .), gen(education)
label var education "Education"

// Alcohol consumption by country
histogram alcohol, percent disc horizontal ///
                   by(country, title("{bf:A} Frequency of drinking alcohohl", ///
                         justification(left) bexpand span) ///
                               note("")) ///
                   ylabel(0(1)6, val) ytitle("") ///
                   ysize(8) name(figure1, replace)

// Alcohol consumption by education by country
  // Collect predicted probabilities
capture program drop my_logit
program define my_logit, eclass
    syntax[if]
    marksample touse
    logit drinking if `touse'
    margins if `touse', post
    exit
end

statsby point = _b[_cons] se = _se[_cons], by(country education) clear: my_logit

 // Calculate stuff
replace point = point * 100
replace se = se * 100

gen lb = point - 1.96 * se
gen ub = point + 1.96 * se

  // Plot
twoway (bar point point education) ///
       (rspike lb ub education), ///
    by(country, legend(off) ///
                   title("{bf:B} Drinking by educational attainment", ///
                         justification(left) bexpand span) ///
                   note("")) ///
       xlabel(0 1 2, val ang(v)) ylabel(0 (10) 50) ///
       ytitle("% drinking more than once a week") ///
    ysize(8) ///
    name(figure2, replace)

graph combine figure1 figure2, col(2) ///
                               note(" " "{it:Source:} European Social Survey 2014", ///
                                    span justification(right) bexpand size(*.8))

Jun 2, 2016

Random graphs (89): Bar graph

clear

// Read in data
import excel "http://hdr.undp.org/sites/default/files/composite_tables/2015_Statistical_Annex_Table_5.xls", ///
       sheet("Table 5") cellrange(B11:C188)

// Prepare country variables
rename B country
drop if country == "HIGH HUMAN DEVELOPMENT"
drop if country == "MEDIUM HUMAN DEVELOPMENT"
drop if country == "LOW HUMAN DEVELOPMENT"

kountryadd "Venezuela (Bolivarian Republic of)" to "Venezuela" add
kountryadd "Bolivia (Plurinational State of)" to "Bolivia" add
kountry country, from(other) stuck
rename _ISO3N_ geo
kountry geo, from(iso3n) to(iso2c)
rename _ISO2C_ cntry
drop geo 

// Prepare GII variable
rename C gii
label var gii "Gender Inequality Index"
replace gii = "" if gii == ".."
destring gii, replace

// Identify countries included in the ESS round 3
generate ess = (inlist(cntry, "AT", "BE", "BG", "CY", "DK", "EE", "FI", "FR") ///
              | inlist(cntry, "DE", "HU", "IE", "NL", "NO", "PL", "PT", "RU") ///
              | inlist(cntry, "SK", "SI", "ES", "SE", "CH", "UA", "GB"))

// Sort countries by GII
egen order_ = rank(gii), unique

// Plot
twoway (bar gii order_, horizontal) ///
       (bar gii order_ if ess, horizontal fcolor(gs1)) ///
      , ysize(8) legend(order(1 "All countries" 2 "Countries in ESS round 3") ring(0)) ///
        xscale(alt) ytitle("") ylabel(none) xlabel(0 (.1) .8, grid) ///
        note(" " "{it:Source:} Human Development Report 2015, Table 5.", span)

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)