Dear All,
I am creating an overlapping histogram to show the difference in log wages for males and females. I want to add descriptive statistics to the graph to show the standard errors and median and a separate box showing mean, median, and sd values.
I have tried to follow several threads but I have failed to get the lines and text. When the text appears, the histogram only shows the male graph. Data sample using dataex:
The codes that I have tried:
It gives an error 13 invalid name.
The next code gives me the values but when I try to add lines it does not work.
Any leads and clues on how to solve it will be appreciated.
I am creating an overlapping histogram to show the difference in log wages for males and females. I want to add descriptive statistics to the graph to show the standard errors and median and a separate box showing mean, median, and sd values.
I have tried to follow several threads but I have failed to get the lines and text. When the text appears, the histogram only shows the male graph. Data sample using dataex:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input int RO3 float log_hourly_wage 2 3.212141 2 1.213983 1 3.657131 1 3.267666 1 3.218876 2 2.198173 1 4.527209 1 4.0734115 2 2.2118309 1 1.8345364 1 4.121373 1 3.669928 1 2.995732 1 3.2834144 1 2.716349 1 3.715908 2 3.6105475 1 4.775681 1 3.624341 1 3.267666 1 3.405753 1 2.70805 1 4.467549 1 3.657131 1 3.7784915 end label values RO3 RO3 label def RO3 1 "Male 1", modify label def RO3 2 "Female 2", modify
Code:
summarize log_hourly_wage if RO3 == 1 local group1_mean = round(r(mean), 0.01) local group1_median = round(r(p50), 0.01) local group1_sd = round(r(sd), 0.01) summarize log_hourly_wage if RO3 == 2 local group2_mean = round(r(mean), 0.01) local group2_median = round(r(p50), 0.01) local group2_sd = round(r(sd), 0.01) // Display the summary statistics in the graph gr twoway /// (histogram log_hourly_wage if RO3 == 1, bin(10) percent xlabel(, nogrid) ylabel(, nogrid) color(red) fcolor(gs12)) /// (histogram log_hourly_wage if RO3 == 2, bin(10) percent xlabel(, nogrid) ylabel(, nogrid) color(black) fcolor(gs12)) /// (rcap `group1_mean' 5 `group1_mean' `group1_mean' `group1_sd', horizontal lc(blue) lw(medthick)) /// (rcap `group2_mean' 5 `group2_mean' `group2_mean' `group2_sd', horizontal lc(red) lw(medthick)) /// (scatteri `group1_mean' 5 1, ms(Oh) mla(`group1_mean') mlabposition(0) mlabgap(0) mlabsize(small) mlabcolor(blue)) /// (scatteri `group2_mean' 5 1, ms(Oh) mla(`group2_mean') mlabposition(0) mlabgap(0) mlabsize(small) mlabcolor(red)) /// (scatteri `group1_median' 5 1, ms(Oh) mla(`group1_median') mlabposition(0) mlabgap(0) mlabsize(small) mlabcolor(blue)) /// (scatteri `group2_median' 5 1, ms(Oh) mla(`group2_median') mlabposition(0) mlabgap(0) mlabsize(small) mlabcolor(red)) /// legend(order(1 "Male" 2 "Female")) /// ytitle("Percent", size(small)) /// text(0.6 `group1_mean' "Male Mean: `group1_mean'", size(small) placement(ne) color(blue)) /// text(0.6 `group2_mean' "Female Mean: `group2_mean'", size(small) placement(ne) color(red)) /// text(0.4 `group1_median' "Male Median: `group1_median'", size(small) placement(e) color(blue)) /// text(0.4 `group2_median' "Female Median: `group2_median'", size(small) placement(e) color(red))
The next code gives me the values but when I try to add lines it does not work.
Code:
summarize log_hourly_wage if RO3 == 1 local group1_mean = r(mean) local group1_median = r(p50) local group1_sd = r(sd) summarize log_hourly_wage if RO3 == 2 local group2_mean = r(mean) local group2_median = r(p50) local group2_sd = r(sd) // Display the summary statistics in the graph gr twoway hist log_hourly_wage if RO3==1, bin(10) percent xlabel(, nogrid) ylabel(, nogrid) color(red) fcolor(%50)|| hist log_hourly_wage if RO3==2, bin(10) percent xlabel(, nogrid) ylabel(, nogrid) color(black) fcolor(%50) legend(order(1 "Male" 2 "Female" )) text(20 30 "Male Mean: `group1_mean'" /// 40 20 "Male Median: `group1_median'" /// 41 20 "Male SD: `group1_sd'", size(small) placement(ne) color(blue)) /// text(10 8 "Female Mean: `group2_mean'" /// 10 7 "Female Median: `group2_median'" /// 10 6 "Female: `group2_sd'", size(small) placement(e) color(red))
Comment