Histogram with density curve and rug

1,328 views
Skip to first unread message

Bob

unread,
Feb 5, 2011, 2:01:12 PM2/5/11
to ggplot2
Hi All,

I'm stuck with what seems to be a really trivial problem: getting a
histogram with a density curve and rug. I can do any two geoms, but
not all three. Here's some data with a vector of zeros just to see if
that would help:

x <- rnorm(100)
zeros <- rep(0, 100)
mydata <- data.frame(x, zeros)
head(mydata)

# Nice histogram with rug, but no density yet (y-axis is counts)
ggplot(mydata, aes(posttest) ) +
geom_histogram() + geom_rug()

# Nice histogram with density, but no rug (y-axis is proportion)
ggplot(mydata, aes(posttest, ..density.. ) ) +
geom_histogram() + geom_density()

# rug looks for density here and fails:
ggplot(mydata, aes(posttest, ..density.. )) +
geom_histogram(aes(posttest) ) +
geom_density() +
geom_rug()

# trying to get ..density.. out of the rug call
ggplot(mydata) +
geom_histogram(aes(posttest, ..density..) ) +
geom_density( aes(posttest, ..density..) ) +
geom_rug(mydata, aes(posttest) )

# trying again using vector of zeros to plot rug values against
ggplot(mydata) +
geom_histogram(aes(posttest, ..density..) ) +
geom_density( aes(posttest, ..density..) ) +
geom_rug(mydata, aes(posttest, zeros) )

I've got to be really close. Any ideas?

Thanks,
Bob

Brandon Hurr

unread,
Feb 5, 2011, 4:52:16 PM2/5/11
to Bob, ggplot2
Bob, 

I could be wrong, but this seemed to work... 

ggplot() +
 geom_histogram(data=mydata, aes(posttest, ..density..) ) +
 geom_density(data=mydata, aes(posttest, ..density..) ) +
 geom_rug(data=mydata, aes(posttest) )

Not sure why, but it complained with
Error: ggplot2 doesn't know how to deal with data of class uneval
.... until I told it what the data was for every geom. :/

Output attached.

B


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

hist.dens.rug.png

Bob

unread,
Feb 6, 2011, 8:54:29 AM2/6/11
to ggplot2
Brandon,

That did it! Thanks very much. I could swear I tried that exact
combination, but you know how that goes. Since they're all using
mydata, it also works as:

ggplot(data=mydata) +
geom_histogram( aes(posttest, ..density..) ) +
geom_density( aes(posttest, ..density..) ) +
geom_rug( aes(posttest) )

Thanks,
Bob

On Feb 5, 4:52 pm, Brandon Hurr <brandon.h...@gmail.com> wrote:
> Bob,
>
> I could be wrong, but this seemed to work...
>
> ggplot() +
>  geom_histogram(data=mydata, aes(posttest, ..density..) ) +
>  geom_density(data=mydata, aes(posttest, ..density..) ) +
>  geom_rug(data=mydata, aes(posttest) )
>
> Not sure why, but it complained with
> *Error: ggplot2 doesn't know how to deal with data of class uneval*
> .... until I told it what the data was for every geom. :/
>
> Output attached.
>
> B
>
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com>
> > More options:http://groups.google.com/group/ggplot2
>
>
>
>  hist.dens.rug.png
> 219KViewDownload
Reply all
Reply to author
Forward
0 new messages