Search code examples
stata

How to move lines in my graph when they overlap


I want to draw a coefficient graph in Stata by using the below code.

However, there is one problem.

twoway  (scatter dv2 coef  if  controls=="basic", mcolor(blue) msize() lp(solid) msymbol(o) lwi(vthin) lcolor(blue)) ///
        (rcap ci_upper ci_lower dv2 if  controls=="basic", hor msymbol(dot) lwi() mcolor(blue) lcolor(blue) lp(dash))  ///
        (scatter dv2 coef  if controls=="w/o sampling weight", mcolor(red) msize()  lp(dash) msymbol(s) lwi(vthin)  lcolor(red)) ///
        (rcap ci_upper ci_lower dv2  if controls=="w/o sampling weight", hor  msymbol(dot) lwi()  mcolor(red) lcolor(red) lp(dash)),  ///
        title(Industry)  ytick(, grid) /*ylabel(1 "`: label (age_cat) 1'" 2 "`: label (age_cat) 2'" 3 "`: label (age_cat) 3'" 4 "`: label (age_cat) 4'", /// 
        labsize(small))*/ legend(pos(6) col(3)  order(1 "with sampling weight" 3 "without sampling weight")) ///
        ylabel() xtitle("Coefficient on Covid19 Pandemic", size(small)) xline(0, lc(black) lp(dash)) xlabel(-0.2(0.05)0.2) plotregion(margin(.1))  graphregion(color(white)) bgcolor(white)  xtit() ytit("", size(small)) 

My code produces this picture.

enter image description here

However, the graphs I see overlap. I want to move the graph I drew, but I don't know what code I need.


Solution

  • You need to offset each subset. Here 0.1 is just token: use whatever value works for you. For more discussion see this paper.

    gen dvb = dv2 + 0.1 
    gen dvc = dv2 - 0.1 
    
    twoway  (scatter dvb coef  if  controls=="basic", mcolor(blue) msize() lp(solid) msymbol(o) lwi(vthin) lcolor(blue)) ///
            (rcap ci_upper ci_lower dvb if  controls=="basic", hor msymbol(dot) lwi() mcolor(blue) lcolor(blue) lp(dash))  ///
            (scatter dvc coef  if controls=="w/o sampling weight", mcolor(red) msize()  lp(dash) msymbol(s) lwi(vthin)  lcolor(red)) ///
            (rcap ci_upper ci_lower dvc if controls=="w/o sampling weight", hor  msymbol(dot) lwi()  mcolor(red) lcolor(red) lp(dash)),  ///
            title(Industry)  ytick(, grid) /*ylabel(1 "`: label (age_cat) 1'" 2 "`: label (age_cat) 2'" 3 "`: label (age_cat) 3'" 4 "`: label (age_cat) 4'", /// 
            labsize(small))*/ legend(pos(6) col(3)  order(1 "with sampling weight" 3 "without sampling weight")) ///
            ylabel() xtitle("Coefficient on Covid19 Pandemic", size(small)) xline(0, lc(black) lp(dash)) xlabel(-0.2(0.05)0.2) plotregion(margin(.1))  graphregion(color(white)) bgcolor(white)  xtit() ytit("", size(small))