Feb 25, 2016

Random graphs (61): Dot plot for two groups

tempname foo
tempname foox
postfile `foo' str2 cntry perc_f perc_f_ll perc_f_ul ///
                          perc_m perc_m_ll perc_m_ul using `foox' , replace

levelsof cntry, local(levels1)

// Calculate proportions by country
foreach country of local levels1 {

    capture proportion nonesep_f if cntry == "`country'"
    *matrix list r(table)
    matrix fcoefs  = r(table)
    local fperc    = fcoefs[1,2] * 100
    local fperc_ll = fcoefs[5,2] * 100
    local fperc_ul = fcoefs[6,2] * 100

    capture proportion nonesep_m if cntry == "`country'"
    *matrix list r(table)
    matrix mcoefs  = r(table)
    local mperc    = mcoefs[1,2] * 100
    local mperc_ll = mcoefs[5,2] * 100
    local mperc_ul = mcoefs[6,2] * 100 
 
    di "`country'"  _skip(2) `fperc_ll' _skip(2) `fperc' _skip(2) `fperc_ul'
    di "`country'"  _skip(2) `mperc_ll' _skip(2) `mperc' _skip(2) `mperc_ul'

    if "`fperc'" != "" {    // Make sure that the loop doesn't break if empty
    post `foo' ("`country'") (`fperc') (`fperc_ll') (`fperc_ul') (`mperc') (`mperc_ll') (`mperc_ul') 
  }
}

postclose `foo'

// Use posted data set
use `foox', clear

// Reverse sign of one variable
replace perc_m    = -perc_m 
replace perc_m_ll = -perc_m_ll
replace perc_m_ul = -perc_m_ul

// Sort countries by size
egen order_ = rank(-perc_f), unique
labmask order_, value(cntry)

// Plot
twoway (dot perc_m order_, horizontal) ///
    (dot perc_f order_, horizontal) ///
      , ylabel(1/31, val) legend(order(1 "Men" 2 "Women") pos(6) row(1)) ///
        ytitle("") xtitle("% of men/women pooling all income") ///
  xline(0) ///
  xlabel(-100 "100" -50 "50" 0 "0" 50 "50" 100 "100") ///
        name(somefigure, replace) ysize(8)