Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: histogram with freq and percent labels
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: histogram with freq and percent labels
Date
Mon, 24 Feb 2014 12:44:55 +0000
I would not approach the problem that way.
It's difficult with what you propose to avoid a very busy graph, but
this shows some technique.
The basic idea is to calculate frequencies and percents and then you
can plot them as you wish.
Having bin width 2.5 and shifting one set of labels 1 unit up is
clearly tuned to the particular example.
sysuse auto, clear
gen mpg2 = 2.5 * floor(mpg/2.5)
bysort mpg2 : gen freq = _N
count if mpg < .
gen percent = string(100 * freq/r(N), "%2.1f") + "%"
clonevar mpg3 = mpg
replace mpg3 = mpg2 - 1.25
gen freq2 = freq + 1
twoway bar freq mpg3, barw(2.5) bfcolor(none) yla(, ang(h)) ///
|| scatter freq mpg3 , ms(none) mla(percent) mlabpos(12) ///
|| scatter freq2 mpg3 , ms(none) mla(freq) mlabpos(12) ///
legend(off) yti(frequency)
Nick
[email protected]
On 24 February 2014 11:07, Philipp <[email protected]> wrote:
> I'd like to create an histogram with frequency values and additionally I'd like to add labels of the frequency and the percentage.
> Without percentage label it is easy:
> hist x, freq addlabes
>
> My first idea was:
> hist x, freq addlabels addplot(x, percent addlabels)
> but addlabels is not working in the addplot-option.
>
> My second idea was adding a third plot (plot3) in plotregion1 (you can see that there already exist two plots when you enter the Graph Editor. One is for the bars and the other for the frequency labels. I have honestly no idea how to add a third plot, but i tried something):
>
> x can be 1, 2, 3:
>
> sum x
> local OBS r(N)
> tab x, matcell(M)
> matrix list M
> display `OBS'
> local p1 = M[1,1]/`OBS'
> local p2 = M[2,1]/`OBS'
> local p2 = M[3,1]/`OBS'
> display `p1' //is working
>
> local i1=1
> forvalues i of local N1 N2 N3 {
> gr_edit plotregion1.barlabels[`i1'].text = {}
> gr_edit plotregion1.barlabels[`i1'].text.Arrpush N=`i'
> local ++i1
> }
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/