Sep 18, 2019

Random graphs (142): Boxplot using twoway commands with overlaid data

use v358 v392 v585 v829 if inrange(v829, 1, 5) using "data allbus\ZA4612_v1-0-1.dta", clear

// Subjective social mobility
recode v829 (5 = 1 "Much lower status") ///
            (4 = 2 "Lower status") ///
            (3 = 3 "About equal") ///
            (2 = 4 "Higher status") ///
            (1 = 5 "Much higher status") ///
            (95 96 98 99 = .), gen(ssm)
label var ssm "Status of own job compared to father's"

// Respondent's social status ISEI
clonevar isei = v358
replace  isei =    . if inlist(isei, 0, 99)
replace  isei = v392 if missing(isei)
replace  isei =    . if inlist(isei, 0, 99)
label var isei "Respondent's ISEI"

// Father's social status ISEI
clonevar fisei = v585 
replace  fisei =    . if inlist(fisei, 0, 99)
label var fisei "Father's ISEI"

// Objective status mobility
gen osm = isei - fisei
label var osm "Objective status mobility"


// Boxplot using -graph- command graph box osm, over(ssm, descending) horizontal /// yline(0) l1title("Status of own job compared to father's" "(self-assessment)") /// name(figure1, replace)


// Boxplot using -twoway- command

// Calculate stuff for boxplot
qui bys ssm: sum osm, detail
qui bys ssm: egen min = pctile(osm), p(25) // Interquartile range
qui bys ssm: egen max = pctile(osm), p(75) // Interquartile range
qui bys ssm: egen med = median(osm)

// Plot
twoway (scatter ssm osm, msymb(o) jitter(2) mcolor(red%10)) ///
       (scatter ssm med, msymb(oh) mcolor(black)) ///
       (rspike  min max ssm, horizontal lcolor(black)), ///
        ylabel(1/5, valuelabel) xline(0) ///
        ytitle("Status of own job compared to father's" "(self-assessment)") ///
        legend(order(2 "Median" 3 "Interquartile range") ring(0) pos(11)) ///
        xtitle(Objective social mobility) name(figure2, replace)
drop min max med // Drop stuff for boxplot