Dear Stata-listers
I am wondering if there is a way to have both N and mean value reported inside bars, using - hbar - with the stack-command? So that for example, the text string shown inside each stacked bar would be "(n=51/m=3.5)" or something similar? Below I have added some code which takes me part of the way, but the bars are not stacked and N is to the left of the bar, not inside the bar. I would like the bars to be stacked over the groups Domestic, Foreign and Total. I have added the "Total" category in the data below.
In advance, thank you for any help and suggestions.
Best wishes,
Johanne
This results in this graph, which is not stacked and which has N reported. Never mind that in this case N is the same for each variable within each group of respondents, it is the general idea which is important.
My problem is how can I stack the bars over the groups and have N reported inside the bar, alongside the mean value?
I am wondering if there is a way to have both N and mean value reported inside bars, using - hbar - with the stack-command? So that for example, the text string shown inside each stacked bar would be "(n=51/m=3.5)" or something similar? Below I have added some code which takes me part of the way, but the bars are not stacked and N is to the left of the bar, not inside the bar. I would like the bars to be stacked over the groups Domestic, Foreign and Total. I have added the "Total" category in the data below.
In advance, thank you for any help and suggestions.
Best wishes,
Johanne
Code:
sysuse auto, clear keep foreign mpg trunk turn replace foreign=2 if foreign !=. label define origin 2 "Total", add save auto2, replace append using "C:\Program Files (x86)\Stata14\ado\base\a\auto.dta" keep foreign mpg trunk turn collapse /// (count) countmpg=mpg /// (count) counttrunk=trunk /// (count) countturn=turn /// (mean) meanmpg=mpg /// (mean) meantrunk=trunk /// (mean) meanturn=turn /// , by(foreign) browse reshape long count mean, i(foreign) j(which) string capture drop order2 capture drop group2 capture drop detail2 label define order2 1 mpg 2 trunk 3 turn encode which, gen(order2) label(order2) label def order2 1 " ", modify label def order2 2 " ", modify label def order2 3 " ", modify gen detail2 = " ({it:n} = " + string(count) + ")" egen group2 = group(order2 detail2), label separate mean, by(order) #delimit ; graph hbar (asis) mean?, over(group2, label(labsize(small))) over(foreign, label(labsize(small))) nofill bar(1, fcolor(eggshell) lcolor(black)) bar(2, fcolor(khaki) lcolor(black))bar(3, fcolor(olive_teal)lcolor(black)) blabel(bar, pos(inside) format(%12.1f) size(vsmall)) legend(lab(1 "MPG") lab(2 "Trunk")lab(3 "Turn") size(vsmall) keygap(0.5) symxsize(5) col(6) position(-2) ring(-1)) plotregion(lcolor(none)) scheme(s1mono) title("Some variables" " ", size(medlarge) ) ytitle(" ") note("Note: Some note", size(small) span) ylab(none) name(fig15statalist2, replace); graph save fig15statalist2, replace; #delimit cr
This results in this graph, which is not stacked and which has N reported. Never mind that in this case N is the same for each variable within each group of respondents, it is the general idea which is important.
My problem is how can I stack the bars over the groups and have N reported inside the bar, alongside the mean value?
Comment