Any way to make histogram with unequal bin width?

203 views
Skip to first unread message

Mauricio

unread,
May 20, 2011, 4:05:29 PM5/20/11
to ggplot2
Seems that the 'breaks' argument for stat_bin requires constant bin
width. Is there a way to get around this?

For example, I called the following:

ggplot(data=x, aes(x=var1)) + stat_bin(breaks=c(0, 2.5, 5, 10, 20,
30))

and got "Error: position_stack requires constant width".

Thanks much

Kohske Takahashi

unread,
May 20, 2011, 7:47:06 PM5/20/11
to Mauricio, ggplot2
Hi

I'm not understand what the unequal-bin-width histogram indicates, and
also I afraid that these kinds of plots confuse readers.
But as a counter part of built-in hist function, which accepts the
variable bin width,
you can do it by hand like this:

d <- rnorm(1000)
h <- hist(d, breaks = c(-5, -3, 1, 2, 3, 5), plot =F)
r <- data.frame(h[2:4], xmin = head(h$breaks, -1), xmax = h$breaks[-1])
ggplot(r, aes(xmin = xmin, xmax = xmax, ymin = 0, ymax = density)) +
geom_rect(colour="white")

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

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

Reply all
Reply to author
Forward
0 new messages