Histogram ggplot2

648 views
Skip to first unread message

Wasteva

unread,
Oct 24, 2012, 2:13:49 PM10/24/12
to ggp...@googlegroups.com
 Can someone help me to include corresponding percentages of histograms sit on the individual blocks. Please I don't mean percentages on the y-axis.
Thanks.

Brandon Hurr

unread,
Oct 24, 2012, 2:16:02 PM10/24/12
to Wasteva, ggp...@googlegroups.com
Can you please provide an example dataset and your current working code to produce said histogram?

See the links below about providing a reproducible example if you're stuck. dput() is your friend and so is Legion™. 

On Wed, Oct 24, 2012 at 7:13 PM, Wasteva <steve...@gmail.com> wrote:
 Can someone help me to include corresponding percentages of histograms sit on the individual blocks. Please I don't mean percentages on the y-axis.
Thanks.


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Oct 24, 2012, 2:48:16 PM10/24/12
to Steve Ampah, ggplot2


On Wed, Oct 24, 2012 at 7:30 PM, Steve Ampah <steve...@gmail.com> wrote:

x<- c(rnorm(30),rnorm(30))
y<-rep(c("A","B","C"),20)

data<-data.frame(x,y)
 
ggplot(data,aes( x )+geom_histogram(binwidth=1)+facet_grid(y~.,margins=T)
 
I want a histogram of x across y so that with each A the total is 100% , that is if within A there are 4 blocks I expect percentages like 10%, 30% , 40%, 20%, and something similar for B and C.

You'll have to bin things outside of ggplot and then compute the percentages for each bin. 

If you want the SAME bins as used by hist(), you can use:
hist(Vec, breaks = ..., plot = FALSE)$counts

Another alternative is to use cut() to bin a vector and return a factor, 
upon which you can then use table() to get the counts. For example:

   table(cut(Vec, breaks = ...))

See ?hist and ?cut for more information.

HTH,
Marc Schwartz 

Ista Zahn

unread,
Oct 24, 2012, 3:09:42 PM10/24/12
to Wasteva, ggp...@googlegroups.com
Hi,

On Wed, Oct 24, 2012 at 2:13 PM, Wasteva <steve...@gmail.com> wrote:
> Can someone help me to include corresponding percentages of histograms sit
> on the individual blocks. Please I don't mean percentages on the y-axis.

There are really two issues here: 1) getting percentages on the
y-axis, and 2) getting the labels on top of the bars.

For the first one:

ggplot(mtcars, aes(x=mpg, y=..count../sum(..count..))) +
geom_histogram() +
scale_y_continuous("Percent", labels=percent)

For the second one:

ggplot(mtcars, aes(x=mpg, y=..count.., label=..count..)) +
geom_histogram() +
geom_text(stat="bin")

putting it all together we get

ggplot(mtcars, aes(x=mpg, y=..count../sum(..count..),
label=scales::percent(..count../sum(..count..)))) +
geom_histogram() +
geom_text(stat="bin") +
scale_y_continuous("Percent", labels=percent)

Hope that helps!
Ista

Wasteva

unread,
Oct 24, 2012, 4:14:09 PM10/24/12
to ggp...@googlegroups.com, Wasteva
Bravo Isata, it worked but there are 0%0%0%0%0% intersecting the x-axis as I have written what could that be.
Thanks

Wasteva

unread,
Oct 24, 2012, 4:19:52 PM10/24/12
to ggp...@googlegroups.com, Wasteva
Again how do I get the x-values to stay in the middle of the bins instead of staying to the left of the bins.
Thanks.

Ista Zahn

unread,
Oct 24, 2012, 4:40:25 PM10/24/12
to Wasteva, ggp...@googlegroups.com
There are bins where the count is zero, and these are labeled as well.
Perhaps you should consider increasing the binwidth.

Ista Zahn

unread,
Oct 24, 2012, 4:40:50 PM10/24/12
to Wasteva, ggp...@googlegroups.com
On Wed, Oct 24, 2012 at 4:19 PM, Wasteva <steve...@gmail.com> wrote:
> Again how do I get the x-values to stay in the middle of the bins instead of
> staying to the left of the bins.

I don't follow the question...
Reply all
Reply to author
Forward
0 new messages