Hello everyone,
I'm stuggeling to add (vertical) lines for the mean and the standard deviation to my histogram. Attached you find the histogram showing the physical attractiveness by gender and migration status. I think I should first create means for all six groups and then include them in the histogram command. Can anyone advise me in the best way to do this?
This is my code to create the histogram:
This is what the histogram looks like at the moment:

Online I found some code to add vertical lines for mean and SD. I'm able to recreate a simple histogram with the lines but I'm struggling to implement it in my more complicate (loop) case.
Thank you in advance!
I'm stuggeling to add (vertical) lines for the mean and the standard deviation to my histogram. Attached you find the histogram showing the physical attractiveness by gender and migration status. I think I should first create means for all six groups and then include them in the histogram command. Can anyone advise me in the best way to do this?
This is my code to create the histogram:
Code:
egen mig_sex=group(sex migstatus) tab mig_sex table migstatus sex mig_sex lab def mig_sex 1 "Männlich: Keine Einwanderungsgeschichte" 2 "Erste Generation" 3 "Zweite Generation" /// 4 "Weiblich: Keine Einwanderungsgeschichte" 5 "Erste Generation" 6 "Zweite Generation" lab val mig_sex mig_sex tab migstatus sex fre mig_sex tab migstatus att if sex==0, chi2 V tab migstatus att if sex==1, chi2 V tab mig_sex if att!=. forvalues i=1/6 { sum mig_sex if mig_sex==`i' & att!=. local n`i' = r(N) } gen double wt=round(cdweight*100) histogram att [fweight=wt], by(mig_sex, row(2) title("Verteilung der physischen Attraktivität, erste Welle: Einwanderungsgeschichte" " ") /// note(`"{bf:Männlich}: Keine Einwanderungsgeschichte N = `n1', Erste Generation N = `n2', Zweite Generation N = `n3'"' /// `"{bf:Weiblich}: Keine Einwanderungsgeschichte N = `n4', Erste Generation N = `n5', Zweite Generation N = `n6'"', size(vsmall)) legend(off)) /// percent discrete name(mig_hist, replace) /// addlabel addlabopts(mlabposition(12) mlabformat(%4.1f) mlabsize(vsmall)) /// yscale(r(0(10)40)) xscale(r(1(1)7)) /// xlabel(1 "Sehr unattraktiv" 2 3 4 5 6 7 "Sehr attraktiv", angle(45)) /// xtitle("Physische Attraktivität" " ") /// scheme(plotplainblind)
Online I found some code to add vertical lines for mean and SD. I'm able to recreate a simple histogram with the lines but I'm struggling to implement it in my more complicate (loop) case.
Code:
summarize att local m=r(mean) local sd=r(sd) local low = `m'-`sd' local high=`m'+`sd' histogram att, /// fc(none) lc(green) xline(`m') /// xline(`low', lc(blue)) xline(`high', lc(blue)) scale(0.5) /// text(0.12 `m' `"mean = $`=string(`m',"%6.2f")'"', /// color(red) orientation(vertical) placement(2)) addlabel addlabopts(mlabposition(12) mlabformat(%4.1f) mlabsize(vsmall))
Comment