I am trying to make this visual from a book by Edward Tufte where he talks about using a stripped down version of the box plot as practice.
I wrote a code but I am unable to figure out why the length of whiskers keep coming out incorrect. Can someone help me out here?
Code:
sysuse auto, clear * Name of variable to use for box plot: local variable price * Display boxplot by which group? local group foreign * Plot type 1 capture separate `variable', by(`group') levelsof `group', local(lvl) foreach level of local lvl { sort `variable' quietly summ `group' local max = `r(max)' local min = `r(min)' local scale = `r(max)' - `r(min)' local offset : display abs(`scale'*0.02) quietly summ `variable' if `group' == `level', detail local level = `level' + 1 local xlab "`xlab' `level' `" "`:lab (`group') `=`level'-1''" "'" local med_p_`level' = `r(p50)' local p75_`level' = `r(p75)' local p25_`level' = `r(p25)' local iqr_`level' = `p75_`level'' - `p25_`level'' display "Median = `med_p_`level''" display "P75 = `p75_`level''" display "P25 = `p25_`level''" display "IQR = `iqr_`level''" display "Low = `=`p25_`level''-(1.5*`iqr_`level'')'" display "Max = `=`p75_`level''+(1.5*`iqr_`level'')'"" display "Varname = `variable'`=`level'-1'" egen llw_`level' = min(max(`variable'`=`level'-1', `=`p25_`level''-(1.5*`iqr_`level'')')) egen uuw_`level' = max(min(`variable'`=`level'-1', `=`p75_`level''+(1.5*`iqr_`level'')')) quietly summ uuw_`level' local max_`level' = `r(mean)' quietly summ llw_`level' local min_`level' = `r(mean)' local lines `lines' /// (scatteri `p75_`level'' `level' `max_`level'' `level', recast(line) lpattern(solid) lcolor(black) lwidth(1)) || /// (scatteri `p25_`level'' `level' `min_`level'' `level', recast(line) lpattern(solid) lcolor(black) lwidth(1)) || /// (scatteri `p75_`level'' `=`level' + `offset'' `p25_`level'' `=`level' + `offset'', recast(line) lpattern(solid) lcolor(black) lwidth(1)) || /// (scatteri `med_p_`level'' `=`level' + `offset'', ms(square) mcolor(background)) || } *drop llw* uuw* twoway `lines', /// ytitle("`: variable label `variable''") /// ylabel(2000(2000)10000) xtitle("") /// xlabel(`xlab', nogrid) /// xscale(range(`=`min' + 0.5' `=`max' + 1.5')) /// scheme(white_tableau) /// title("{bf}Tufte Styled Box Plot", pos(11) margin(b+3) size(*.7)) /// subtitle("`: variable label `variable'' grouped by `: variable label `group''", pos(11) margin(b+6 t=-3) size(*.6)) /// legend(off) * Tufte style box plot version 2 graph box mpg, box(1, color(white%0)) medtype(marker) medmarker(mcolor(black) mlwidth(0)) cwhiskers alsize(0) intensity(0) over(foreign) lintensity(1) lines(lpattern(solid) lwidth(medium) lcolor(black)) nooutside ylabel(, nogrid) scheme(white_tableau)
0 Response to Box plot help
Post a Comment