Filling only one part of a histogram.

13 views
Skip to first unread message

Cedric Ginestet

unread,
Jun 17, 2010, 3:19:56 AM6/17/10
to ggplot2
Dear all,

I am trying to fill in, only one part of a histogram.
plo <- ggplot(as.data.frame(rnorm(1000)),aes(x=rnorm(1000))) +
geom_histogram() + geom_vline(xintercept=0); plo

How do you the part of the histogram, which is right of the vline?

Thanks for your help,
Cedric

Charlotte Wickham

unread,
Jun 17, 2010, 3:53:04 PM6/17/10
to Cedric Ginestet, ggplot2
With a little rewrite of your example, like this:

ggplot(data.frame(x = rnorm(1000)), aes(x = x)) +
geom_histogram(aes(fill = x < 0 )) + geom_vline(xintercept=0) +
scale_fill_manual(values = c("black","grey20"))

Charlotte

> --
> 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
>

Cedric Ginestet

unread,
Jun 18, 2010, 4:52:34 AM6/18/10
to ggplot2
Dear Charlotte,

Thank you so much for your help. That's very much appreciated.
Although your solution works well for some example, it doesn't seem to
be robust enough for different choices of cut-off points. Check this
example for instance:

x= rnorm(1000)
ggplot(data.frame(x), aes(x = x)) +
geom_histogram(aes(fill = x < 1 )) + geom_vline(xintercept=1) +
scale_fill_manual(values = c("red4","blue4"))

In general, the coloured part of the histogram will not exactly follow
the vertical line at 1.0. This is, I think because we are producing
two different histograms, as opposed to colouring one part of a single
histogram. With the standard plotting devices, you can do this with
the polygon function:

Rvec <- rnorm(1000); cutoff <- 1.5
h <- hist(Rvec,plot=FALSE,breaks=70)
y <- smooth(smooth(h$density)); x <- h$mids
plot(x,y, type="l",xlab=expression(r[ij]),ylab="Density");
grid(5,5,lwd=2)
polygon(c(cutoff, x[x>=cutoff]), c(0, y[x>=cutoff]), density=20)

I wonder whether the use of geom_polygon would solve the problem. What
do you think?
Best wishes,
Cedric

On Jun 17, 8:53 pm, Charlotte Wickham <cwick...@gmail.com> wrote:
> With a little rewrite of your example, like this:
>
> ggplot(data.frame(x = rnorm(1000)), aes(x = x)) +
>   geom_histogram(aes(fill = x < 0 )) + geom_vline(xintercept=0) +
>   scale_fill_manual(values = c("black","grey20"))
>
> Charlotte
>
> On Thu, Jun 17, 2010 at 12:19 AM, Cedric Ginestet
>

fernando

unread,
Jun 18, 2010, 12:39:42 PM6/18/10
to Cedric Ginestet, ggplot2
Hi Cedric,

Think you can obtain the result you want easily using stat_bin options,

Hope this helps,
fernando

x= rnorm(1000)
ggplot(data.frame(x), aes(x = x)) +

geom_histogram(aes(fill = x < 1), origin = -5, binwidth = 0.25) +


geom_vline(xintercept=1) +
scale_fill_manual(values = c("red4","blue4"))

-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
Cedric Ginestet
Enviado el: viernes, 18 de junio de 2010 10:53
Para: ggplot2
Asunto: Re: Filling only one part of a histogram.

Reply all
Reply to author
Forward
0 new messages